views:

159

answers:

1

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 information between program invocations (and reapply it to a CLLocation object)?

A: 

Hi,

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 :)

deanWombourne
I assume that removeObjectForKey would work (even though a double isn't strictly an object!)
deanWombourne
perfect, I had found that too (sorry for not RTFM'ing)
KevinDTimm