ios - 如何在 iOS 7 中超过 180 秒的后台运行 NSTimer?

标签 ios timer nstimer

我已经试过了,但在 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

Scheduled NSTimer when app is in background?

NSTimer on background IS working

关于ios - 如何在 iOS 7 中超过 180 秒的后台运行 NSTimer?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22628922/

相关文章:

iphone - objective-c : Timer crashes app when turned on a second time

ios - 尽管实现正确,但无法在设备上接收推送通知

ios - 如何创建一个新的文本文件以写入 objective c ipad app

opengl - 如何在顶点着色器中使用计时器在 OpenGL 中设置点大小的动画

c# - 实现永无止境的任务的正确方法。 (计时器与任务)

ios - 如何为 UIButton 的操作设置每小时限制?

ios - 如何删除警告 "Conditional cast from ' 任何'到 'AnyObject' 总是成功"

ios - 使用 Asset Catalog 减小应用程序文件大小的方法

java - 如何在不阻塞 UI 的情况下暂停 Java 指定时间?

swift - @selector() 在 Swift 中?