使用NSTimer的時(shí)候,發(fā)現(xiàn)每次APP進(jìn)入后臺(tái),或者屏幕休眠后,NSTimer就會(huì)暫停。為了解決這個(gè)問題,翻閱了各種博客和網(wǎng)頁。最終在偉大的stackoverflow上找到了一個(gè)簡單并且真正可行的解決方案!代碼如下:
[[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:nil];
self.timer = [NSTimerscheduledTimerWithTimeInterval:1 target:selfselector:@selector(timeFireMethod)userInfo:nilrepeats:YES];
[[NSRunLoop currentRunLoop] addTimer:self.timerforMode:NSRunLoopCommonModes];
原問題鏈接:http://stackoverflow.com/questions/15092016/how-to-run-nstimer-in-background-and-sleep-in-iOS
問題回答者表示他也是從其他地方看到的這個(gè)解決方案。知道真正原po的可以留言