How to call a method every nth minute while app is in background?

旧街凉风 提交于 2019-12-11 19:44:47

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!