应用程序在后台运行一段时间后,位置跟踪会停止 [英] Location tracking stops after a while when app is in the background

查看:134
本文介绍了应用程序在后台运行一段时间后,位置跟踪会停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个简单的应用程序,可以跟踪用户位置,并在每次更新位置时创建本地通知。

I've created a simple app which tracks user location and creates local notification for every time location is updated.

我启用了下面的后台模式,

I enabled the background modes below,

let locationManager = CLLocationManager()

open override func viewDidLoad() {
       locationManager.delegate = self;
       locationManager.desiredAccuracy = kCLLocationAccuracyBest;
       locationManager.distanceFilter = 10
       locationManager.allowsBackgroundLocationUpdates = true
       locationManager.startUpdatingLocation()
}

open func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
       let notification = UILocalNotification()
       notification.alertBody = "location updated"
       notification.fireDate = Date()
       UIApplication.shared.scheduleLocalNotification(notification)
}

我为设置字符串NSLocationAlwaysUsageDescription 并请求许可。第一次加载应用时始终使用的用户授予权限。

I set string for NSLocationAlwaysUsageDescription and ask for permission. User grant permission for always usage when the app loaded first time.

当应用程序处于前台时,当它背景仍在工作时,它运行良好至少在5-40分钟的时间范围内,可以通过电池或其他打开的应用程序更改

It's working well when app is in the foreground, when it goes background still working at least in 5-40 minutes time range which is changeable by battery or other opened apps.

问题是它停止工作的原因,不是预期的继续工作?

The problem is why it stops working, doesn't it expected to be keep working?

我从未在Apple文档中看到时间限制。

I've never seen a time limit in Apple docs.

推荐答案

当应用程序移至后台时切换到重要的位置更新。如果应用程序无限期地在后台保持活动状态,iOS将卸载该应用程序。

Switch to significant location updates when the app moves to background. iOS will unload the app if it keep alive in the background indefinitely.

locationManager.pausesLocationUpdatesAutomatically = false

这篇关于应用程序在后台运行一段时间后,位置跟踪会停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆