1

I am programming in Objective C on Xcode and seem to be having a problem when my app goes into the background. I have a GPS tracking app and it works fine when the user has the app running but when it goes in the background the GPS doesn't follow the user location step by step. It pauses and then when the app re-opens, it automatically finds the location but I want the GPS to be running all the time so it won't "go off road"1 like I have in the picture attached.

"GPS location goes off road

I have location updates and background fetch turned on already in background modes.

   #pragma mark - CLLocationManagerDelegate
   - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:          (NSArray *)locations
   {

if (!self.timerPause) {
    for (CLLocation *newLocation in locations) {

        NSDate *eventDate = newLocation.timestamp;

        NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];

        if (fabs(howRecent) < 10.0 && newLocation.horizontalAccuracy < 20) 

other code

     if (self.locationManager == nil) {
    self.locationManager = [[CLLocationManager alloc] init];
}
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.activityType = CLActivityTypeAutomotiveNavigation;
self.locationManager.distanceFilter = 10; // meters
if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
    [self.locationManager requestWhenInUseAuthorization];
}
[self.locationManager startUpdatingLocation];
Carlos Alan
  • 27
  • 11
  • What settings do you have on your `CLLocationManager`? in particular, activity type? Are you using deferred location updates? Have you set `allowsBackgroundLocationUpdates` to true? Are you processing all locations in `didUpdateLocations`? – Paulw11 Mar 17 '16 at 03:12
  • Have you set the activity type to `CLActivityTypeAutomotiveNavigation` ? – Paulw11 Mar 17 '16 at 03:48
  • @Paulw11 I updated my post with my code if you could take a look please. – Carlos Alan Mar 17 '16 at 03:59
  • Why do you throw away locations older than 10 seconds? Just because a location update is deferred doesn't mean that it wasn't a valid location that should be recorded – Paulw11 Mar 17 '16 at 04:04
  • Refer the link: [BackgroundLocationUpdates](http://stackoverflow.com/questions/30808192/allowsbackgroundlocationupdates-in-cllocationmanager-in-ios9) – Chetan Mar 17 '16 at 05:07

1 Answers1

3
locationManager.allowsBackgroundLocationUpdates = YES;

Add this where you have set up the location manager. It should work :)

Alakh
  • 118
  • 8