2

Why can't I set screen brightness in applicationDidEnterBackground or applicationWillResignActive?

This is my code:

- (void)applicationDidEnterBackground:(UIApplication *)application
{        
    [[UIScreen mainScreen] setBrightness:1.0]; 
}

Can't understand it...!? It works in applicationDidEnterForeground etc, just not when i close the app using the home button.

Thanks to anyone that can help...

Adam Waite
  • 19,175
  • 22
  • 126
  • 148
  • I would not be surprised if the brightness gets set back to default after exiting completely, in order to avoid user frustration. Your brightness probably gets set properly, for a tiny amount of time. – Aurum Aquila Nov 29 '11 at 21:11
  • 1
    Good suggestion but I don't think that is it. I already change the brightness from high to low in the app and want it to restore the original brightness when the app is closed... – Adam Waite Nov 29 '11 at 21:18
  • Isn't that already happening anyway? As far as I know, iOS will simply reset the brightness itself. – Tom van der Woerdt Nov 29 '11 at 21:20
  • 1
    I have my system brightness set at 1.0 as i enter the app for the first time. The brightness gets lowered using [[UIScreen mainScreen] setBrightness:0.1] successfully. When i press the home button I want it to restore the original system brightness but i'm struggling to do it, it just stays dark. – Adam Waite Nov 29 '11 at 21:30
  • possible duplicate of [applicationWillResignActive and setBrightness not working?](http://stackoverflow.com/questions/8316358/applicationwillresignactive-and-setbrightness-not-working) – jrturton Nov 29 '11 at 21:40
  • yes, it's practically the same question but nobody could answer the first so I rephrased it. – Adam Waite Nov 29 '11 at 21:44
  • I've had to use a ridiculous way around of doing this. It's impossible due to iOS limitations to reduce the brightness on delegate methods that involve the app going inactive. I used an imageview containing a black rectangle with an opacity of 0.9 and set it to hidden whilst i want my app to be dim. The status bar was removed too because i couldn't make that dim with the rectangle. – Adam Waite Nov 29 '11 at 23:01
  • User frustration is relative. I use a brightness slider app and dont want that apple reverts the brightness. – Proud Member Apr 24 '12 at 17:55

3 Answers3

2

If you want to change the brightess you should do that:

func applicationWillResignActive(_ application: UIApplication) {
        DispatchQueue.main.async {
            UIScreen.main.brightness = 0.5
        }
    }
}
starmarmor
  • 41
  • 3
1

It's probably because your app is relinquishing control to the OS, and the OS will then control the brightness according to the user's global settings. If you want to change the brightness setting globally, you'd have to access the user's global iphone settings and change them, and I'm not sure that's possible.

When you're setting the brightness on applicationDidEnterForeground: your app is in control again, and can adjust stuff like brightness. As soon as your app loses that control thought, the OS takes over and brings the device back into compliance with the user's global settings.

Hope that helps.

Ian
  • 3,806
  • 2
  • 20
  • 23
  • Yeah sounds like you're right. Can you think of any workaround? Like is there something that gets called just before applicationWillResignActive or anything? Stumped! – Adam Waite Nov 29 '11 at 21:22
  • 2
    Although... it is entering the background and the screen remains dark although my global settings have the brightness near full... – Adam Waite Nov 29 '11 at 21:24
  • 1
    I've had to use a ridiculous way around of doing this. It's impossible due to iOS limitations to reduce the brightness on delegate methods that involve the app going inactive. I used an imageview containing a black rectangle with an opacity of 0.9 and set it to hidden whilst i want my app to be dim. The status bar was removed too because i couldn't make that dim with the rectangle. – Adam Waite Nov 29 '11 at 23:01
  • I'm glad it works - you could also have a fake image of the status bar that you show beneath your black rectangle as a placeholder until the app goes active again. I don't know if that works for your app though. Glad to hear you got something to work though :) – Ian Nov 29 '11 at 23:56
0

I'm pretty sure this is a bug with the current version (that the setBrightness call has no effect inside of either applicationDidEnterBackground or applicationWillResignActive).

What I do currently is similar to what you arrived at - I set the brightness to 1.0 on application active, and then reduce brightness when needed by modifying the opacity of a black layer on top of all other graphics. That way, if the user suspends the app, then their screen is at least bright (rather than at whatever arbitrary brightness the app was at).

When the user locks their screen, it will be restored to the brightness in their iPhone settings.