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

2024-03-28

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

除非您启用其中一项Background modes,这是行不通的。

Why?

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

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

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

iphone - NSTimers 在后台 https://stackoverflow.com/questions/8943214/iphone-nstimers-in-background?rq=1

当应用程序在后台时安排 NSTimer? https://stackoverflow.com/questions/8415870/scheduled-nstimer-when-app-is-in-background?lq=1

后台 NSTimer 正在工作 https://stackoverflow.com/questions/5187741/nstimer-on-background-is-working/17445679

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 iOS 7 中让 NSTimer 在后台运行超过 180 秒? 的相关文章

随机推荐