1

I'm trying to keep my iOS app running forever in the background, (or location sampling and diagnostic about the location) and i found this code -

 [self.locationManager stopUpdatingLocation];
 self.timer = [NSTimer scheduledTimerWithTimeInterval:self.currentTimerTime target:self selector:@selector(checkLocation) userInfo:nil repeats:NO];
 [[NSRunLoop currentRunLoop]addTimer:self.timer forMode:NSRunLoopCommonModes];
 [[UIApplication sharedApplication]beginBackgroundTaskWithExpirationHandler:nil];

This is working great but i have a feeling that apple would not like that, is that the best practice?

ozd
  • 1,194
  • 10
  • 34

1 Answers1

0

As you've probably discovered while this works in development it will not work in production. Apps get beginBackgroundTaskWithExpirationHandler get a limited amount of time to execute, unlimited in development but normally 60 seconds in released apps, at which point the expiration handler (which you are not assigning in your sample code) is called, and your app is expected to end the background task using the ID returned by the beginBackgroundTaskWithExpirationHandler call (which you are not capturing). Failure to do so will result in your app being terminated. For more information see this related question / answer.

objective c - Proper use of beginBackgroundTaskWithExpirationHandler

Community
  • 1
  • 1
BadPirate
  • 25,802
  • 10
  • 92
  • 123