ios 如何在我的应用程序运行时防止 iPhone 屏幕变暗或关闭?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/125619/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I prevent the iPhone screen from dimming or turning off while my application is running?
提问by lajos
I'm working on an app that requires no user input, but I don't want the iPhone to enter the power saving mode.
我正在开发一个不需要用户输入的应用程序,但我不希望 iPhone 进入省电模式。
Is it possible to disable power saving from an app?
是否可以禁用应用程序的省电功能?
回答by lajos
Objective-C
目标-C
[[UIApplication sharedApplication] setIdleTimerDisabled:YES];
Swift
迅速
UIApplication.shared.isIdleTimerDisabled = true
回答by Aby Mathew
In swiftyou can use this as
在swift你可以使用它作为
UIApplication.sharedApplication().idleTimerDisabled = true
回答by Charlie Seligman
Swift 3:
斯威夫特 3:
UIApplication.shared.isIdleTimerDisabled = true
回答by JMStudios.jrichardson
I have put this line of code in my view controller yet we still get customers saying the screen will dim or turn off until someone touches the screen. I have seen other posts where not only do you programatically set
我已经把这行代码放在我的视图控制器中,但我们仍然有客户说屏幕会变暗或关闭,直到有人触摸屏幕。我看过其他帖子,其中您不仅以编程方式设置
UIApplication.sharedApplication().idleTimerDisabled = true
to true but you must reset it to false first
为真,但您必须先将其重置为假
UIApplication.sharedApplication().idleTimerDisabled = false
UIApplication.sharedApplication().idleTimerDisabled = true
Sadly this still did not work and customers are still getting dimmed screens. We have Apple Configurator profile preventing the device from going to sleep, and still some devices screen go dim and the customer needs to press the home button to wake the screen. I now put this code into a timer that fires every 2.5 hours to reset the idle timer, hopefully this will work.
可悲的是,这仍然不起作用,客户的屏幕仍然变暗。我们有 Apple Configurator 配置文件阻止设备进入睡眠状态,但仍有一些设备屏幕变暗,客户需要按主页按钮唤醒屏幕。我现在将此代码放入一个计时器中,该计时器每 2.5 小时触发一次以重置空闲计时器,希望这会起作用。
回答by JMStudios.jrichardson
We were having the same issue. Turned out to be a rogue process on our MDM server that was deleted in our account but on the server was still sending the command to dim our devices.
我们遇到了同样的问题。结果是我们 MDM 服务器上的一个流氓进程在我们的帐户中被删除,但服务器上仍在发送命令来调暗我们的设备。