If you want to return the brightness back to the program value when the device is unlocked again, pay attention to the notification UIApplicationDidBecomeActiveNotification and set the brightness to the desired level in your selector.
I did some more research on all this brightness, and here is what I found:
UIScreenBrightnessDidChangeNotification is called ONLY when the system changes brightness or when the user changes brightness from the control panel or settings. NOT when you change the brightness programmatically.
What I suggested from this Apple Doc here (see below) is that after it is programmatically configured, the brightness will no longer change.
The brightness changes made by the application remain valid until the device is locked, regardless of whether the application is closed. The brightness of the system (which the user can set in Settings or the Control Center) is restored the next time the display is turned on.
However, I found that this is not the case (or I misinterpret it). The brightness changes (and UIScreenBrightnessDidChangeNotification is called) after you set the brightness programmatically. However, it ONLY receives a call when the system thinks that it should increase or decrease the brightness (due to changes in the brightness of the environment) *. If the brightness of the environment remains unchanged, your screen will remain as bright as you programmatically.
What does it mean? Well, 2 things:
- If you want to keep the brightness at the level that you set it programmatically, regardless of the change in environment, you must monitor the
UIScreenBrightnessDidChangeNotification level and reset the brightness to the desired level each time it is called. - If you want to return to "system" brightness, you really cannot, because you simply cannot say what the brightness of the system would be if there were changes in the environment (because you reset it every time), You have 2 options for this .
- Remember the brightness before you set your brightness programmatically and return to this value. Or
- Set the brightness to 0.5 and let the system work with it.
The βdangerβ in both situations is that it will remain at the value you set until a change in the brightness of the medium is detected.
* There are 2 special cases ... when you set the brightness to 0.0, and the system thinks that it should decrease the brightness, nothing happens, because it is already at 0.0. Secondly, when you set it to 1.0, it will remain at 1.0, regardless of how the environment changes.
guido source share