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

Dee*_*pta 10 timer nstimer ios

我试过这个但是在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");
    }
Run Code Online (Sandbox Code Playgroud)

nul*_*ull 11

除非你启用其中一个,否则Background modes它不会起作用.

为什么?

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

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

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

iphone - NSTimers在背景中

应用程序在后台时预定的NSTimer?

NSTimer在背景上工作