【发布时间】:2017-05-30 18:33:03
【问题描述】:
我正在尝试在我的应用程序暂停时接收位置更新 - 根据我阅读的文档,这应该不是问题,我正在执行所有必需的步骤,但是一旦应用程序暂停,位置似乎没有更新。
我看过类似的帖子,其中包含涉及使用 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
}()
“您可以从 Xcode 项目的 Capabilities 选项卡的 Background 模式部分启用位置支持。(您也可以通过在应用程序的 Info.plist 文件中包含带有位置值的 UIBackgroundModes 键来启用此支持。)启用此模式不会阻止系统暂停应用程序,但它会告诉系统每当有新的位置数据要传递时它应该唤醒应用程序。因此,此键有效地让应用程序在后台运行以处理位置随时更新。”
“当应用处于前台、后台运行或挂起时,系统会向后台位置应用传递位置更新。在应用被挂起的情况下,系统会唤醒应用,传递更新到位置管理器的委托,然后尽快将应用返回到暂停状态。”
【问题讨论】:
标签: swift background location cllocationmanager