-
Notifications
You must be signed in to change notification settings - Fork 244
/
Copy pathPassThroughBackgroundView.m
110 lines (90 loc) · 3.64 KB
/
PassThroughBackgroundView.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// PassThroughBackgroundView.m
//
// This code is provided under the MIT License.
//
// Please visit www.count.ly for more information.
#import "PassThroughBackgroundView.h"
#if (TARGET_OS_IOS)
@implementation PassThroughBackgroundView
@synthesize webView;
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
#if (TARGET_OS_IOS)
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(handleScreenChange) name:UIDeviceOrientationDidChangeNotification object:nil];
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(handleScreenChange) name:UIScreenModeDidChangeNotification object:nil];
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(handleScreenChange) name:UIApplicationDidBecomeActiveNotification object:nil];
#endif
return self;
}
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
if (self.webView && CGRectContainsPoint(self.webView.frame, point)) {
return YES;
}
if (self.dismissButton && CGRectContainsPoint(self.dismissButton.frame, point)) {
return YES;
}
return NO;
}
- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection {
[super traitCollectionDidChange:previousTraitCollection];
if (self.traitCollection.horizontalSizeClass != previousTraitCollection.horizontalSizeClass) {
[self adjustWebViewForTraitCollection:self.traitCollection];
}
}
- (void)adjustWebViewForTraitCollection:(UITraitCollection *)traitCollection {
if (traitCollection.horizontalSizeClass == UIUserInterfaceSizeClassCompact) {
[self handleScreenChange];
}
}
CGSize getWindowSize(void) {
CGSize size = CGSizeZero;
// Attempt to retrieve the size from the connected scenes (for modern apps)
if (@available(iOS 13.0, *)) {
NSSet<UIScene *> *scenes = [[UIApplication sharedApplication] connectedScenes];
for (UIScene *scene in scenes) {
if ([scene isKindOfClass:[UIWindowScene class]]) {
UIWindowScene *windowScene = (UIWindowScene *)scene;
UIWindow *window = windowScene.windows.firstObject;
if (window) {
size = window.bounds.size;
return size; // Return immediately if we find a valid size
}
}
}
}
// Fallback for legacy apps using AppDelegate
id<UIApplicationDelegate> appDelegate = [[UIApplication sharedApplication] delegate];
if ([appDelegate respondsToSelector:@selector(window)]) {
UIWindow *legacyWindow = [appDelegate performSelector:@selector(window)];
if (legacyWindow) {
size = legacyWindow.bounds.size;
}
}
return size;
}
- (void)handleScreenChange {
// Execute after a short delay to ensure properties are updated
dispatch_async(dispatch_get_main_queue(), ^{
[self updateWindowSize];
});
}
- (void)updateWindowSize {
CGSize size = getWindowSize();
CGFloat width = size.width;
CGFloat height = size.height;
NSString *postMessage = [NSString stringWithFormat:
@"javascript:window.postMessage({type: 'resize', width: %f, height: %f}, '*');",
width,
height];
[self.webView evaluateJavaScript:postMessage completionHandler:^(id result, NSError *err) {
if (err != nil) {
CLY_LOG_E(@"[PassThroughBackgroundView] updateWindowSize, %@", err);
}
}];
}
// Always remove observers when the view is deallocated
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
#endif