调用 didEnterRegion 时不再调用 didUpdateLocations
didUpdateLocations not being called anymore when didEnterRegion is being called
我在同时处理 Geofence
和用户当前位置时遇到问题。
当应用程序开始运行时,didUpdateLocations
完美调用并且我获得了所需的用户位置。
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
但是一旦 didEnterRegion
委托被调用,
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
是空的,我现在只是在其中打印一个日志,它们将 post 将来向服务器发送一些数据。
the didUpdateLocations will not fire anymore. are they related
together?
否! 参见
这里的didChangeAuthorizationStatus
没有意义。授权保持不变。刚刚停止。
普通追踪不会对regionMonitoring产生任何影响。即由 OS.
管理
所以也许您的正常跟踪已经停止,因为在后台的时间太长了。为避免将 pausesLocationUpdatesAutomatically
设置为 false
,默认值为 true
还要确保 allowsBackgroundLocationUpdates
设置为 true
。从其 link:
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. You have to disconnect from your mac and use logging让你有真实的体验
- 确保您看到 this question。它会带您了解一些地理围栏限制,我不确定 Apple 从那时起改进了多少
但老实说,我仍然认为您只是犯了一个简单的错误,最终以某种方式停止了您的位置跟踪...
我在同时处理 Geofence
和用户当前位置时遇到问题。
当应用程序开始运行时,didUpdateLocations
完美调用并且我获得了所需的用户位置。
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
但是一旦 didEnterRegion
委托被调用,
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
是空的,我现在只是在其中打印一个日志,它们将 post 将来向服务器发送一些数据。
the didUpdateLocations will not fire anymore. are they related together?
否! 参见
这里的didChangeAuthorizationStatus
没有意义。授权保持不变。刚刚停止。
普通追踪不会对regionMonitoring产生任何影响。即由 OS.
管理所以也许您的正常跟踪已经停止,因为在后台的时间太长了。为避免将
pausesLocationUpdatesAutomatically
设置为false
,默认值为true
还要确保
allowsBackgroundLocationUpdates
设置为true
。从其 link:
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. You have to disconnect from your mac and use logging让你有真实的体验
- 确保您看到 this question。它会带您了解一些地理围栏限制,我不确定 Apple 从那时起改进了多少
但老实说,我仍然认为您只是犯了一个简单的错误,最终以某种方式停止了您的位置跟踪...