我已经试过了,但在 iOS 7 和 Xcode 4.6.2 中工作时间不超过 180 秒。请帮助我
UIBackgroundTaskIdentifier bgTask = UIBackgroundTaskInvalid;
UIApplication *app = [UIApplication sharedApplication];
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
[app endBackgroundTask:bgTask];
}];
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:20 target:self selector:@selector(timerMethod) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
-(void) timerMethod{
NSLog(@"in timerMethod");
}
最佳答案
除非您启用其中一种后台模式
,否则它不会工作。
为什么?
在 ios 停止计时器之后,您有大约 10 分钟的后台执行时间。
应用程序锁定后(iOS7)计时器不会触发,因为 ios 暂停了前台应用程序并且
bgTask
不会再次触发。
有一些解决方法,考虑检查以下问题:
iphone - NSTimers in background
关于ios - 如何在 iOS 7 中超过 180 秒的后台运行 NSTimer?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22628922/