【发布时间】:2018-01-20 03:10:10
【问题描述】:
我创建了一个简单的应用程序,它可以跟踪用户位置并在每次更新位置时创建本地通知。
我启用了下面的背景模式,
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 文档中看到过时间限制。
【问题讨论】:
-
为了授权,你请求了
requestAlwaysAuthorization(),对吧? -
@AhmadF,是的“即使您不使用应用程序,也允许“应用程序”访问您的位置?”被提示。我已经允许了,毫无疑问,在 iPhone 隐私设置中,该应用程序的位置服务显示为“始终”。
-
@ocanal : 请参考这个网址stackoverflow.com/questions/6347503/…
-
@ocanal 您可能需要检查this answer 以确保您为您的目标设置了适当的键;我已经更新了我的答案,希望对您有所帮助:)
标签: ios swift core-location cllocationmanager