我有一个健身应用程序,可以接收位置更新,并且自iOS 4以来一直在后台工作正常。我只是注意到当iOS中的应用程序移动到后台(切换应用程序或锁定设备)时,位置更新会停止。我没有发现任何功能或背景特权变更的通知;有谁知道发生了什么?
我的位置设置非常基本:
if (!self.locationManager) {
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.distanceFilter = kCLDistanceFilterNone;
self.locationManager.activityType = CLActivityTypeFitness;
self.locationManager.pausesLocationUpdatesAutomatically = TRUE;
}
// request permissions on iOS 8+
if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
[self.locationManager requestWhenInUseAuthorization];
}
然后这个回调接收更新:
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
DLog(@"didUpdateToLocation");
}
当应用程序处于前台时,一切都很好。一旦移动到后台,此消息就会出现在控制台中:
/BuildRoot/Library/Caches/com.apple.xbs/Sources/ExternalAccessory/ExternalAccessory-353.22.1/EAAccessoryManager.m:__52-[EAAccessoryManager _checkForConnectedAccessories:]_block_invoke-785 ending background task
然后更新停止。当我将应用程序带到前台时,它们会再次恢复。
我不是直接使用EAAccessoryManager
而只能猜测它是什么。我正在使用BluetoothLE
框架从心率监视器接收数据,但是当我禁用此功能(停止实例化BluetoothLE
对象)时,问题仍然存在。
我的UIBackgroundModes设置为location
,external-accessory
和bluetooth-central
因为我也有Pebble手表连接。我尝试添加bluetooth-peripheral
,但这没有帮助。我也有NSLocationWhenInUseUsageDescription
和NSBluetoothPeripheralUsageDescription
设置,这些都在工作。
在iOS 9中的CLLocationManager
添加了一个新属性 - allowsBackgroundUpdates
。如果要在后台接收位置更新,则此属性默认为false
,并且必须显式设置为true
。
针对iOS 8 SDK构建的应用程序获得了grandfathered
功能,并且即使不设置此属性也会继续接收后台更新。