【发布时间】:2017-03-26 19:44:55
【问题描述】:
我有一个接收位置更新的健身应用程序,并且自 iOS 4 以来一直在后台运行良好。我刚刚注意到,当应用程序在 iOS 中移动到后台(切换应用程序或锁定设备)时,位置更新现在停止10. 未发现功能或后台权限变更通知;有谁知道发生了什么?
我的位置设置非常基本:
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,它们正在工作。
【问题讨论】:
-
您不需要外部附件背景模式。除非您使用的是外部附件框架。从 ios9 开始,您需要设置
allowsBackgroundLocationUpdates,您似乎没有这样做 developer.apple.com/reference/corelocation/cllocationmanager/… 但是如果您在 ios9 之前最后一次重建您的应用程序,那么旧的行为就会被继承 -
就是这样!自 iOS 8 以来我没有更新应用程序,所以我不知道 iOS 9 的变化。如果您将此作为答案添加,我会接受。
标签: ios core-location background-process core-bluetooth