【发布时间】: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 ,你能解决这个问题吗?如果是,你愿意在这里分享它作为正确答案吗