Persisting CoreLocation

前端 未结 1 703
小鲜肉
小鲜肉 2020-12-20 10:19

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

相关标签:
1条回答
  • 2020-12-20 10:38

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

    0 讨论(0)
提交回复
热议问题