我在同时处理地理围栏
和用户当前位置时遇到问题。
当应用程序开始工作时,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。EnterRegion
和 ExitRegion
都是空的,我只是打印登录信息现在,他们将来会将一些数据发布到服务器。
最佳答案
the didUpdateLocations will not fire anymore. are they related together?
不!请参阅 here
didChangeAuthorizationStatus
在这里毫无意义。授权保持不变。它刚刚被停止。
正常跟踪不会对regionMonitoring产生任何影响。这是由操作系统管理的。
因此,您的正常跟踪可能由于后台运行时间过长而停止。为了避免设置
pausesLocationUpdatesAutomatically
为false
,默认为true
还要确保
allowsBackgroundLocationUpdates
设置为true
。从其链接:
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/