I’ve an app which run in background, and I want to stop all location services 30 min after entering into background mode.
So in my background function, I do this :
// 1800 sec = 30 min * 60 sec.
NSDate *date30min = [[NSDate alloc] initWithTimeIntervalSinceNow:1800.0];
NSLog(@"Date30min : %@", date30min);
self.timer = [[NSTimer alloc] initWithFireDate:date30min interval:1 target:self selector:@selector(stopLocation) userInfo:nil repeats:NO];
And my stopLocation function is :
- (void)stopLocation
{
NSLog(@"[My APP] [PASS INTO stopLocation]");
[self.locationManager stopMonitoringSignificantLocationChanges];
[self.locationManager stopUpdatingLocation];
[self.locationManager stopUpdatingHeading];
}
But my timer never call the function, what’s my error please ?
(My function is correctly implement into my .h and .m file, I tested this out of the background function.
Please help..
It’s not a problem with forgetting to add your timer to a run loop (in this case).
NSTimerobjects don’t fire when your app goes into the background. So, I would use another technique if you want to do something 30 minutes after backgrounding.For example, use a Background Task to run your
stopLocationmethod later:Of course, you also need to have declared this:
in your Info.plist file, but I assume you already figured that out.