ios - 即使应用程序未在前台运行,我们能否定期在 IOS swift 中获取位置更新

标签 ios iphone background location

即使应用没有在前台运行,我也想定期获取用户的位置更新。

在我看过的大部分文章中,他们都说操作系统会强制停止或暂停应用程序已启动的后台服务。

我需要的是该应用应定期检查用户位置,当该位置大于 10 公里时,该应用应触发本地通知。

当用户再次运行该应用时,我们能够正确执行该功能。但它不会在后台工作。如果我们将其集成为后台服务,则会导致应用程序崩溃。 :(

经过长时间的搜索,这部分引起了我的注意

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

据我所知,他们说操作系统会在感知到位置变化时唤醒应用程序以获取当前位置。

但我没有得到任何帮助教程来完成同样的任务

这些是我引用的教程

Periodic iOS background location updates

How do I get a background location update every n minutes in my iOS application?

Getting user location every n minutes after app goes to background

iOS Not the typical background location tracking timer issue

iOS long-running background timer with "location" background mode

我需要的最好是后台服务或完成工作的东西,检查当前位置计算距离并发出本地通知。

但据我所知,在应用程序退出后台后的一段时间内,所有后台服务都会被操作系统暂停或终止。

伙计们,我很绝望,我已经 2 周了。

为 Android 构建相同的内容实际上是小菜一碟。

有什么帮助吗???

最佳答案

您可以轻松获得后台位置更新,您需要在功能的“后台模式”部分下启用“位置更新”。

还需要申请Always Authorization,最后加上这个

if #available(iOS 9.0, *) {
        locationManager.allowsBackgroundLocationUpdates = true
}

确保“位置更新”已激活,否则上述代码将导致崩溃。

此外,在您的 AppDelegate 类中编写 CLLocationManager 委托(delegate)方法,因为这会增加调用这些方法的机会,如 Raywenderlich 背景定位教程中所述

关于ios - 即使应用程序未在前台运行,我们能否定期在 IOS swift 中获取位置更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44538337/

相关文章:

ios - 更改 Coreplot 图形的刻度标签

iphone - performSelector之后需要调用NSAutoReleasePool :withObject:afterDelay?

iphone - 在不是 ICarousel 中的 subview 的标签中显示 iCarousel 的当前索引

jquery - 为什么我的选项卡区域底部有空格?

ios - 如何向 ios 警报添加操作

ios - dispatch_after 会阻塞 UI 吗?

iOS 如何检查 ABRecordRef ABAddressBook、ABPerson 是否启用了 WhatsApp?

iphone - 将按钮添加到 ui 导航 Controller 底部栏

android - 如何在Android中设置键盘背景

java - JTree - 如何将背景设置为节点