2012-06-03 16 views
2

关闭后,我有一个要数长达8小时(28800秒) 之后,它应该被释放的NSTimer继续计数的应用程序在iPhone

即时知道如何保持计时器在后台运行的定时器和/或应用程序关闭了吗?

这是的NSTimer:

stopWatchTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 
                 target:self 
                selector:@selector(updateTimer) 
                userInfo:nil 
                repeats:YES]; 

,这是我的情况:

counter++; 

if (counter >= 28800) { 
    [stopWatchTimer invalidate]; 
    counter =0; 

    timeLabel.text = @"Time Out"; 
} 
+0

也许你可以找到答案,在这个线程 http://stackoverflow.com/questions/4154332/ios4-create-background-timer –

+0

如何使用它? – Mutawe

回答

2

当应用程序进入的背景下,在- (无效)applicationDidEnterBackground:应用委托方法在nsuserdefault添加当前计数器值和当前时间。

现在,当应用程序之前变得活跃 - (空)applicationWillEnterForeground:将调用,所以该方法获得总秒数应用在后台即(应用程序的当前时间) - (时间申请去该存储的背景在nsuserdefault)计算秒

所以加这也是在 - (空)applicationWillEnterForeground:

if((seconds calculated) > (28800 - (current counter value stored in nsuserdefault))) 
{ 
    // stop timer as it has gone beyond eight hours 
} 
else 
{ 
    // continue task 
} 
+0

好吧,但是我去背景的时间和我回来的时间之间会有时差 – Mutawe

+0

您了解我描述的逻辑,如果不询问任何查询 –

相关问题