如何在iOS 7中超过180秒后运行NSTimer?
我已经尝试过,但在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
,否则不会奏效。
为什么?
-
在这个计时器被ios停止之后,你有大约10分钟的后台执行。
-
定时器不会在应用程序被locking(iOS7)后触发,因为ios挂起前台应用程序,
bgTask
不会再被触发。
有一些解决方法,考虑检查下面的问题:
iphone – NSTimers在背景中
计划NSTimer当应用程序在后台?
NSTimer在背景上工作