Unable to restore brightness on application exit
Asked Answered
D

3

12

I have a feature in my app (a metronome app for musicians) to dim the screen in order to save on battery life. To set the brightness, I am using:

[UIScreen mainScreen].brightness = 0.1;

I am saving the original brightness on app start up in viewDidLoad(...) and saving that to my User Defaults.

When changing views within the app, I retrieve the original brightness from User Defaults and restore with a call:

[UIScreen mainScreen].brightness = originalBrightness;

This works fine. I have NSLog(...) messages showing the original value, etc... so the mechanism works.

The issue I am having is how to restore the original brightness on application exit as the Home button is pressed.

I have added similar code to my application delegate methods for:

applicationWillResignActive(...)
applicationDidEnterBackground(...)
applicationWillTerminate(...)

They each have a method call to set the screen brightness as before in the view with similar NSLog(...)s showing the retrieved original brightness...and it is all correct. I have also debugged in and the calls to set brightness are being made...but the app exits, and the device screen is still at the lower, dimmed level.

Finally, if you click the "lock" button on top, then press the Home button to wake it up...the brightness is correct.

My suspicion is that whatever action or event is triggered with my call to set the brightness when the app is exiting is not getting through, maybe due to an invalid state or similar.

Also, my app is set to NOT run in the background, set in the info.plist as:

Application does not run in background     YES

Any help would be appreciated.

Thanks!

Divan answered 29/2, 2012 at 23:30 Comment(4)
Are these private APIs? I am not seeing either mainWindow or mainWindows (typo?) properties in the UIScreen class. Do you mean mainScreen?Cascio
Yes, typo on my part. I is is '[UIScreen mainScreen].brightness=val;' or [[UIScreen mainScreen] setBrightness:val];'Divan
I'm pretty sure there is no way to make this setting persistent outside of your app. Apple's documentation doesn't specifically say this but I've seen two other brightness-changing apps where this is the case. Someone here concurs.Sensorimotor
Any luck? I doing something similar but ran into the same problem. Heck, even [UIScreen mainScreen].brightness = 1.0; doesn't work.Hypoploid
C
2

iOS allows that app a little bit of time before exiting. You might try a sleep for a fraction of a second after setting the brightness.

Cascio answered 29/2, 2012 at 23:42 Comment(1)
@piccano I tried that after posting here, but in the delegate to no avail. I tried: [code]sleep()[/code] as well as [code][Thread sleepForTimeInterval:][/code] but no change.Divan
E
0

If you read https://devforums.apple.com/thread/139813 carefully it says that brightness changes are not permanent. The original user brightness is back, when you hit the lock button and unlock it again. Actually, I had to restore my app brightness if user hits lock button while my app is running:

- (void)applicationDidBecomeActive:(UIApplication *)application{
    [self setBrightness];
}
Eisegesis answered 16/7, 2013 at 12:27 Comment(0)
H
0

Not had much joy with this. Workaround was to reset brightness on a ViewController viewWillDisappear. Not a great solution but the only one I have found so far to work (this has been broken for years...)

Hermosillo answered 30/8, 2015 at 23:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.