iOSNSTimer 后台运行

使用NSTimer的时候,发现每次APP进入后台,或者屏幕休眠后,NSTimer就会暂停。为了解决这个问题,翻阅了各种博客和网页。最终在伟大的stackoverflow上找到了一个简单并且真正可行的解决方案!代码如下:

[[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:nil];  这句话重点,加这行代码可以让程序在后台运行一段时间

    self.timer = [NSTimerscheduledTimerWithTimeInterval:1 target:selfselector:@selector(timeFireMethod)userInfo:nilrepeats:YES];

    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];

原文地址:https://www.cnblogs.com/LynnAIQ/p/6228336.html