I am trying to control the brightness of the screen while the app is in the background, and I am not sure if this is possible.
this UIScreen brightness documentation notes that
Brightness changes made by an app remain in effect until the device is locked, regardless of whether the app is closed. The system brightness (which the user can set in Settings or Control Center) is restored the next time the display is turned on.
this does not seem to say that you cannot set the screen brightness while in the background, on the applications main thread or otherwise. It merely says that the brightness is reset on a lock/unlock cycle.
consider the below snippets:
backgroundTask = UIApplication.shared().beginBackgroundTask(withName: "something", expirationHandler: handler) // handler ends the background task
registerCallbackForSomeEvent(callback: changeScreenBrightness)
the event is irrelevant. I can print() while the app is suspended and it all works fine.
and the implementation of changeSCreenBrightness()
is something like the following:
func changeScreenBrightness() {
let newBrightness = getNewBrightnessSomehow()
// main or global, it doesn't seem to be happening...
DispatchQueue.main.async(execute: {
// this works in the foreground?! but not the background..
UIScreen.main().brightness = newBrightness
// a print() will be executed however..
})
}
so.. can anyone definitively say that this is impossible? or does someone have working code that I could study?
thanks!