应用程序终止时的位置更新

时间:2017-09-08 07:44:05

标签: ios swift ios10

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

我尝试了什么: 在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应该可以

1 个答案:

答案 0 :(得分:0)

您是否尝试在此处启用后台获取功能? enter image description here

相关问题