从后台重新启动位置更新 [英] Restart location updates from the background

查看:166
本文介绍了从后台重新启动位置更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个监视用户位置的应用程序。

在我调用 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆