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
If you have the UIBackgroundModes
in your plist with location
key then you don't need to use beginBackgroundTaskWithExpirationHandler
method. That's redundant. Also you're using it incorrectly (see here) but that's moot since your plist is set.
With UIBackgroundModes location
in the plist the app will continue to run in the background indefinitely only as long as CLLocationManger
is running. If you call stopUpdatingLocation
while in the background then the app will stop and won't start again.
Maybe you could call beginBackgroundTaskWithExpirationHandler
just before calling stopUpdatingLocation
and then after calling startUpdatingLocation
you could call the endBackgroundTask
to keep it backgrounded while the GPS is stopped, but I've never tried that - it's just an idea.
Another option (which I haven't tried) is to keep the location manager running while in the background but once you get an accurate location change the desiredAccuracy
property to 1000m or higher to allow the GPS chip to get turned off (to save battery). Then 10 minutes later when you need another location update, change the desiredAccuracy
back to 100m to turn on the GPS until you get an accurate location, repeat.
When you call startUpdatingLocation
on the location manager you must give it time to get a position. You should not immediately call stopUpdatingLocation
. We let it run for a maximum of 10 seconds or until we get a non-cached high accuracy location.
You need to filter out cached locations and check the accuracy of the location you get to make sure it meets your minimum required accuracy (see here). The first update you get may be 10 mins or 10 days old. The first accuracy you get may be 3000m.
Consider using the significant location change APIs instead. Once you get the significant change notification, you could start CLLocationManager
for a few seconds to get a high accuracy position. I'm not certain, I've never used the significant location change services.