如何在 iOS 7 中超过 180 秒在后台运行 NSTimer? [英] How to run NSTimer in background beyond 180sec in iOS 7?

查看:17
本文介绍了如何在 iOS 7 中超过 180 秒在后台运行 NSTimer?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试过这个,但在 iOS 7 和 Xcode 4.6.2 中工作时间不超过 180 秒.请帮帮我

I have tried this but not working more than 180 sec in iOS 7 and Xcode 4.6.2. Please help me

    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");
    }

推荐答案

除非您启用其中一种 Background 模式,否则它将无法工作.

Unless you enable one of the Background modes, it is not gonna work.

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

  • You have around 10 minutes of background execution after this the timer is stopped by ios.

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

The timer will not fire after app is locked (iOS7), since ios suspends the foreground app and bgTask will not get fire again.

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

There is some workarounds, consider to check below question:

iphone - 后台的 NSTimers

当应用在后台时调度 NSTimer?

NSTimer 在后台运行

这篇关于如何在 iOS 7 中超过 180 秒在后台运行 NSTimer?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆