0

code not working in did enter in background method.

 func applicationDidEnterBackground(_ application: UIApplication) {

        UIScreen.main.brightness = CGFloat(0.5)

    }
  • even i am already tried with delay but still not working. – Pawan Paliwal Jan 17 '19 at 05:00
  • 6
    You can't. When an app enters the background it is no longer able to change the screen brightness. – rmaddy Jan 17 '19 at 05:01
  • 2
    1. Conceptually, if your app goes into background, it wont be visible, so why is it trying to change UI (brightness)? 2. Have you tried it doing this in `applicationWillResignActive` instead? – Swapnil Luktuke Jan 17 '19 at 05:24

1 Answers1

0

I tried to do this in the applicationWillResignActive and seems to achieve what you are looking for. Not sure if this is recommended behaviour though.

Here are my methods from AppDelegate

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

        UIScreen.main.brightness = 1.0;
        return true
    }



func applicationWillResignActive(_ application: UIApplication) {

         UIScreen.main.brightness = 0.5
    }
humblePilgrim
  • 1,818
  • 4
  • 25
  • 47
  • it's working in applicationWillResignActive but not working in DidEnterInBackground . can you check un DidEnterInBackground at Your side ? – Pawan Paliwal Jan 17 '19 at 07:46
  • Any reason why you want this to happen in DidEnterInBackground – humblePilgrim Jan 18 '19 at 05:50
  • my app functionality is in low brightness so when user go in other app so it should be not go with low brightness. i.e. i want to change brightness when user enter in backround mode. – Pawan Paliwal Jan 18 '19 at 06:07
  • The above code doesn't work in your case ? When the user navigates to another app, the brightness does change right ? – humblePilgrim Jan 18 '19 at 06:09
  • yes, i want to change brightness when user navigate to other app. – Pawan Paliwal Jan 18 '19 at 06:11
  • That is happening right ? Launch the aapp -> Brightness becomes 1, send app to background/Switch to another app -> brightness becomes .5. I just checked ... – humblePilgrim Jan 18 '19 at 06:19