@@ -17,6 +17,7 @@ - (instancetype)initWithFrame:(CGRect)frame {
17
17
#if (TARGET_OS_IOS)
18
18
[NSNotificationCenter .defaultCenter addObserver: self selector: @selector (handleScreenChange ) name: UIDeviceOrientationDidChangeNotification object: nil ];
19
19
[NSNotificationCenter .defaultCenter addObserver: self selector: @selector (handleScreenChange ) name: UIScreenModeDidChangeNotification object: nil ];
20
+ [NSNotificationCenter .defaultCenter addObserver: self selector: @selector (handleScreenChange ) name: UIApplicationDidBecomeActiveNotification object: nil ];
20
21
#endif
21
22
return self;
22
23
}
@@ -47,36 +48,59 @@ - (void)adjustWebViewForTraitCollection:(UITraitCollection *)traitCollection {
47
48
}
48
49
}
49
50
50
- - (void )handleScreenChange {
51
- CGRect screenBounds = [UIScreen mainScreen ].bounds ;
52
- if (@available (iOS 11.0 , *)) {
53
- CGFloat top = UIApplication.sharedApplication .keyWindow .safeAreaInsets .top ;
54
-
55
- if (top) {
56
- screenBounds.origin .y += top + 5 ;
57
- screenBounds.size .height -= top + 5 ;
58
- } else {
59
- screenBounds.origin .y += 20.0 ;
60
- screenBounds.size .height -= 20.0 ;
51
+ CGSize getWindowSize (void ) {
52
+ CGSize size = CGSizeZero ;
53
+
54
+ // Attempt to retrieve the size from the connected scenes (for modern apps)
55
+ if (@available (iOS 13.0 , *)) {
56
+ NSSet <UIScene *> *scenes = [[UIApplication sharedApplication ] connectedScenes ];
57
+ for (UIScene *scene in scenes) {
58
+ if ([scene isKindOfClass: [UIWindowScene class ]]) {
59
+ UIWindowScene *windowScene = (UIWindowScene *)scene;
60
+ UIWindow *window = windowScene.windows .firstObject ;
61
+ if (window) {
62
+ size = window.bounds .size ;
63
+ return size; // Return immediately if we find a valid size
64
+ }
65
+ }
61
66
}
62
- } else {
63
- screenBounds.origin .y += 20.0 ;
64
- screenBounds.size .height -= 20.0 ;
65
67
}
66
-
67
- CGFloat width = screenBounds.size .width ;
68
- CGFloat height = screenBounds.size .height ;
68
+
69
+ // Fallback for legacy apps using AppDelegate
70
+ id <UIApplicationDelegate> appDelegate = [[UIApplication sharedApplication ] delegate ];
71
+ if ([appDelegate respondsToSelector: @selector (window )]) {
72
+ UIWindow *legacyWindow = [appDelegate performSelector: @selector (window )];
73
+ if (legacyWindow) {
74
+ size = legacyWindow.bounds .size ;
75
+ }
76
+ }
77
+
78
+ return size;
79
+ }
80
+
81
+ - (void )handleScreenChange {
82
+ // Execute after a short delay to ensure properties are updated
83
+ dispatch_async (dispatch_get_main_queue (), ^{
84
+ [self updateWindowSize ];
85
+ });
86
+ }
87
+
88
+ - (void )updateWindowSize {
89
+ CGSize size = getWindowSize ();
90
+ CGFloat width = size.width ;
91
+ CGFloat height = size.height ;
69
92
70
93
NSString *postMessage = [NSString stringWithFormat:
71
94
@" javascript:window.postMessage({type: 'resize', width: %f , height: %f }, '*');" ,
72
95
width,
73
96
height];
74
- NSURL *uri = [NSURL URLWithString: postMessage];
75
- NSURLRequest *request = [NSURLRequest requestWithURL: uri];
76
- [self .webView loadRequest: request];
97
+ [self .webView evaluateJavaScript: postMessage completionHandler: ^(id result, NSError *err) {
98
+ if (err != nil ) {
99
+ CLY_LOG_E (@" [PassThroughBackgroundView] updateWindowSize, %@ " , err);
100
+ }
101
+ }];
77
102
}
78
103
79
-
80
104
// Always remove observers when the view is deallocated
81
105
- (void )dealloc {
82
106
[[NSNotificationCenter defaultCenter ] removeObserver: self ];
0 commit comments