我创建了一个简单的应用程序来跟踪用户位置并在每次位置更新时创建本地通知。
我启用了下面的背景模式,
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
设置字符串并请求许可。首次加载应用程序时,用户授予始终使用权限。
当应用程序在前台时运行良好,当它进入后台时仍在运行至少在 5-40 分钟的时间范围内是可变的 通过电池或其他打开的应用程序。
问题是为什么它停止工作,难道它不应该继续工作吗?
我从未在 Apple 文档中看到过时间限制。
最佳答案
当应用移至后台时,切换到重要的位置更新。如果应用程序无限期地在后台保持事件状态,iOS 将卸载该应用程序。
locationManager.pausesLocationUpdatesAutomatically = false
关于ios - 当应用程序处于后台时,位置跟踪会在一段时间后停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48008221/