2017-09-08 38 views
1

我想在应用终止时获取用户位置更新。位置应用终止时更新

我试了一下: 在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它应该是可能的

+0

你打开了背景位置获取? – vivek

+0

如果您的意思是在项目设置 - >功能 - >背景模式位置更新 –

+0

[如何获取iOS 7和8的位置更新(即使应用程序暂停)](https://stackoverflow.com/questions/ 27742677/how-to-get-location-updates-for-ios-7-and-8-even-when-the-app-is-suspended) –

回答

0

您是否尝试过在这里启用背景获取? enter image description here

+0

在哪里可以找到这个窗口? –

+0

这是Edit Scheme。所以,在Xcode的左上角你会看到目标,从那里你可以从下拉菜单中选择编辑方案 – prabodhprakash

相关问题