-->

Restart location updates from the background

2019-08-27 19:19发布

问题:

I'm working on an app that monitors the user's location.
After I call startUpdatingLocation() I can put the app in the background. It continues to update the location. That works well.

My question is: can I restart the continuous location updates from a didExitRegion call in the background? Now if I simply call startUpdatingLocation() again, the app gets killed after 10 seconds.

func locationManager(_ manager: CLLocationManager, didExitRegion region: CLRegion) {
    locationManager.startUpdatingLocation()
}

So the steps would be:

  • I ask the location manager to startUpdatingLocation().
  • The user puts the app in the background while the location updates are continuously being received.
  • The user stops for a while (for a coffee for example) so I ask the location manager to put down a geofence and stopUpdatingLocation().
  • The user continues his drive so when he leaves the geofence I'd like to ask the location manager to startUpdatingLocation() again.

回答1:

Please use

startMonitoringSignificantLocationChanges()

which works even after the app is killed. The startUpdatingLocation() is terminated by the system automatically if your app is killed.

You will also need to enable these as indicated in the picture.