我希望在应用终止时获取用户位置更新。
我尝试了什么: 在AppDelegate中:
var locationManger: CLLocationManager!
在applicationDidFinishLaunching
中setupLocationManager()
还有AppDelegate:
@nonobjc func applicationWillTerminate(application: UIApplication) {
locationManger.startMonitoringSignificantLocationChanges()
}
func setupLocationManager(){
locationManger = CLLocationManager()
locationManger.delegate = self
locationManger.requestAlwaysAuthorization()
locationManger.desiredAccuracy = kCLLocationAccuracyHundredMeters
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if UIApplication.shared.applicationState == .inactive{
let notification = UNMutableNotificationContent()
notification.title = "Location Update"
notification.body = "updated in background"
let notificationTrigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
let request = UNNotificationRequest(identifier: "locationUpdate", content: notification, trigger: notificationTrigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
}
}
但我没有得到推送通知...... 有任何想法吗? 参考这篇文章:IOS Getting location updates when app terminated without using significantChange应该可以