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