我的应用程序需要将位置数据发布到我的后端。我已将位置权限设置为并always
设置pausesLocationUpdatesAutomatically
为并且需要继续跟踪手机位置,即使应用程序处于后台或手机重启后也是如此。false
CLLocationManager
当应用程序处于后台模式时,我能够使其工作。但是当手机重新启动时它会停止工作。
我怎样才能做到这一点?
顺便说一句,我知道
我的应用程序需要将位置数据发布到我的后端。我已将位置权限设置为并always
设置pausesLocationUpdatesAutomatically
为并且需要继续跟踪手机位置,即使应用程序处于后台或手机重启后也是如此。false
CLLocationManager
当应用程序处于后台模式时,我能够使其工作。但是当手机重新启动时它会停止工作。
我怎样才能做到这一点?
顺便说一句,我知道
这是由 Apple 文档记录的 WELL。只有使用“startMonitoringSignificantLocationChanges”启动位置管理器时,您才能获得它。
例如:
1)在App delegate中启动一个单例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) {
...
}
在 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>