1

I'm trying to track location using locationManager in the background every "time interval" when the app is in the background.

Its working great on iOS 8 iPhone 6 - but i keep getting terminated after 2.5 - 3 minutes, by the iOS in iOS 9 iPhone 6s.

My main question is - what can be the difference between iPhone 6 iOS 8 and 6s iOS 9? better watchdog timer for background tasks? if so how can i workaround that?

I sow solutions that includes decrease of accuracy to save battery life instead of time intervals, I don't want that because the user is seeing the location monitoring indication all the time(full arrow).

some of my code -

-(void)initialize
{
    self.locationManager  = [[CLLocationManager alloc]init];
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
    self.locationManager.distanceFilter = 5;
    self.locationManager.delegate = self;

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(addMonitorsBeforeWillTerminate)
                                                 name:UIApplicationWillTerminateNotification
                                               object:nil];

    [[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidEnterBackgroundNotification object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) {
        [[UIApplication sharedApplication]beginBackgroundTaskWithExpirationHandler:nil];
    }];
      [self.locationManager startUpdatingLocation];
    }

thanks

casillas
  • 16,351
  • 19
  • 115
  • 215
ozd
  • 1,194
  • 10
  • 34
  • 1
    Find my answer here, it may be helpful. http://stackoverflow.com/questions/34512007/periodic-sending-small-data-to-server-in-background-mode/34512750#34512750 – technerd Dec 30 '15 at 17:24
  • You've saved my life, thank you!! – ozd Dec 31 '15 at 06:19

3 Answers3

0
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)


 if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"9.0")){


    [self.locationManager setAllowsBackgroundLocationUpdates:YES];

}

More depth and breadth information could be found in the following link.

Community
  • 1
  • 1
casillas
  • 16,351
  • 19
  • 115
  • 215
0

Two options I recommend.

  1. When in background switch to kCLLocationAccuracyThreeKilometers. That will switch off GPS and won't drain the battery because it will use cell information only. Request permission for background location services and create an NSTimer callback (this will keep your app running in the BG). Inside the callback switch to the desired accuracy, make the update of the location then switch back to low accuracy again.
  2. Sign up for significant location changes. With that however your app won't be notified every X minutes.
Teddy
  • 1,056
  • 1
  • 14
  • 24
  • kCLLocationAccuracyThreeKilometers is keeping the full arrow i want to avoid. did you able to do it without the arrow? also what do you mean "NSTimer callback" - [NSTimer scheduledTimerWithTimeInterval:15 target:self selector:@selector(timerFired) userInfo:nil repeats:NO]; - or something else? – ozd Dec 30 '15 at 18:36
  • It will keep the full arrow because it is running location services. Concerning NSTimer that is exactly what I meant. But running it every 15 secs does not make too much sense with my methodology. – Teddy Dec 31 '15 at 06:28
0

For however struggling with that in the future, the answer came from "technerd" by comment so i turned it in to an answer -

https://github.com/voyage11/Location

ozd
  • 1,194
  • 10
  • 34