iOS Dev Notes

iOS Development From The Frontlines

Core Location Redux: Adding an NSTimer

with 5 comments

Yesterday, I posted a tutorial on getting started with Core Location. Be sure to check it out if you haven’t already.

One of the comments we received pointed out that it would be a good idea to also have an NSTimer running to stop the Core Location updates should we never receive a location with a good-enough accuracy. This prevents the case where the device keeps Location Services powered up (such as the GPS and radio) and drawing power. Let’s take a look at how to implement this timer:

First, setup an NSTimer class variable in the same class where you’re using the CLLocationManager:

@property (strong, nonatomic) NSTimer *locationTimer;

Be sure to include the corresponding @synthesize. Next, let’s implement a method, called stopUpdatingLocations, to be called when the NSTimer determines a preset interval of time has elapsed:

- (void)stopUpdatingLocations { [locationManager stopUpdatingLocation]; [locationTimer invalidate]; }

This method does two things. It first sends a message to the locationManager to stop updating location information. Note that it’s okay to send multiple stopUpdatingLocation messages to a CLLocationManager, such as the case where we tell it to stop updating after we’ve received a location that’s “good-enough”, and then the timer fires.

Lastly, this method invalidates the NSTimer object as we no longer need it.

One last note: we also want to stop this timer should we unload our view (remember that we’re already stopping Core Location updates in our viewDidUnload method). So, let’s add the following there as well:

[locationTimer invalidate];

Starting the Timer

Great, now let’s kick off the timer. In our viewDidLoad method, we’re going to add the following to the end (just after startUpdatingLocation):

self.locationTimer = [NSTimer scheduledTimerWithTimeInterval:30.0 target:self selector:@selector(stopUpdatingLocations) userInfo:nil repeats:NO];

This sets up a new NSTimer, saves it to the locationTimer class variable, and schedules it with a 30 second timeout (I picked this arbitrarily – you’ll want to decide for yourself how long you’re willing to wait for a location update). When the timer fires, it will call our stopUpdatingLocations method which we already implemented above.

Hope you find this helpful. If you’re going to be implementing Core Location services in your own apps, it’s best to think through every possible edge case to ensure your app is rock-solid. Be sure to post with any other questions or comments you might have.


Looking to add push notifications to your iOS app? Check out PushLayer, a service from the authors of iOSDevNotes.

Bookmark and Share

Subscribe for updates

Fill out the form below to join my newsletter. I'll also send you a bonus copy of my ebook, "5 Steps To Becoming a Better iOS Developer".

Written by rtwomey

October 27th, 2011 at 11:11 am

5 Responses to 'Core Location Redux: Adding an NSTimer'

Subscribe to comments with RSS or TrackBack to 'Core Location Redux: Adding an NSTimer'.

  1. [...] location, which would thus leave Core Location running indefinitely. Check out the follow-up post which shows how to implement an NSTimer to stop Core Location after a pre-set tim…. #af-form-1503460406 .af-body .af-textWrap{width:98%;display:block;float:none;} [...]

  2. I am trying to run this code on device. It will work perfectly on Simulator but on device, it didn’t work. Can you help me in this regard.

    Saqib

    7 Feb 12 at 9:13 am

  3. [...] First, setup an NSTimer class variable in the same class where you’re using the CLLocationManager : @property ( strong, nonatomic ) NSTimer * locationTimer; One of the comments we received pointed out that it would be a good idea to also have an NSTimer running to stop the Core Location updates should we never receive a location with a good-enough accuracy. This prevents the case where the device keeps Location Services powered up (such as the GPS and radio) and drawing power. Let’s take a look at how to implement this timer: Core Location Redux: Adding an NSTimer [...]

  4. Can you please post some opensource code to let user use the timer in background with location manager updates.

    Ajay

    12 Jun 12 at 2:22 pm

  5. - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation*)newLocation fromLocation:(CLLocation *)oldLocation;

    the above method is always giving the iphone default location that is the location with latitude 37.785834 and longitude -122.406417 which is not my location. please help.

    Aj

    26 Jun 12 at 8:01 am

Leave a Reply