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

大憨熊 提交于 2019-12-23 12:27:30

问题


My application need to post the location data to my backend. I have set the location permissions to always and set the pausesLocationUpdatesAutomatically to false for CLLocationManager and need to continue tracking the phones location even when the application is in background or after the phone is restarted.

I'm able to make it work when the application is in background mode. But it stops working when the phone is restarted.

How can I do this?

By the way, I know the


回答1:


This is WELL documented by Apple documentations. Only if Location manager is started with "startMonitoringSignificantLocationChanges" you can get it.

So for example:

1) in App delegate start a singleton GeoLocationManager.shared.locationManager, so if you restart will be running.

2) in call back:

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

update your position

it will be re-entrant if crashes. I think You already set il plist:

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

note: add code for privacy, as since ios9 is mandatory..

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

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


来源:https://stackoverflow.com/questions/52000463/how-to-continue-tracking-location-after-the-user-restarts-the-phone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!