我想每 X 秒获取一次用户坐标。
我添加了进入应用程序委托的位置。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
...
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
if([CLLocationManager locationServicesEnabled]){
[self.locationManager startUpdatingLocation];
}
...
在这里我处理新坐标
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation *loc = [locations objectAtIndex:0];
NSDate* eventDate = loc.timestamp;
NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
if (howRecent < 10)
{
CLLocation* location = [locations lastObject];
double lat = location.coordinate.latitude;
double lng = location.coordinate.longitude;
NSLog(@"lat:%f lng:%f", lat, lng);
...
但我不知道当应用程序在后台时如何继续获取 iPhone 坐标。我想我应该添加一些代码:
- (void)applicationDidEnterBackground:(UIApplication *)application...
但我不知道该怎么做才能让位置保持活力?