尽管启用了后台位置更新,但应用程序挂起后,CLLocationManager委派未接收到位置更新



我正试图在应用程序挂起时接收位置更新-根据我阅读的文档,这应该不是问题,我正在遵循所有必要的步骤,但一旦应用程序挂起来,位置更新似乎就不会发生。

我看到过类似的帖子,其中有涉及使用startMonitoringSignificantLocationChanges的解决方案,但根据我对文档的理解,我不需要在startUpdatingLocation之上实现这一点。

根据文档,如果启用了后台位置更新,系统将被暂停,但当有新的位置数据时会被唤醒。

由于文档中有"在应用程序挂起的情况下,系统会唤醒应用程序,并将更新传递给位置经理的代表">我希望我可以像处理前台或后台应用程序一样处理位置更新。

我在Capabilities选项卡中选择了位置更新,在info.plist中,关键UIBackgroundModes包括值数组中的位置。

我错过了什么?

作为参考,以下是我声明CLLocationManager的方式。

lazy var locationManager: CLLocationManager = {
let locationManager = CLLocationManager()
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.delegate = self
locationManager.activityType = .fitness
locationManager.requestAlwaysAuthorization()
locationManager.requestWhenInUseAuthorization()
locationManager.allowsBackgroundLocationUpdates = true
locationManager.pausesLocationUpdatesAutomatically = true
return locationManager
}()

https://developer.apple.com/library/content/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html#//apple_ref/doc/uid/TP40007072-CH4-SW1

"您可以从Xcode项目的"功能"选项卡的"背景模式"部分启用位置支持。(您也可以通过在应用程序的Info.plist文件中包含带有位置值的UIBackgroundModes键来启用此支持。)。)启用此模式不会阻止系统挂起应用程序,但它会告诉系统,只要有新的位置数据要传递,就应该唤醒应用程序。因此,这个密钥可以有效地让应用程序在后台运行,以便在位置更新发生时进行处理。">

https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/LocationAwarenessPG/CoreLocation/CoreLocation.html#//apple_ref/doc/uid/TP40009497-CH2-SW1

"当应用程序处于前台、在后台运行或挂起时,系统会将位置更新传递给后台定位应用程序。如果应用程序挂起,系统会唤醒应用程序,将更新传递给位置经理的代表,然后尽快将应用程序返回到挂起状态。">

我也遇到了同样的问题,防止应用程序挂起的唯一方法是设置

locationManager.pausesLocationUpdatesAutomatically = false

pausesLocationUpdatesAutomatically设置为true时,系统将应用程序置于挂起状态,并且将不再接收位置更改,直到用户再次手动将应用程序带到前台。

最新更新