0

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....

Gizmodo
  • 3,151
  • 7
  • 45
  • 92
  • Did you try it? Trying it yourself first is faster than posting a question and waiting for an answer. – rmaddy Sep 29 '14 at 23:30
  • There is also the `UIScreenBrightnessDidChangeNotification` notification. Give that a shot. – rmaddy Sep 29 '14 at 23:31

2 Answers2

2

According to the Apple Developer docs:

Brightness changes made by an app remain in effect only while the app is active. The system restores the user-supplied brightness setting at appropriate times when your app is not in the foreground. So if you change the value of this property, you do not need to record the previous value and restore it when your app moves to the background.

dliebeskind
  • 101
  • 2
  • 12
1

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;
  }
}
Jason Coco
  • 77,985
  • 20
  • 184
  • 180