Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

swift

ios8

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.

like image 841
Lee Avatar asked Feb 22 '16 11:02

Lee


People also ask

How do I stop my lock screen from dimming?

Navigate to Settings, and then tap Display. Tap the switch next to Adaptive brightness to turn the setting off. Then, adjust the Brightness bar until you've reached your desired level of brightness.

How do I stop my iPhone from locking and dimming?

To turn off Auto-Dimming: Open Settings > Accessibility > Display & Text Size, and tap the Auto-Brightness toggle. To turn off Night Shift: Open Settings > Display & Brightness > Night Shift, and tap the Scheduled toggle. Your iPhone display will also dim when Low Power Mode activates due to the battery running low.

How do I stop my iPhone screen from dimming when watching videos?

It may look like your iPhone 14 Pro or Pro Max keeps dimming when it transitions to Always On Display. To turn off Always On Display, open Settings and tap Display & Brightness. Scroll down and turn off the switch next to Always On.

How do I stop my iPad from darkening?

Turn Dark Mode on or offOpen Control Center, touch and hold. , then tap. to turn Dark Mode on or off. Go to Settings > Display & Brightness, then select Dark to turn on Dark Mode or select Light to turn it off.


2 Answers

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
like image 188
Idan Avatar answered Sep 21 '22 14:09

Idan


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.

like image 25
JMStudios.jrichardson Avatar answered Sep 21 '22 14:09

JMStudios.jrichardson