Lee
Lee

Reputation: 3329

Swift, prevent device lock but allow turn off lcd light (dim screen)

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.

Upvotes: 16

Views: 6914

Answers (2)

JMStudios.jrichardson
JMStudios.jrichardson

Reputation: 416

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.

Upvotes: 7

Idan
Idan

Reputation: 5450

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

Upvotes: 10

Related Questions