发布这个问题并附上答案,以便其他人可以找到它。
我们在商店中有一个记录活动路径的应用程序。当我们将 WatchOS 更新到版本 4 时,我们的后台 gps 更新在应用程序转换到后台时停止。
一些研究在Apple 的开发者网站上发现了这个链接,表明我们需要在位置管理器中设置一个新属性。
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];
见标有“新!!”的行 这就是允许应用再次接收后台位置更新的原因。您还需要按照链接中的说明设置属性。
想要在挂起时接收位置更新的应用程序必须在其应用程序的 Info.plist 文件中包含 UIBackgroundModes 键(带有位置值),并将此属性的值设置为 YES。后台更新需要具有位置值的 UIBackgroundModes 键