Note that there are some explanatory texts on larger screens.

plurals
  1. POPeriodic iOS background location updates
    text
    copied!<p>I'm writing an application that requires background location updates with <strong>high accuracy and low frequency</strong>. The solution seems to be a background NSTimer task that starts the location manager's updates, which then immediately shuts down. This question has been asked before:</p> <p><a href="https://stackoverflow.com/questions/6347503/how-do-i-get-a-background-location-update-every-n-minutes-in-my-ios-application">How do I get a background location update every n minutes in my iOS application?</a></p> <p><a href="https://stackoverflow.com/questions/10235203/getting-user-location-every-n-minutes-after-app-goes-to-background?rq=1">Getting user location every n minutes after app goes to background</a></p> <p><a href="https://stackoverflow.com/questions/13377750/ios-not-the-typical-background-location-tracking-timer-issue">iOS Not the typical background location tracking timer issue</a></p> <p><a href="https://stackoverflow.com/questions/15650139/ios-long-running-background-timer-with-location-background-mode">iOS long-running background timer with &quot;location&quot; background mode</a></p> <p><a href="https://stackoverflow.com/questions/10706265/ios-full-time-background-service-based-on-location-tracking">iOS full-time background-service based on location tracking</a></p> <p>but I have yet to get a <strong>minimum example</strong> working. After trying every permutation of the above accepted answers, I put together a starting point. Entering background:</p> <pre><code>- (void)applicationDidEnterBackground:(UIApplication *)application { self.bgTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{ NSLog(@"ending background task"); [[UIApplication sharedApplication] endBackgroundTask:self.bgTask]; self.bgTask = UIBackgroundTaskInvalid; }]; self.timer = [NSTimer scheduledTimerWithTimeInterval:60 target:self.locationManager selector:@selector(startUpdatingLocation) userInfo:nil repeats:YES]; } </code></pre> <p>and the delegate method:</p> <pre><code>- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { NSLog(@"%@", newLocation); NSLog(@"background time: %f", [UIApplication sharedApplication].backgroundTimeRemaining); [self.locationManager stopUpdatingLocation]; } </code></pre> <p>The current behavior is that the <code>backgroundTimeRemaining</code> decrements from 180 seconds to zero (while logging location), and then the expiration handler executes and no further location updates are generated. How do I modify the above code in order to receive <strong>periodic location updates in the background indefinitely</strong>?</p> <p><strong>Update</strong>: I'm targeting iOS 7 and there appears to be some evidence that background tasks behave differently:</p> <p><a href="https://stackoverflow.com/questions/18901583/start-location-manager-in-ios-7-from-background-task">Start Location Manager in iOS 7 from background task</a></p>
 

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