如何在用户重启phone后继续定位?

How to continue tracking location after the user restarts the phone?

我的应用程序需要 post 将位置数据发送到我的后端。我已将位置权限设置为 always,并将 pausesLocationUpdatesAutomatically 设置为 false(对于 CLLocationManager),并且需要继续跟踪 phone 的位置,即使应用程序处于在后台或 phone 重新启动后。

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

我该怎么做?

顺便说一句,我知道

Apple 文档对此进行了很好的记录。 仅当位置管理器以 "startMonitoringSignificantLocationChanges" 启动时,您才能获得它。

例如:

1) 在App delegate中启动一个singletonGeoLocationManager.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) {
...
}

在 plist 中:

<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>