问题
Hello everyone I need to call a method when the app is in background. This is now i am trying to achieve this but the timer is never called.
- (void)applicationDidEnterBackground:(UIApplication *)application
{
UIApplication* app = [UIApplication sharedApplication];
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
[app endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
// Start the long-running task and return immediately.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// Do the work associated with the task.
self.timer = nil;
[self initTimer];
});
}
- (void)initTimer {
DebugLog(@"timer INIT!!!!!");
if (self.timer == nil) {
self.timer = [NSTimer scheduledTimerWithTimeInterval:0.3
target:self
selector:@selector(checkUpdates:)
userInfo:nil
repeats:YES];
}
}
- (void)checkUpdates:(NSTimer *)timer{
[UIApplication sharedApplication].applicationIconBadgeNumber++;
DebugLog(@"timer FIRED!!!!!");
[self initTimer];
}
I need to call the method checkUpdates
every Nth minute or seconds while the app is in background.Please note that initTimer
is called when the app enters background but the timer is never called.
回答1:
Apple will not allow the use the application in background if you want to use the application in background you have to use the 'UIBackgroundModes'.
and after that from Appdeligate you can manage this thread
you should check
http://developer.apple.com/library/ios/#documentation/General/Reference/InfoPlistKeyReference/Articles/iPhoneOSKeys.html
hope this will help you
来源:https://stackoverflow.com/questions/16709869/how-to-call-a-method-every-nth-minute-while-app-is-in-background