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

Posted

技术标签:

【中文标题】如何在 iOS 7 中超过 180 秒的后台运行 NSTimer?【英文标题】:How to run NSTimer in background beyond 180sec in iOS 7? 【发布时间】:2014-05-02 23:05:10 【问题描述】:

我已经尝试过,但在 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");
    

【问题讨论】:

签出这个:***.com/a/9623490/905514 @Arvind-Systematix 实际上我有 xcode 4.6.2 版本。这个解决方案可以在那个 Xcode 版本上工作吗? @Arvind-Systematix 我需要 IOS 7 和 xcode 4.6.2 版本的这个功能。 您是如何将 IOS 7 与 Xcode 4.6 一起使用的,因为它是在 XCode 5 中引入的? en.wikipedia.org/wiki/Xcode。使用 IOS 7 时,您可以在后台打开/关闭刷新应用程序。 我同意你的观点,但我的项目是在 Xcode 4.6.2 中开发的,它在 iOS 6 设备上运行良好,但在 iOS7 设备上运行良好 【参考方案1】:

除非您启用Background modes 之一,否则它不会起作用。

为什么?

在此之后,您有大约 10 分钟的后台执行时间,计时器被 ios 停止。

应用程序锁定后计时器不会触发 (iOS7),因为 ios 会暂停前台应用程序,bgTask 不会再次触发。

有一些解决方法,请考虑检查以下问题:

iphone - NSTimers in background

Scheduled NSTimer when app is in background?

NSTimer on background IS working

【讨论】:

以上是关于如何在 iOS 7 中超过 180 秒的后台运行 NSTimer?的主要内容,如果未能解决你的问题,请参考以下文章

致命错误:在 XAMPP 中超过了 30 秒的最大执行时间

致命错误:[关闭]中超过了 30 秒的最大执行时间

如何每小时在后台运行约 30 秒的进程(iphone 应用程序)

致命错误:第 306 行的 /var/www/html/central/system/database/drivers/mysqli/mysqli_driver.php 中超过了 30 秒的最大执行时

为啥运行我的 js resizer 代码会使文档在 IE7 - IE11 和 MS Edge 中超过 100% 的窗口高度和宽度?

保持程序在后台长时间运行-b