用户重启手机后如何继续跟踪位置?



我的应用程序需要将位置数据发布到我的后端。我已将位置权限设置为always并将pausesLocationUpdatesAutomatically设置为false以进行CLLocationManager,即使应用程序在后台或手机重新启动后,也需要继续跟踪手机位置。

当应用程序处于后台模式时,我能够使其工作。但是当手机重新启动时,它会停止工作。

我该怎么做?

顺便说一句,我知道

这在 Apple 文档中得到了很好的记录。 只有当位置管理器以"开始监控重大位置更改"启动时,您才能获得它。

所以例如:

1(在应用程序委托中启动一个单一的GeoLocationManager.shared.locationManager,所以如果你重新启动将运行。

2( 回拨:

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
{

更新您的位置

如果崩溃,它将是可重入的。 我认为您已经设置了 il plist:

<key>UIBackgroundModes</key>
<array>
<string>location</string>
</array>

注意:添加隐私代码,因为iOS9是强制性的。

func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
...
}

在列表上:

<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>Needed to track on maps even if background</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>Needed to track on maps even if background</string>

最新更新