Skip to content

Commit eacbfd1

Browse files
authored
MOB-11622: Fix InApp messages displaying in wrong window with Stage Manager (#917)
1 parent d5b9e7d commit eacbfd1

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

swift-sdk/Internal/Utilities/IterableUtil.swift

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,59 @@ import UIKit
88

99
@objc final class IterableUtil: NSObject {
1010
static var rootViewController: UIViewController? {
11+
// Try modern approach first for iOS 13+ multi-window support
12+
if #available(iOS 13.0, *) {
13+
if let activeViewController = getActiveWindowRootViewController() {
14+
return activeViewController
15+
}
16+
}
17+
18+
// Existing fallback chain - unchanged for backward compatibility
1119
if let rootViewController = AppExtensionHelper.application?.delegate?.window??.rootViewController {
1220
return rootViewController
1321
} else {
1422
return AppExtensionHelper.application?.windows.first?.rootViewController
1523
}
1624
}
1725

26+
@available(iOS 13.0, *)
27+
private static func getActiveWindowRootViewController() -> UIViewController? {
28+
guard let application = AppExtensionHelper.application else { return nil }
29+
30+
// Find active scenes (foreground active takes priority)
31+
let activeScenes = application.connectedScenes
32+
.compactMap { $0 as? UIWindowScene }
33+
.filter { $0.activationState == .foregroundActive }
34+
35+
// Look for key window in active scenes first
36+
for scene in activeScenes {
37+
if let keyWindow = scene.windows.first(where: { $0.isKeyWindow }),
38+
let rootVC = keyWindow.rootViewController {
39+
return rootVC
40+
}
41+
}
42+
43+
// Fallback to first window in first active scene
44+
if let firstActiveScene = activeScenes.first,
45+
let rootVC = firstActiveScene.windows.first?.rootViewController {
46+
return rootVC
47+
}
48+
49+
// Final fallback: any foreground inactive scene with key window
50+
let inactiveScenes = application.connectedScenes
51+
.compactMap { $0 as? UIWindowScene }
52+
.filter { $0.activationState == .foregroundInactive }
53+
54+
for scene in inactiveScenes {
55+
if let keyWindow = scene.windows.first(where: { $0.isKeyWindow }),
56+
let rootVC = keyWindow.rootViewController {
57+
return rootVC
58+
}
59+
}
60+
61+
return nil
62+
}
63+
1864
static func trim(string: String) -> String {
1965
string.trimmingCharacters(in: .whitespaces)
2066
}

0 commit comments

Comments
 (0)