@@ -8,13 +8,59 @@ import UIKit
8
8
9
9
@objc final class IterableUtil : NSObject {
10
10
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
11
19
if let rootViewController = AppExtensionHelper . application? . delegate? . window?? . rootViewController {
12
20
return rootViewController
13
21
} else {
14
22
return AppExtensionHelper . application? . windows. first? . rootViewController
15
23
}
16
24
}
17
25
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
+
18
64
static func trim( string: String ) -> String {
19
65
string. trimmingCharacters ( in: . whitespaces)
20
66
}
0 commit comments