Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I did this in an application I'm developing. The timers don't work when the app is in the background but the app is constantly receiving the location updates. I read somewhere in the documentation (i can't seem to find it now, i'll post an update when i do) that a method can be called only on an active run loop when the app is in the background. The app delegate has an active run loop even in the bg so you dont need to create your own to make this work. [Im not sure if this is the correct explanation but thats how I understood from what i read]</p> <p>First of all, add the <code>location</code> object for the key <code>UIBackgroundModes</code> in your app's info.plist. Now, what you need to do is start the location updates anywhere in your app:</p> <pre><code> CLLocationManager locationManager = [[CLLocationManager alloc] init]; locationManager.delegate = self;//or whatever class you have for managing location [locationManager startUpdatingLocation]; </code></pre> <p>Next, write a method to handle the location updates, say <code>-(void)didUpdateToLocation:(CLLocation*)location</code>, in the app delegate. Then implement the method <code>locationManager:didUpdateLocation:fromLocation</code> of <code>CLLocationManagerDelegate</code> in the class in which you started the location manager (since we set the location manager delegate to 'self'). Inside this method you need to check if the time interval after which you have to handle the location updates has elapsed. You can do this by saving the current time every time. If that time has elapsed, call the method UpdateLocation from your app delegate:</p> <pre><code>NSDate *newLocationTimestamp = newLocation.timestamp; NSDate *lastLocationUpdateTiemstamp; int locationUpdateInterval = 300;//5 mins NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; if (userDefaults) { lastLocationUpdateTiemstamp = [userDefaults objectForKey:kLastLocationUpdateTimestamp]; if (!([newLocationTimestamp timeIntervalSinceDate:lastLocationUpdateTiemstamp] &lt; locationUpdateInterval)) { //NSLog(@"New Location: %@", newLocation); [(AppDelegate*)[UIApplication sharedApplication].delegate didUpdateToLocation:newLocation]; [userDefaults setObject:newLocationTimestamp forKey:kLastLocationUpdateTimestamp]; } } } </code></pre> <p>This will call your method every 5 mins even when your app is in background. Imp: This implementation drains the battery, if your location data's accuracy is not critical you should use <code>[locationManager startMonitoringSignificantLocationChanges]</code></p> <p>Before adding this to your app, please read the <a href="http://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/LocationAwarenessPG/Introduction/Introduction.html" rel="noreferrer">Location Awareness Programming Guide</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload