If you want to set the brightness back to a programatic value when the device is unlocked again, observe the UIApplicationDidBecomeActiveNotification
notification and set the brightness back to you desired level in your selector.
I've done some more research on this whole brightness thing and here's what I found:
The UIScreenBrightnessDidChangeNotification
gets called ONLY when the system changes the brightness or when the user changes the brightness from the control panel or settings. NOT when you change the brightness programatically.
What I assumed from this Apple Doc here (see below) is that brightness will not change anymore after you set it programatically.
Brightness changes made by an app remain in effect until the device is
locked, regardless of whether the app is closed. The system brightness
(which the user can set in Settings or Control Center) is restored the
next time the display is turned on.
However I found this not to be true (or I'm misinterpreting it). The brightness does change (and UIScreenBrightnessDidChangeNotification
does get called) after you set the brightness programatically. However it ONLY gets called when the system thinks it should increase or decrease the brightness (due to changes in the environment brightness) *. If the brightness of the environment stays the same your screen will stay as bright as you set it programatically.
What does this mean? Well, 2 things:
- If you want to keep the brightness at the level you set it programatically, no matter environment changes, you have to observe the
UIScreenBrightnessDidChangeNotification
and reset the brightness to your desired level every time it's called.
- If you want to go back to the "system" brightness, well you can't really, because you simply can't tell what the system brightness would be if environment changes took place (because you are resetting it every time). You have 2 options for this.
- Remember the brightness before you set your brightness programatically and set it back to that value. Or
- Put the brightness to 0.5 and let the system to it's job.
The 'danger' in both situations is that it will stay on the value you set it until an environment brightness change is detected.
*There are 2 special cases... when you set the brightness to 0.0 and the system thinks it should decrease brightness nothing happens because it's already on 0.0. Secondly, when you set it to 1.0 it will stay on 1.0, no matter how the environment changes.