iOS:应用程序挂起状态下的位置更新

标签 ios swift gps core-location cllocationmanager

当应用程序处于暂停/终止状态时是否可以更新位置。

当应用程序处于挂起/终止状态时,我想每 500 米更新一次位置。

提前致谢。

最佳答案

终止后,获取更新的唯一真正方法是使用区域监控,但您只会获得主要更新,因此我怀疑它能否在 500 米范围内工作。

来自文档:

If your app is terminated either by a user or by the system, the system doesn't automatically restart your app when new location updates arrive. A user must explicitly relaunch your app before the delivery of location updates resumes. The only way to have your app relaunched automatically is to use region monitoring or significant-change location service. However, when a user disables the Background App Refresh setting either globally or specifically for your app, the system doesn't relaunch your app for any location events, including significant change or region monitoring events. Further, while Background App Refresh is off your app won't receive significant change or region monitoring events even when it's in the foreground.

这是有充分理由的。首先,人们不希望应用程序一直“窥探”他们,即使有充分的理由,想象一下有 10 个应用程序不断地这样做。这将开始导致您的性能和电池生命周期问题。

编辑 很多人似乎对这个问题感到困惑,所以我将尝试澄清一些事情。

您的应用程序不应在终止(或真正暂停)时持续跟踪位置。只是出于隐私方面的原因,您不能这样做,这样您就不会过度消耗电池。

当您的应用位于前台时,您可以通过 startUpdatingLocation 使用位置服务来监控设备位置。假设应用程序已获得许可,这将使用所有可用的硬件(GPS、WiFi、蜂窝网络)。

现在从文档本身来看:

If you start this service and your app is suspended, the system stops the delivery of events until your app starts running again (either in the foreground or background). If your app is terminated, the delivery of new location events stops altogether. Therefore, if your app needs to receive location events while in the background, it must include the UIBackgroundModes key (with the location value) in its Info.plist file.

因此,一旦暂停和终止,您的选择实际上是监视区域并监视重大位置变化。这些都不是特别准确或频繁。原因之一是他们只使用低功耗方法来获取位置(WiFi 和蜂窝网络),而不使用 GPS。

因此,当应用程序暂停或终止时,无法进行准确和/或频繁的位置跟踪。您必须接受这一点,并且您需要相应地设计您的应用程序。

关于iOS:应用程序挂起状态下的位置更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48021503/

相关文章:

android - Android 中更快的 GPS 定位

android - Android 正在读取 SQLite 中的 iOS 日期(NSDate)

ios - 当键盘打开时,为什么我的 View 会重置其位置?

objective-c - 我的应用程序未解析 JSON 响应

swift - 更新约束时如何避免 UICollectionView 中的淡入淡出动画

ios - Swift Rest 客户端连接速度缓慢

ios - CLLocationManager.requestLocation() 大约需要 10 秒

安卓室内增强现实

ios - iBeacon 扫描响应格式

ios - Crashlytics 记录错误已删除?