在WatchOS 4上未收到后台GPS更新

Bud*_*ead 0 watchkit watchos-4

将这个问题与答案一起发布,以便其他人可以找到它。

我们在商店中有一个应用程序,它记录活动的路径。当我们将WatchOS更新到版本4时,当应用程序过渡到后台时,我们的后台gps更新停止了。

Bud*_*ead 5

一些研究在Apple的Developer网站上找到了此链接,表明我们需要在位置管理器中设置一个新属性。

self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
if (@available(watchOS 4.0, *)) {
    self.locationManager.activityType = CLActivityTypeOther;
    self.locationManager.allowsBackgroundLocationUpdates = YES; // NEW!!
}

[self.locationManager startUpdatingLocation];
Run Code Online (Sandbox Code Playgroud)

看到标有“ NEW !!”的行。这就是允许该应用再次接收后台位置更新的原因。您还需要按照链接中所述设置属性。

希望在挂起时接收位置更新的应用程序必须在其应用程序的Info.plist文件中包含UIBackgroundModes键(具有位置值),并将此属性的值设置为YES。进行后台更新时,需要使用带有位置值的UIBackgroundModes键