Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

applicationWillResignActive and setBrightness not working?

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];        
}

Am i missing something?

like image 953
Adam Waite Avatar asked Nov 29 '11 19:11

Adam Waite


3 Answers

I answered a similar question here: IOS5 setBrightness didn't work with applicationWillResignActive

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 try to switch to another app. Try pressing Home button twice and your brightness is gone.

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.

UPDATE: My bug report was closed because according to Apple it's not a bug. It is weird.

like image 99
Tibidabo Avatar answered Nov 09 '22 01:11

Tibidabo


Since the current methods don't work in the app delegate methods, you can use a UIView as described by Adam.

Make a UIView lvar in the delegate h file:

UIView *view_;

Then in the implementation:

// create view in app delegate didFinishLaunchingWithOptions method and add it to the window
view_ = [[UIView alloc] initWithFrame:self.window.frame]; // delegate lvar
[view_ setBackgroundColor:[UIColor blackColor]];
[view_ setAlpha:0.5];
[self.window addSubview:view_];
[self.window makeKeyAndVisible];
return YES;

Make the view the size of your screen or view controller as needed. and make sure it's on top of all other subviews.

Keep in mind this will have a significant affect on the graphics performance of the app, but unless you are doing something crazy with the UI, it should be okay.

like image 1
TigerCoding Avatar answered Nov 08 '22 23:11

TigerCoding


Have you tried registering your main view controller (or whatever object as to that) for the UIApplicationWillResignActiveNotification which is sent after executing applicationWillResignActive?

This will give you a chance to modify the brightness outside of applicationWillResignActive. Don't know if this method makes any difference, but trying it should be easy.

Simply call:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appWillResign) name:UIApplicationWillResignActiveNotification object:nil];

then define:

- (void)appWillResign {
    [[NSUserDefaults standardUserDefaults] floatForKey:@"sysBright"];
    [[UIScreen mainScreen] setBrightness:sysBright];        
}
like image 1
sergio Avatar answered Nov 08 '22 23:11

sergio