How do I prevent the iPhone screen from dimming or turning off while my application is running?

百般思念 提交于 2019-11-26 10:12:36

问题


I\'m working on an app that requires no user input, but I don\'t want the iPhone to enter the power saving mode.

Is it possible to disable power saving from an app?


回答1:


Objective-C

[[UIApplication sharedApplication] setIdleTimerDisabled:YES];

Swift

UIApplication.shared.isIdleTimerDisabled = true



回答2:


In swift you can use this as

UIApplication.sharedApplication().idleTimerDisabled = true



回答3:


Swift 3:

UIApplication.shared.isIdleTimerDisabled = true



回答4:


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.




回答5:


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.



来源:https://stackoverflow.com/questions/125619/how-do-i-prevent-the-iphone-screen-from-dimming-or-turning-off-while-my-applicat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!