ios - 启用后禁用 allowsBackgroundLocationUpdates (CLLocationManager) 不起作用

标签 ios objective-c swift core-location cllocationmanager

我的应用程序与持续的后台位置更新一起工作。 当然它有所有的权限和其他东西,比如

allowsBackgroundLocationUpdates = true

最初我想在后台使用 NSProcessInfoPowerStateDidChange 通知响应 LowPowerMode 更改。并在不调用 stopUpdatingLocation 的情况下禁用 allowsBackgroundLocationUpdates 停止更新位置

但是收到通知和设置后才发现

allowsBackgroundLocationUpdates = false

在后台应用程序正在运行。

所以我走得更远,确定了 disabling allowsBackgroundLocationUpdates 甚至 in foreground 在它最初设置为 true 根本不起作用。应用程序继续在后台运行。

Apple 关于此事的文档

you use this property to enable and disable the behavior based on your app’s behavior


来自 CLLocationManager 类,allowsBackgroundLocationUpdates 属性的描述

With UIBackgroundModes set to include "location" in Info.plist, you must also set this property to YES at runtime whenever calling -startUpdatingLocation with the intent to continue in the background. Resetting this property to NO is equivalent to omitting "location" from the UIBackgroundModes value.

Simple project在 GitHub 上显示了我提到的行为。

那么问题来了,这是属性(property)的故意行为吗?

最佳答案

When the value of this property is false, apps receive location updates normally while running in either the foreground or background based on its current authorization. Updates stop only when the app is suspended, thereby preventing the app from being woken up to handle those events.Apple API Discussion.

我认为属性(property)讨论可以解决你的问题。

关于ios - 启用后禁用 allowsBackgroundLocationUpdates (CLLocationManager) 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41704302/

相关文章:

ios - 全局将 UIImageView 添加到 UINavigationBar 而不是标题

ios - UITableViewCell 3 选择的时间更改多个选择的背景颜色

iphone - iOS 上的区域监控

ios - 将 16 位颜色范围内的值转换为 UIColor

ios - 如何在 GLSL ES 中编写 const 数组

ios - 有没有办法(或工具)实时更新 iOS 模拟器?

swift - 如何获取新的 swift AttributedString 的大小?

ios - 从 XIB 加载 View 作为 ScrollView 的 subview

swift - 在 UI 测试中将模拟数据插入核心数据

ios - Unity3D WWW 在 iOS 的主线程上调用