Questions in this vein are asked a lot on this site, but they're all filled with confusion or out of date answers, and I was wondering if anyone could give me a definitive answer as to how to solve the problem in my case? I'd like to update the location in the background somewhat frequently, but turn off location services when uneeded so that power is saved. So I have periodic background updates working as such
func applicationWillResignActive(application: UIApplication) {
NSNotificationCenter.defaultCenter().postNotificationName(BeginBackGroundMode, object:self)
}
This function this notification calls ncludes this -
locationManager.pausesLocationUpdatesAutomatically = false
timer = NSTimer.scheduledTimerWithTimeInterval(10.0, target: self, selector: Selector("onTick"), userInfo: nil, repeats: true)
so this is repeated in the background
func onTick(){
println("ticking")
println("\(Coordinates!.latitude)")
println( "\(Coordinates!.longitude)")
self.locationManager.allowDeferredLocationUpdatesUntilTraveled(CLLocationDistanceMax, timeout: 4)
}
func locationManager(manager: CLLocationManager!,didFinishDeferredUpdatesWithError error: NSError!) {
locationManager.startUpdatingLocation()
}
This works, but I feel like I might not be going about it the right way, so any improvements in code elegance or understanding would be awesome. As for my question, where in the process should I turn location on and off to save battery? Or are there other way's you'd reccomend saving battery? (I need max accuracy). Thanks in advance