当应用程序在后台时,如何每 n 分钟调用一个方法? [英] How to call a method every nth minute while app is in background?

查看:73
本文介绍了当应用程序在后台时,如何每 n 分钟调用一个方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,当应用程序在后台时,我需要调用一个方法.现在我正在尝试实现这一点,但从未调用过计时器.

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];
}

当应用程序在后台时,我需要每隔 N 分钟或秒调用方法 checkUpdates.请注意 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.

推荐答案

Apple 不允许在后台使用该应用程序,如果您想在后台使用该应用程序,则必须使用UIBackgroundModes".

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'.

然后从Appdeligate你可以管理这个线程

and after that from Appdeligate you can manage this thread

你应该检查

http://developer.apple.com/library/ios/#documentation/General/Reference/InfoPlistKeyReference/Articles/iPhoneOSKeys.html

希望对你有帮助

这篇关于当应用程序在后台时,如何每 n 分钟调用一个方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆