【发布时间】:2015-03-03 10:10:24
【问题描述】:
即使我的应用程序在后台运行,我也想在后台连续执行特定任务。
这是我尝试过的代码。定时器在进入后台时只触发一次。
- (void)applicationDidEnterBackground:(UIApplication *)application
{
NSTimer * timer = [NSTimer timerWithTimeInterval:2.0
target:self
selector:@selector(timerTicked)
userInfo:nil
repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:timer
forMode:NSDefaultRunLoopMode];
}
- (void) timerTicked
{
NSLog(@"Timer method");
}
【问题讨论】:
-
嘿,这篇文章似乎可以解决您的问题。 stackoverflow.com/questions/12916633/…
-
@LoVo 你解决了我的问题。谢谢你..
-
@Rakesh 不,不会。看看这个方法的App Doc。 developer.apple.com/library/ios/documentation/UIKit/Reference/…:你的做法违反了苹果的建议,这甚至不会超过几分钟
标签: ios iphone background nstimer