I'm trying to get the user location even if the app is in the background.
The application have also always location permission.
It is working well the app is in the background, but when the user kills the app it doesn't work.
I'm using this startMonitoringSignificantLocationChanges to get the location
locationManager?.startMonitoringSignificantLocationChanges()
locationManager?.allowsBackgroundLocationUpdates = true
But after the phone is restarted, even if app is not running, the location update works. Only when the app is killed by user on the task, it doesn't work.
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()
}
I just solved this by using allowDeferredLocationUpdates
func applicationWillTerminate(_ application: UIApplication) {
Log.debug("Will Terminate")
self.locationManager?.stopMonitoringSignificantLocationChanges()
self.locationManager?.allowDeferredLocationUpdates(untilTraveled: 5, timeout: 60000)
self.locationManager?.startMonitoringSignificantLocationChanges()
}