iOS 位置管理器后台更新在 iOS 10 后停止

iOS Location Manager background updates stopped in iOS 10

我有一个接收位置更新的健身应用程序,并且自 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 设置为 locationexternal-accessorybluetooth-central,因为我也有 Pebble 手表连接。我尝试添加 bluetooth-peripheral 但这没有帮助。我也设置了 NSLocationWhenInUseUsageDescriptionNSBluetoothPeripheralUsageDescription,它们正在工作。

新 属性 已添加到 iOS 9 - allowsBackgroundUpdates 中的 CLLocationManager。此 属性 默认为 false,如果您想在后台接收位置更新,则必须明确设置为 true

针对 iOS 8 SDK 构建的应用程序获得 grandfathered 功能并继续接收后台更新,即使没有设置此项 属性。