I\'m writing an application that requires background location updates with high accuracy and low frequency. The solution seems to be a background NSTimer t
locationManager.allowsBackgroundLocationUpdates = true
It seems that stopUpdatingLocation
is what triggers the background watchdog timer, so I replaced it in didUpdateLocation
with:
[self.locationManager setDesiredAccuracy:kCLLocationAccuracyThreeKilometers];
[self.locationManager setDistanceFilter:99999];
which appears to effectively power down the GPS. The selector for the background NSTimer
then becomes:
- (void) changeAccuracy {
[self.locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[self.locationManager setDistanceFilter:kCLDistanceFilterNone];
}
All I'm doing is periodically toggling the accuracy to get a high-accuracy coordinate every few minutes and because the locationManager
hasn't been stopped, backgroundTimeRemaining
stays at its maximum value. This reduced battery consumption from ~10% per hour (with constant kCLLocationAccuracyBest
in the background) to ~2% per hour on my device.
When it is time to start location service and stop background task, background task should be stopped with a delay (1 second should be enough). Otherwise location service wont start. Also Location Service should be left ON for a couple of seconds (e.g. 3 seconds).
There is a cocoapod APScheduledLocationManager that allows to get background location updates every n seconds with desired location accuracy.
let manager = APScheduledLocationManager(delegate: self)
manager.startUpdatingLocation(interval: 170, acceptableLocationAccuracy: 100)
The repository also contains an example app written in Swift 3.