尽管启用了后台位置更新,但 CLLocationManager 委托在应用程序暂停后未收到位置更新

CLLocationManager delegate not receiving location updates after application is suspended despite background location updates enabled

我正在尝试在我的应用程序暂停时接收位置更新 - 根据我阅读的文档,这应该不是问题,我正在执行所有必需的步骤,但一旦应用程序被暂停, 位置更新似乎没有发生。

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

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

由于文档说明 "In the case of a suspended app, the system wakes up the app, delivers the update to the location manager’s delegate" 我希望我可以像应用程序在前台或后台一样处理位置更新。

我在“功能”选项卡中选择了位置更新,在 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

"You enable location support from the Background modes section of the Capabilities tab in your Xcode project. (You can also enable this support by including the UIBackgroundModes key with the location value in your app’s Info.plist file.) Enabling this mode does not prevent the system from suspending the app, but it does tell the system that it should wake up the app whenever there is new location data to deliver. Thus, this key effectively lets the app run in the background to process location updates whenever they occur."

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

"The system delivers location updates to background location apps when the app is in the foreground, is running in the background, or is suspended. In the case of a suspended app, the system wakes up the app, delivers the update to the location manager’s delegate, and then returns the app to the suspended state as soon as possible."

我遇到了同样的问题,防止应用暂停的唯一方法是设置

locationManager.pausesLocationUpdatesAutomatically = false

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