【问题标题】:iOS Location Manager background updates stopped in iOS 10iOS 位置管理器后台更新在 iOS 10 中停止
【发布时间】: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


【解决方案1】:

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

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

【讨论】:

  • 现在我知道 iOS 9 中确实发生了变化,我看到了很多关于这个问题的搜索结果。
猜你喜欢
  • 2017-12-16
  • 2015-09-18
  • 1970-01-01
  • 1970-01-01
  • 2016-09-30
  • 2017-05-30
  • 2017-03-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多