10

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];        
}
phnessu4
  • 131
  • 1
  • 8
  • What do you mean by "quit button" ? The red minus in the task switcher or the home button? – conradev Nov 27 '11 at 05:27
  • sorry for that , it's home button, i already change the post content – phnessu4 Nov 27 '11 at 07:56
  • hi, did you find any workaround? I'm stuck at this too... – cpprulez Jan 04 '12 at 19:47
  • You can't do this, I tried a while back. Check out my question from ages ago for suggestions -http://stackoverflow.com/questions/8316358/applicationwillresignactive-and-setbrightness-not-working#comment12659025_8316358 – Adam Waite Apr 12 '12 at 14:15
  • CGFloat sysBright = [[NSUserDefaults standardUserDefaults] floatForKey:@"sysBright"] [[UIScreen mainScreen] setBrightness:sysBright]; this is one correction in your code. But i dont think this will work because apple wont allow this – Shrikant Phadke Sep 16 '20 at 11:14

4 Answers4

5

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.

Tibidabo
  • 21,461
  • 5
  • 90
  • 86
1

Try this...

- (void)applicationWillResignActive:(UIApplication *)application
{        
    CGFloat brightness = [[NSUserDefaults standardUserDefaults] floatForKey:@"sysBright"];
    [[UIScreen mainScreen] setBrightness:brightness];        
}
  • Wouldn't you be using a private API if you get the brightness that way? Why not use `CGFloat initialScreenBrightness = [UIScreen mainScreen].brightness;` – Fernando Cervantes Jun 17 '12 at 15:21
0

There are cases when you really need to suspend the application (make it go to background, like when you push the Home button) and still preserve the brightness you have previously set on the screen.

Example: I 'm currently working on an underwater application (it takes photos with an iPhone in an waterproof case for scientific reasons) and we don't have access to the whole device screen.

The underwater housing implements 3 mechanical "touch" buttons on very specific places and we have to disable the auto-lock feature because there is no way to perform a slide-gesture to unlock the device.

We still need a way to preserve battery life when not using the application i.e. suspend the application and setting a low level on screen brightness.

The solution we implemented is:

a) We tell the user to turn the Auto-Brightness off in settings and disable the auto-lock feature

b) We turn the Brightness to 100% on ApplicationDelegate class:

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Adjust Brightness to full
    [[UIScreen mainScreen] setBrightness:1.0];
}

c) We execute the following code with the touch on an application button, to ensure that the application goes to background (not consuming CPU cycles) and the brightness go to lower level possible (preserve battery):

[[UIScreen mainScreen] setBrightness:0.0]; // Set to low brightness
[[UIApplication sharedApplication] performSelector:@selector(suspend)]; // Simulate Home button

I hope this could be helpful to somebody.

PS: The Apple Human interface guides is one thing and the actual needs of a real-world application is another (you cannot predict or restrict anything in advance).

Fivos Vilanakis
  • 1,490
  • 12
  • 13
0

According to Apple´s DevForum it seems to be a bug that Apple is unwilling to fix.

ehrpaulhardt
  • 1,607
  • 12
  • 19