I have a problem getting the current UIScene
when multiple scenes are connected.
Specifically, my app (iOS 13) supports multiple windows on the iPad. When I have two windows side by side and I access the apps connected scenes (with UIApplication.shared.connectedScenes
), all of them have the activationState
set to .foregroundActive
. But I am actually only interacting with one of them.
I found some code here on stackoverflow to retrieve the current key window (can't remember who it was that posted it) but as you can see it will always return one of the two windows.
let keyWindow: UIWindow? = UIApplication.shared.connectedScenes
.filter({$0.activationState == .foregroundActive})
.map({$0 as? UIWindowScene})
.compactMap({$0})
.first?.windows
.filter({$0.isKeyWindow}).first
Am I missing some method to determine the last window that was interacted with or are the multiple .foregroundActive
states a bug?
Thanks!
Although UIApplication.shared.keyWindow
is deprecated after iOS 13.0 by Apple, it seems this attribute can help you find the active one when multiple scenes in the foreground by using:
UIApplication.shared.keyWindow?.windowScene
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With