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

查看:98
本文介绍了如何在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");
    }


推荐答案

除非您启用其中一个背景模式,它不会起作用。

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 on background IS工作

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

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