ios - 调用 didEnterRegion 时,didUpdateLocations 不再被调用

标签 ios swift core-location geofencing region-monitoring

我在同时处理地理围栏和用户当前位置时遇到问题。

当应用程序开始工作时,didUpdateLocations 完美调用,并且我获得了所需的用户位置。

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

但是一旦 didEnterRegion 委托(delegate)被调用,

func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {}

didUpdateLocations 将不再触发。 它们有关联吗?

我还检查了

func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {}

调用didEnterRegion后状态没有改变。 我还添加了

locationManager?.startUpdatingLocation()

didEnterRegion 中,但 didUpdateLocations 仍然没有被调用。

有谁知道问题出在哪里吗? 如何跟踪区域中的位置?

PS:调用 EnterRegion 时自动停止更新位置,调用 ExitRegion 时自动开始更新位置

更新:我使用 xCode9、iOS 10、swift 4。EnterRegionExitRegion 都是空的,我只是打印登录信息现在,他们将来会将一些数据发布到服务器。

最佳答案

the didUpdateLocations will not fire anymore. are they related together?

不!请参阅 here

didChangeAuthorizationStatus 在这里毫无意义。授权保持不变。它刚刚被停止。

正常跟踪不会对regionMonitoring产生任何影响。这是由操作系统管理的。

Updates stop only when the app is suspended, thereby preventing the app from being woken up to handle those events.

我的猜测是,当您进入某个区域时,您的应用程序将不再暂停,并且可以再次开始跟踪,因此您必须确保您的应用程序不会被暂停。要跟踪您可以使用UIApplication.shared.backgroundTimeRemaining。请参阅this example

  • 避免使用模拟器进行测试。由于应用程序的生命周期有点困惑。请参阅this post from an Apple employee 。您必须断开与 Mac 的连接并使用 logging让您拥有真实的体验
  • 确保您看到 this question 。它会引导您克服一些地理围栏限制,我不确定苹果自那时以来改进了多少

但说实话,我仍然认为你只是犯了一个简单的错误,最终导致你的位置跟踪停止......

关于ios - 调用 didEnterRegion 时,didUpdateLocations 不再被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47843269/

相关文章:

ios - 当应用程序/ View Controller 离开前台时, Realm 因无效对象而崩溃

Swift - 如果未授予核心位置请求权限

ios - 采用什么策略来监控后台用户位置?

ios - GMSMapView:以坐标为中心并缩小以显示用户位置

ios - Prototype Cell 中的 UISwitch,多行仅引用最后一行。为什么

swift - 添加 30 天后如何修复日期格式

ios - 我可以根据 map View 上已有的路线获取大概的行程时间吗?

iphone - 将我的 JSON 放入 tableView

ios - 为什么我的 JSON URL 不输出 JSON 数据?

ios - 在当前 View 中显示警报,以及重定向到另一个 View