【问题标题】:Deferring Location Updates in Background在后台推迟位置更新
【发布时间】:2014-03-28 21:41:59
【问题描述】:

我有一个在前台和后台收集位置数据的应用。为了节省电池,我想使用 allowDeferredLocationUpdatesUntilTraveled 属性,如Apple Doc 中所述 从文档中,allowDeferredLocationUpdatesUntilTraveled 在前台设置(基于时间和距离)。一旦应用进入后台,我们将不会收到定期的位置更新,而是根据 allowDeferredLocationUpdatesUntilTraveled 接收它们。

我已经实现了以下代码,但延迟不会在后台调用。

#pragma mark - Location Manager
-(void) setLocationManager
{
    locationManager = [[CLLocationManager alloc] init];
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    locationManager.distanceFilter = kCLDistanceFilterNone;
    locationManager.delegate = self;
    [locationManager startUpdatingLocation];
}


-(void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
     NSLog(@"didUpdateLocations");

     if (!deferringUpdates)
     {
         CLLocationDistance distance = 200;
         NSTimeInterval time = 60;
    
         [locationManager allowDeferredLocationUpdatesUntilTraveled:distance timeout:time];
    
          deferringUpdates = YES;
      }
}

-(void)locationManager:(CLLocationManager *)manager didFinishDeferredUpdatesWithError:(NSError *)error

{
    NSLog(@"didFinishDeferredUpdatesWithError");

}

SetLocationManager 在前台被调用并且工作正常。一旦应用程序进入后台,我仍然会收到定期的位置更新,而不是 allowDeferredLocationUpdatesUntilTraveled。

我还在 info.plist 文件中设置了以下值 后台获取 后台位置更新 设备功能 - 定位服务

有人幸运地实现了这个吗?

【问题讨论】:

  • locationManager:didFinishDeferredUpdatesWithError: 方法返回的error 的值是多少?
  • 嘿@mobiletest ,你能解决这个问题吗?如果是,你愿意在这里分享它作为正确答案吗

标签: ios7 cllocationmanager


【解决方案1】:

您需要在 didFinishDeferredUpdatesWithError 中将 deferringUpdates 设置回 NO:根据 Apple 文档,“委托的 locationManager:didFinishDeferredUpdatesWithError: 方法在您每次调用 (allowDeferredLocationUpdatesUntilTraveled) 时准确调用 一次”。

因此 allowDeferredLocationUpdatesUntilTraveled 只被调用一次,直到 deferringUpdates 为 NO。

【讨论】:

    猜你喜欢
    • 2013-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-10
    • 1970-01-01
    相关资源
    最近更新 更多