Working with Location Timers in iOS 6


As you probably know, iOS 6 changed the method by which the NSLocation is updated. Instead of “updatedFromLocation:()oldLocation toNewLocation()NewLocation” There is now a simple method didUpdateLocations().

Besides setting location accuracy filter, distance filter, and heading filter, you need to invoke the method to do this. You should really make sure you’re using a Location Singleton if you’re using anything with NSLocation. It’s worth it to not have to worry about multiple instances of location and memory leaks.

The real trick with NSLocationManager is to get it to run on a timer. The solution is relatively simple, once you recognize that you can’t use the timestamp of the location that is passed to the method didUpdateLocations. Instead, make a function in your callback (connection:didReceiveData) and save a time stamp from the system time (make sure you either save it as a global or a NSUserDefault so you can access it in the first method. Then before the data is first sent to the server, place a conditional around the code that makes your server call. Then all you have to do is compare the current time stamp when the didUpdateLocations method is called to the one when the response came the last time. If the difference (NSTimeInterval) is more than 30 seconds (or whatever time limit you prescribe) then you’ll send another request. This will cut down on the number of requests to the sever and save on battery life with iOS 6.

What Are Your Thoughts?

Your email address will not be published. Required fields are marked *