Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IOS5 setBrightness didn't work with applicationWillResignActive

I use [[UIScreen mainScreen]setBrightness: ] (in sdk 5.0) to change the system background light in my app.

The following steps work with my app

  1. Active the app, get the system brightness as default, then save as sysBright.

  2. Change the brightness with my app, changed brightness, then save as appBright.

  3. ResignActive app with home button or lock button, set brightness to sysBright (step 1 value, system default brightness).

  4. Active app again. Then it will repeat the above steps form 1 to 3.

Something is wrong with step 3, when I inactivate the app with the lock button, the function applicationWillResignActive works well, it can restore the brightness value (sysBright).

But when I press the home button, it doesn't work anymore. The brightness is still the value I changed in my app. (appBright)

Does anyone have any idea about it? Thanks for any help ~

Here is the code:

float appBright,sysBright;

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    sysBright = [[UIScreen mainScreen] brightness];
    [[NSUserDefaults standardUserDefaults] setFloat:sysBright forKey:@"sysBright"];

    [[UIScreen mainScreen] setBrightness:appBright];
}

//doesn't work when i ResignActive with the home button
- (void)applicationWillResignActive:(UIApplication *)application
{        
    [[NSUserDefaults standardUserDefaults] floatForKey:@"sysBright"];
    [[UIScreen mainScreen] setBrightness:sysBright];        
}
like image 680
phnessu4 Avatar asked Nov 26 '11 18:11

phnessu4


2 Answers

iOS is not meant to retain in-app brightness values. It should restore system value after the app resigns active, quits, crashes etc. So officially there is no need to do that in applicationWillResignActive.

But it does't work. It's a bug. In fact it works if you switch to another app (press home button twice and select another app)

Don't waste your time just file a bug report to Apple (I did well).

Unlock screen restores default system brightness. Just press the power button twice and unlock to restore original brightness.

like image 82
Tibidabo Avatar answered Nov 16 '22 03:11

Tibidabo


Try this...

- (void)applicationWillResignActive:(UIApplication *)application
{        
    CGFloat brightness = [[NSUserDefaults standardUserDefaults] floatForKey:@"sysBright"];
    [[UIScreen mainScreen] setBrightness:brightness];        
}
like image 29
Andreas Grauel Avatar answered Nov 16 '22 01:11

Andreas Grauel