Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
885 views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...