27

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?

Adam Waite
  • 19,748
  • 19
  • 120
  • 146

6 Answers6

12

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.

Community
  • 1
  • 1
Tibidabo
  • 20,865
  • 4
  • 85
  • 85
  • Do file a bug report. Apple prioritizes fixes them based on the number of duplicates. It also helps if you explain how the fix will improve the user experience. I'm going to file it again even though yours was closed. – progrmr Dec 19 '12 at 23:41
1

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.

TigerCoding
  • 8,660
  • 10
  • 44
  • 72
  • Yeah, I already picked that up from his comments, but I don't want to do it that way. You'd also be better off doing it as a new window at status bar level than as a subview, otherwise you'd probably have to keep moving it to the front all the time! – jrturton Feb 09 '12 at 19:58
  • It's a hack, I know, but it works for my apps. Hopefully there will be a fix in the future. – TigerCoding Feb 09 '12 at 20:00
  • This doesn't help my situation, I'm reducing the backlight to reduce battery usage. – progrmr Dec 19 '12 at 23:43
1

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];        
}
sergio
  • 67,961
  • 11
  • 98
  • 119
  • Good idea, but no, same result. I'm beginning to think the answer is just to file a bug. I mean really, in-app brightness changes shouldn't even be able to affect the whole device - or maybe not. iBooks has the same issue. Changing the brightness in there is a global setting. – jrturton Feb 11 '12 at 20:51
0

When you tap the home button your application doesn't resign active but rather goes into "Background Mode" and has its own delegate method which you need to define:

- (void)applicationDidEnterBackground:(UIApplication *)application

Something like this (in addition to the applicationDidBecomeActive: delegate method) should get the intended behavior:

- (void)applicationDidEnterBackground:(UIApplication *)application
{        
    sysBright = [[NSUserDefaults standardUserDefaults] floatForKey:@"sysBright"];
    [[UIScreen mainScreen] setBrightness:sysBright]; 
}
JasonMArcher
  • 12,386
  • 20
  • 54
  • 51
Suhail Patel
  • 13,374
  • 2
  • 41
  • 48
  • Thanks for the suggestion but it still isn't working... I tried: - (void)applicationDidEnterBackground:(UIApplication *)application { NSLog(@"The App is Running in the background"); //Set Brightness back to normal [[UIScreen mainScreen] setBrightness:sysBright]; } ` and it NSLogs the comment on me pressing the home button but doesn't alter the brightness?! – Adam Waite Nov 29 '11 at 20:21
  • Not sure about the rest of the program but then you'd probably need to get back the sysBright variable from user defaults. See my updated code in my original answer – Suhail Patel Nov 29 '11 at 20:32
  • 2
    Tried your new suggestion but still no luck. I can NSLog the correct sysBright value in the applicationDidEnterBackground method but it just wont actually change the brightness. The brightness does change in applicationWillEnterForeground when I open the app back up using the same code! very confused! – Adam Waite Nov 29 '11 at 20:58
  • 1
    I think it's impossible to do as of now, but I have made a ridiculous workaround involving an ImageView set as a black rectangle with 0.9 opacity. Works a charm if i'm honest, just frustrating that I wasted so much time without finding a solution involving [UIScreen setBrightness]; – Adam Waite Nov 30 '11 at 02:29
0

Second to last line, you didn't assign the sysBright variable.

sysBright = [[NSUserDefaults standardUserDefaults] floatForKey:@"sysBright"];
[[UIScreen mainScreen] setBrightness:sysBright];        
Tim
  • 14,221
  • 6
  • 37
  • 62
  • I think it's impossible to do as of now, but I have made a ridiculous workaround involving an ImageView set as a black rectangle with 0.9 opacity. Works a charm if i'm honest, just frustrating that I wasted so much time without finding a solution involving [UIScreen setBrightness]; – Adam Waite Nov 30 '11 at 02:29
  • I just tried it in my own app, it's working great. Make sure the brightness value is between 0 and 1 and is a float. – Tim Nov 30 '11 at 20:53
  • @Tim, theres a bounty in this if you can give me the details I am after. Setting the screen brightness in the app delegate methods does not work. – jrturton Feb 06 '12 at 07:18
-1

did you call synchronize?

[[NSUserDefaults standardUserDefaults] synchronize];
yeahdixon
  • 6,131
  • 1
  • 36
  • 42