126

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?

Juan Boero
  • 6,281
  • 1
  • 44
  • 62
lajos
  • 25,525
  • 19
  • 65
  • 75

5 Answers5

185

Objective-C

[[UIApplication sharedApplication] setIdleTimerDisabled:YES];

Swift

UIApplication.shared.isIdleTimerDisabled = true
Valentin Shamardin
  • 3,569
  • 4
  • 34
  • 51
lajos
  • 25,525
  • 19
  • 65
  • 75
  • 2
    Does it matter where this line of code is used? Should it be in the AppDelegate or the App's main viewcontroller? –  Sep 22 '10 at 23:48
  • 17
    That depends on how you intend on using it. You may have only one view that you do not want to timeout in your application. In this case you'd use it in that view controller (when the view appears). To re-enable the timer you'd then use `[[UIApplication sharedApplication] setIdleTimerDisabled:NO];` when the view disappears. – Liam George Betsworth Apr 17 '12 at 14:48
  • 2
    Does the app need to re-enable the timer when the app has lost focus, or is this handled automatically by the OS? – Stan James Oct 21 '14 at 17:10
  • 5
    Stan - when the app resigns active, the OS will re-enable idle mode, you do **not** have to call `[[UIApplication sharedApplication] setIdleTimerDisabled:NO];` – Chris Stringer Apr 10 '15 at 19:35
  • IMHO it makes no difference from where you call this. It is always the entire app hit and each view is kept on screen, not dimmed. – decades Nov 19 '18 at 21:39
  • Is there any way to keep alive only one view in the app. If any other view is active the screen should dim as usual. I have tried setting [[UIApplication sharedApplication] setIdleTimerDisabled: NO]; in other view but didn't work. – Ayush Malviya Sep 12 '19 at 07:33
22

In swift you can use this as

UIApplication.sharedApplication().idleTimerDisabled = true
Kumar KL
  • 15,315
  • 9
  • 38
  • 60
Vettiyanakan
  • 7,957
  • 6
  • 37
  • 55
12

Swift 3:

UIApplication.shared.isIdleTimerDisabled = true
Charlie S
  • 4,366
  • 6
  • 59
  • 97
2

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.

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
  • 1
    Sadly putting the code into a function that fires on a timer did not fix the problem. I left my iPad on for over 24 hours and it did not go dim and no interaction. According to one of our clients a user used the device 15min prior and then the device went dim. – JMStudios.jrichardson Mar 21 '16 at 23:04
  • I'm having the same problem: I have a view controller where I set idelTimerDisabled = true (the app is supposed to remain open through the night, an alarm clock), and most of the time it works, but sometimes the app is backgrounded sometime during the night. Anyone have an idea what could move the app to the background? or reset the flag? – Yariv Adam May 11 '18 at 05:32
  • Yes you could reset the flag and I was doing that as well. I had this in a function as well. Turns out it was a "rouge process" on our MDM server sending pings to our devices to dim the screens. very annoying. – JMStudios.jrichardson May 12 '18 at 15:00
0

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.