从后台重新启动位置更新 [英] Restart location updates from the background
问题描述
我正在开发一个监视用户位置的应用程序。
在我调用 startUpdatingLocation()
之后,我可以将该应用程序置于后台。它继续更新位置。
我的问题是:我可以从 didExitRegion
调用重新启动连续位置更新吗?在后台?现在,如果我再次简单地调用 startUpdatingLocation()
,那么应用程序会在10秒后被杀死。 $ b
func locationManager(_ manager:CLLocationManager,didExitRegion region:CLRegion){
locationManager.startUpdatingLocation()
}
所以步骤是:
- 我要求位置管理器
startUpdatingLocation()
。 - 用户将应用程序置于后台,同时不断接收位置更新。
- 用户停一会儿(例如喝杯咖啡),所以我要求位置管理员放下一个地理围栏,
stopUpdatingLocation()
。 - 用户继续他的驱动器,所以当他离开geofence时,我想再次请求位置管理器
startUpdatingLocation()
。
请使用
startMonitoringSignificantLocationCha nges()
即使在应用程序被终止后也能正常工作。如果您的应用程序被终止,系统会自动终止startUpdatingLocation()。
您还需要按照图中所示启用它们。
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.
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.
这篇关于从后台重新启动位置更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!