【发布时间】:2012-03-14 12:18:18
【问题描述】:
我正在为越狱 iPhone 开发。我正在阅读 iOS 5 入门书籍。目前,我使用了书中的位置代码并稍作更改。我在代码中所做的更改是我将位置写入文本文件中。但是程序崩溃了。虽然没有任何修改的相同程序运行良好。
代码如下:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = 2.0f;
[locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
FILE *file = fopen("/usr/locations.txt", "a");
if (!file)
{
fprintf(file, "Error opening file");
}
if (startingPoint == nil)
self.startingPoint = newLocation;
NSString *latitudeString = [NSString stringWithFormat:@"%g\u00B0",
newLocation.coordinate.latitude];
latitudeLabel.text = latitudeString;
NSNumber *latitude = [[NSNumber alloc] initWithDouble:newLocation.coordinate.latitude];
NSNumber *longitude = [[NSNumber alloc] initWithDouble:newLocation.coordinate.longitude];
NSString *longitudeString = [NSString stringWithFormat:@"%g\u00B0",
newLocation.coordinate.longitude];
longitudeLabel.text = longitudeString;
NSString *horizontalAccuracyString = [NSString stringWithFormat:@"%gm",
newLocation.horizontalAccuracy];
horizontalAccuracyLabel.text = horizontalAccuracyString;
NSString *altitudeString = [NSString stringWithFormat:@"%gm",
newLocation.altitude];
altitudeLabel.text = altitudeString;
NSString *verticalAccuracyString = [NSString stringWithFormat:@"%gm",
newLocation.verticalAccuracy];
verticalAccuracyLabel.text = verticalAccuracyString;
CLLocationDistance distance = [newLocation
distanceFromLocation:startingPoint];
NSString *distanceString = [NSString stringWithFormat:@"%gm", distance];
distanceTraveledLabel.text = distanceString;
struct tm *local;
time_t t;
t = time(NULL);
local = localtime(&t);
fprintf(file, "Local time and Date: %s ", asctime(local));
//----------------- ------------------------------------------------------------
fprintf(file, "Latitude = %lf, Longitude = %lf \n",
newLocation.coordinate.latitude, newLocation.coordinate.longitude);
fclose(file);
}
【问题讨论】:
-
你读过Apple’s documentation关于文件系统访问的内容吗?阅读。
-
现在,我只想在控制台上打印位置。但我也无法这样做。我的开发IDE是Xcode 4.2,iOS版本是5.0.1