【问题标题】:can we get location updates in IOS swift in a regular intervel of time even when app is not running in foreground即使应用程序没有在前台运行,我们能否定期在 IOS swift 中获取位置更新
【发布时间】:2017-11-16 05:25:27
【问题描述】:

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

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

我需要的是应用程序应定期检查用户位置,当该位置大于 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 构建相同的功能实际上是小菜一碟。

任何帮助???

【问题讨论】:

    标签: ios iphone background location


    【解决方案1】:

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

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

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-18
      • 1970-01-01
      • 2019-04-18
      • 2016-04-15
      • 1970-01-01
      • 1970-01-01
      • 2015-02-28
      相关资源
      最近更新 更多