Restart location updates from the background

岁酱吖の 提交于 2020-01-07 04:28:08

问题


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.



来源:https://stackoverflow.com/questions/46300105/restart-location-updates-from-the-background

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