2

我想每 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...

但我不知道该怎么做才能让位置保持活力?

4

1 回答 1

2

在您的 info.plist 中:

R.Click -> 添加一行并选择Required background modes-> 并在项目 0 处选择App registers for location updates

于 2012-10-11T12:52:25.483 回答