简体   繁体   English

屏幕锁定时的NSTimer

[英]NSTimer when screen is locked

How do I keep the NSTimer persisting after the screen is locked in iOS 8? 在iOS 8中锁定屏幕后,如何保持NSTimer持久? Every time I lock the screen it stops. 每次锁定屏幕时,它都会停止。 I have tried putting it in an NSRunLoop to no luck. 我试过将它放在NSRunLoop是没有运气的。

iOS always suspends NSTimers when the app is backgrounded. 当应用程序后台运行时,iOS始终挂起NSTimers。 You can request some extra background time by using UIApplication beginBackgroundTaskWithExpirationHandler: but be warned that it won't keep your timer going forever. 您可以使用UIApplication beginBackgroundTaskWithExpirationHandler:来请求一些额外的背景时间,但要注意,这不会使您的计时器永远运行下去。 Your best bet is to remember what time you are backgrounded in applicationWillResignActive and then check the current time when you are re-activated in applicationDidBecomeActive or applicationWillEnterForeground . 最好的选择是记住在applicationWillResignActive中后台运行的时间,然后检查在applicationDidBecomeActiveapplicationWillEnterForeground中重新激活时的当前时间。 Then you can calculate how much time passed when your app was inactive and do whatever is appropriate. 然后,您可以计算出应用程序处于非活动状态时经过的时间,并采取适当的措施。

See the UIApplication docs here https://developer.apple.com/library/ios/documentation/uikit/reference/UIApplication_Class/Reference/Reference.html , particularly the section on "Managing Background Execution". 请参阅https://developer.apple.com/library/ios/documentation/uikit/reference/UIApplication_Class/Reference/Reference.html ,请参见UIApplication文档,尤其是有关“管理后台执行”的部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM