Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting currently focused UIScene when multiple connected scenes are active in foreground

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!

like image 427
freshking Avatar asked Aug 27 '19 17:08

freshking


1 Answers

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
like image 51
Saafo Avatar answered Sep 29 '22 03:09

Saafo