16

I am building application using swift 2 targeting minimal iOS 8.

I need to present my application all the time without interrupted by device lock.

I am aware and currently using UIApplication.sharedApplication().idleTimerDisabled = true to disable device locking.

The question is, is it possible (and how ?) to prevent device lock, but allow the screen to be dimmed (light off) ?

The goal is to save a bit more battery by turning off lcd light (without turning it off or lock device).

Thank you.

Idan
  • 5,405
  • 7
  • 35
  • 52
Lee
  • 3,259
  • 4
  • 21
  • 27
  • Possibly related? [Keep display on when the proximity sensor is covered](http://stackoverflow.com/q/27147847/2415822) – JAL Jun 03 '16 at 18:57

2 Answers2

10

You cannot prevent the dimming, but you can set the brightness during the app operation. You will still disable the idle timer, but modify the brightness manually.

UIApplication.sharedApplication().idleTimerDisabled = true
UIScreen.mainScreen().brightness = CGFloat(0.1)

Few notes regarding your question:

  • Brightness values are between 0.0 to 1.0
  • If the the app did enter to background (idle timer is enabled), the brightness value will go back to the user settings
  • Settings a Display's Brightness

Swift 3

 UIApplication.shared.isIdleTimerDisabled = true
Alex Pelletier
  • 4,933
  • 6
  • 34
  • 58
Idan
  • 5,405
  • 7
  • 35
  • 52
7

For my case we do not want the device to auto lock or dim. We have the snippet of code

UIApplication.sharedApplication().idleTimerDisabled = true

In Swift 4+

UIApplication.shared.isIdleTimerDisabled = true

In the view controllers that should not auto-lock or dim, yet our devices are still dimming but not auto-locking. For us this is annoying and a bug but for you it sounds like it's what you want. Although from other posts I have found:

  1. iPhone - phone goes to sleep even if idleTimerDisabled is YES
  2. idleTimerDisabled not working since iPhone 3.0

It seems to be device specific, let me know if this helps.

Jin Kim
  • 35
  • 1
  • 7
  • Yes, what you've described is what I want. But the code you provided doesn't help. Thanks anyway. – Lee Apr 06 '16 at 08:49