当应用程序被用户杀死时如何重新启动位置更新?

How to restart the location update when the app is killed by user?

我正在尝试获取用户位置,即使应用程序在后台也是如此。

该应用程序也始终具有位置权限。

该应用程序在后台运行良好,但是当用户关闭该应用程序时它无法运行。

我正在使用此 startMonitoringSignificantLocationChanges 来获取位置

locationManager?.startMonitoringSignificantLocationChanges()
locationManager?.allowsBackgroundLocationUpdates = true

但是在 phone 重启后,即使应用程序不是 运行,位置更新也有效。 只有当应用程序被用户在任务中杀死时,它才不起作用。

func applicationDidEnterBackground(_ application: UIApplication) {
        Log.debug("Did Enter Background")
        self.locationManager?.stopMonitoringSignificantLocationChanges()
        self.locationManager           = CLLocationManager()
        self.locationManager?.delegate = self
        self.locationManager?.requestAlwaysAuthorization()
        self.locationManager?.desiredAccuracy = kCLLocationAccuracyBest
        self.locationManager?.distanceFilter = 500
        self.locationManager?.activityType = CLActivityType.otherNavigation
        self.locationManager!.allowsBackgroundLocationUpdates = true
        self.locationManager?.pausesLocationUpdatesAutomatically = false
        self.locationManager?.startMonitoringSignificantLocationChanges()
}

我刚刚使用 allowDeferredLocationUpdates

解决了这个问题
func applicationWillTerminate(_ application: UIApplication) {
        Log.debug("Will Terminate")
        
        self.locationManager?.stopMonitoringSignificantLocationChanges()
        self.locationManager?.allowDeferredLocationUpdates(untilTraveled: 5, timeout: 60000)
        self.locationManager?.startMonitoringSignificantLocationChanges()

}