This is what I am truing to do on this iO7 or later app:
When the user opens my app, I want to get the screen brightness value and turn up the brightness all the way. When the user leaves the app, I want the brightness to go back to previous value.
So far I use this:
-(void) applicationWillResignActive
{
[[UIScreen mainScreen] setBrightness:oldScreenBrightness];
}
-(void) applicationDidBecomeActive
{
screenBrightness = [UIScreen mainScreen].brightness;
[[UIScreen mainScreen] setBrightness:1.0];
}
This works fine, except, if the user adjusts the brightness via Control Center WHILE using the app, it will still go back to the old brightness upon leaving the app when it should not.
So I was wondering, if I can Key Value Observe [UIScreen mainScreen].brightness and check if it's changed by the user to omit changing it back to old value....
I think you're overcomplicating it. On your way out, check the current brightness level. If it's 1.0
, switch it back to whatever you got when you started. If it's anything but 1.0
, you can feel fairly confident that the user changed it to something else, so do nothing.
It's also probably best to create some kind of constant for your screen brightness to make the code more readable and the intention clear.
static const CGFloat kAppConfiguredBrightness = (CGFloat)1.;
-(void)applicationWillResignActive
{
UIScreen* screen = [UIScreen mainScreen];
if (screen.brightness == kAppConfigredBrightness) {
screen.brightness = oldScreenBrightness;
}
}