2017-01-15 20 views
1

我试图在暂停应用程序时接收位置更新 - 根据我已阅读的文档,这应该不是问题,而我我遵循所有必需的步骤,但是一旦应用程序被暂停,位置更新似乎不会发生。尽管已启用后台位置更新,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项目功能选项卡的背景模式部分位置的支持。(你也可以通过包括在位置值的UIBackgroundModes键启用此支持你)启用此模式并不会阻止系统暂停应用程序,但它确实告诉系统,只要有新的位置数据要传送,它就应该唤醒应用程序。因此,该键有效地让应用程序在后台运行以处理位置更新。“

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

“该系统提供位置更新背景位置信息的应用时,应用程序在前台运行,在后台运行时,或悬浮在悬浮应用程序的情况下,系统被唤醒启动应用程序,将更新传递给位置管理器的代理,然后尽快将应用程序恢复到暂停状态。“

回答

1

我有同样的问题,并防止beeing暂停该应用程序的唯一方法是设置

locationManager.pausesLocationUpdatesAutomatically = false 

pausesLocationUpdatesAutomatically设置为true系统使应用程序处于暂停状态,不会接收位置直到用户手动将应用程序再次移至前台。