我想在應用終止時獲取用戶位置更新。位置應用終止時更新
我試了一下: 在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它應該是可能的
你打開了背景位置獲取? – vivek
如果您的意思是在項目設置 - >功能 - >背景模式位置更新 –
[如何獲取iOS 7和8的位置更新(即使應用程序暫停)](https://stackoverflow.com/questions/ 27742677/how-to-get-location-updates-for-ios-7-and-8-even-when-the-app-is-suspended) –