Persisting CoreLocation
I have a need to store the current location to 'disk' on the iphone if the application I'm writing is terminated. Then, when the app starts again, I want to restore this information. However, the CLLocation coordinate property is read only.
What can I do to save this info开发者_运维百科rmation between program invocations (and reapply it to a CLLocation object)?
You could use NSDefaults to store it. Something like
#define kLocationLat @"LOCATION_LAT"
#define kLocationLng @"LOCATION_LNG"
// Store the location
[[NSUserDefaults standardUserDefaults] setDouble:location.coordinate.lat forKey:kLocationLat];
[[NSUserDefaults standardUserDefaults] setDouble:location.coordinate.lng forKey:kLocationLng];
// Retrieve the location
CLLocationDegrees lat = [[NSUserDefaults standardUserDefaults] doubleForKey:kLocationLat];
CLLocationDegrees lng = [[NSUserDefaults standardUserDefaults] doubleForKey:kLocationLng];
CLLocation *location = [[CLLocation alloc] initWithLatitude:lat longitude:lng];
Sam
PS I don't have a mac to hand so there might be syntax errors in the above code but you get the idea :)
精彩评论