Skip to content

Commit a259792

Browse files
fix: make getTopWindow more robust (#621)
## 📜 Description This PR updates the top window retrieval method: Old: `UIApplication.shared.windows.last` (deprecated in iOS 15+) New: `UIWindowScene`-based approach Differences: - `UIApplication.shared.windows.last`: - It accesses all windows associated with the application. - The last window in this array isn't always the topmost or key window, especially in more complex app setups. - This approach is deprecated as of iOS 15, though it still works in many cases. - `UIWindowScene`: - Iterates through all connected scenes to find the active foreground scene - Ensures selection of the correct key window in multi-window environments (e.g., Split View in iPadOS) - Provides more reliable behaviour in complex app configurations that utilize multiple `UIWindowScenes` ## 💡 Motivation and Context Closes #618 In our case, this change was required to get the children of the `OverKeyboardView` to show. Without it, they were still rendered, but invisible. This change should enhance the reliability across different iOS versions and device types. ## 📢 Changelog ### iOS - Get the last key window in the current `UIWindowScene` as opposed to the last window of all windows associated with the app ## 🤔 How Has This Been Tested? It works on my company project. Did not manage to get the example app to compile on my machine. Could be due to the (lack of) provisioning profiles. ## 📝 Checklist - [x] CI successfully passed - [x] I added new mocks and corresponding unit-tests if library API was changed
1 parent 0553cd8 commit a259792

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

ios/extensions/UIApplication.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,22 @@ import Foundation
99
import UIKit
1010

1111
public extension UIApplication {
12+
var activeWindow: UIWindow? {
13+
if #available(iOS 13.0, *) {
14+
for scene in connectedScenes {
15+
if scene.activationState == .foregroundActive,
16+
let windowScene = scene as? UIWindowScene,
17+
let keyWindow = windowScene.windows.first(where: { $0.isKeyWindow })
18+
{
19+
return keyWindow
20+
}
21+
}
22+
return nil
23+
} else {
24+
return windows.last { $0.isKeyWindow }
25+
}
26+
}
27+
1228
static func topViewController(
1329
base: UIViewController? = UIApplication.shared.keyWindow?.rootViewController
1430
) -> UIViewController? {

ios/extensions/UIWindow.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public extension UIWindow {
3838

3939
func getTopWindow() -> UIWindow? {
4040
// Return the keyboard window if it's available, otherwise return the last window
41-
return keyboardWindow ?? UIApplication.shared.windows.last
41+
return keyboardWindow ?? UIApplication.shared.activeWindow
4242
}
4343
}
4444

0 commit comments

Comments
 (0)