如何在iOS中以编程方式禁用/启用睡眠模式? [英] How to disable/enable the sleep mode programmatically in iOS?
问题描述
我有一个应用程序,它必须保持清醒状态直到倒计时结束,但是只要达到分配的睡眠时间,它就会进入睡眠模式".
I have an app that must stay awake until the end of a countdown but it will go into 'sleep mode' whenever it reaches the allocated time to sleep.
在我的应用中,我可以选择推迟睡眠,以便用户可以禁用/启用它.
In my app, I have the option to postpone sleep, so users can disable/enable it.
我该如何以编程方式进行?
How do I do it programmatically?
推荐答案
您可以按以下方式禁用空闲计时器;
You can disable the idle timer as follows;
在Objective-C中:
In Objective-C:
[UIApplication sharedApplication].idleTimerDisabled = YES;
在Swift中:
UIApplication.sharedApplication().idleTimerDisabled = true
在Swift 3.0&中Swift 4.0:
In Swift 3.0 & Swift 4.0:
UIApplication.shared.isIdleTimerDisabled = true
将其设置回NO
或false
以重新启用睡眠模式.
Set it back to NO
or false
to re-enable sleep mode.
例如,如果在离开视图之前需要它,则可以通过覆盖viewWillDisappear来重新设置它:
For example, if you need it until you leave the view you can set it back by overriding the viewWillDisappear:
override func viewWillDisappear(_ animated: Bool) {
UIApplication.shared.isIdleTimerDisabled = false
}
有关 UIApplication类.
这篇关于如何在iOS中以编程方式禁用/启用睡眠模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!