即使应用程序没有在前台

时间:2017-06-14 07:33:55

标签: ios iphone background location

即使应用未在前台运行,我也希望定期获取用户的位置更新。

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

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

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

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

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

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

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

请伙计们,我是绝望的,我已经有两个星期了。

为Android构建相同实际上是件小事。

任何帮助???

1 个答案:

答案 0 :(得分:2)

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

您还需要申请永久授权,最后添加此

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

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

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