【问题标题】:How to run NSTimer in background beyond 180sec in iOS 7?如何在 iOS 7 中超过 180 秒的后台运行 NSTimer?
【发布时间】: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");
    }

【问题讨论】:

  • @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 设备上运行良好

标签: ios timer nstimer


【解决方案1】:

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

为什么?

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

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

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

iphone - NSTimers in background

Scheduled NSTimer when app is in background?

NSTimer on background IS working

【讨论】:

    猜你喜欢
    • 2014-05-21
    • 2013-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-08
    • 2012-03-02
    • 2015-12-03
    • 1970-01-01
    相关资源
    最近更新 更多