Writing to a plist in a LocationManager method
I am trying to write a latitude value and longitude value to a plist inside a LocationManager method.
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
NSString *latitude = [[NSString alloc] initWithFormat:@"%g", newLocation.coordinate.latitude];
NSString *longitude = [[NSString alloc] initWithFormat:@"%g", newLocation.coordinate.longitude];
NSString *filePath = [[NSBundle mainBundle] pathForResource:
开发者_StackOverflow社区 @"PlayerData" ofType:@"plist"];
NSMutableDictionary* plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
[plistDict setValue:latitude forKey:@"MyLatitude"];
[plistDict setValue:longitude forKey:@"MyLongitude"];
[plistDict writeToFile:filePath atomically: YES];
myLatitude.text = latitude;
myLongitude.text = longitude;
NSLog(@"Location: %@", [newLocation description]);
}
For some reason it never writes the value to the plist, but prints out the values correctly.
NSLog(@"Lat i s:%@ Lon is :%@", latitude, longitude);
What would cause this not to update/write to the plist?
Thanks
iOS applications are sandboxed, so you can't write to the application bundle. You need to write to one of these directories. Which one you need to write to depends on what kind of data it is.
You can't write to the main bundle (may work in simulator but not device).
Try saving the file to the Documents folder instead.
精彩评论