Skip to content

Commit 891650c

Browse files
feat: resize me action all done
1 parent b16193e commit 891650c

File tree

3 files changed

+83
-49
lines changed

3 files changed

+83
-49
lines changed

CountlyContentBuilderInternal.m

+38-19
Original file line numberDiff line numberDiff line change
@@ -151,30 +151,49 @@ - (NSURLRequest *)fetchContentsRequest
151151
return request;
152152
}
153153

154-
- (NSString *)resolutionJson {
155-
//TODO: check why area is not clickable and safearea things
156-
CGRect screenBounds = [UIScreen mainScreen].bounds;
157-
if (@available(iOS 11.0, *)) {
158-
CGFloat top = UIApplication.sharedApplication.keyWindow.safeAreaInsets.top;
159-
160-
if (top) {
161-
screenBounds.origin.y += top + 5;
162-
screenBounds.size.height -= top + 5;
163-
} else {
164-
screenBounds.origin.y += 20.0;
165-
screenBounds.size.height -= 20.0;
154+
- (CGSize)getWindowSize {
155+
CGSize size = CGSizeZero;
156+
157+
// Attempt to retrieve the size from the connected scenes (for modern apps)
158+
if (@available(iOS 13.0, *)) {
159+
NSSet<UIScene *> *scenes = [[UIApplication sharedApplication] connectedScenes];
160+
for (UIScene *scene in scenes) {
161+
if ([scene isKindOfClass:[UIWindowScene class]]) {
162+
UIWindowScene *windowScene = (UIWindowScene *)scene;
163+
UIWindow *window = windowScene.windows.firstObject;
164+
if (window) {
165+
size = window.bounds.size;
166+
return size; // Return immediately if we find a valid size
167+
}
168+
}
169+
}
170+
}
171+
172+
// Fallback for legacy apps using AppDelegate
173+
id<UIApplicationDelegate> appDelegate = [[UIApplication sharedApplication] delegate];
174+
if ([appDelegate respondsToSelector:@selector(window)]) {
175+
UIWindow *legacyWindow = [appDelegate performSelector:@selector(window)];
176+
if (legacyWindow) {
177+
size = legacyWindow.bounds.size;
166178
}
167-
} else {
168-
screenBounds.origin.y += 20.0;
169-
screenBounds.size.height -= 20.0;
170179
}
180+
181+
return size;
182+
}
183+
184+
- (NSString *)resolutionJson {
185+
//TODO: check why area is not clickable and safearea things
186+
CGSize size = [self getWindowSize];
171187

172-
CGFloat width = screenBounds.size.width;
173-
CGFloat height = screenBounds.size.height;
188+
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
189+
BOOL isLandscape = UIInterfaceOrientationIsLandscape(orientation);
190+
191+
CGFloat lHpW = isLandscape ? size.height : size.width;
192+
CGFloat lWpH = isLandscape ? size.width : size.height;
174193

175194
NSDictionary *resolutionDict = @{
176-
@"portrait": @{@"height": @(height), @"width": @(width)},
177-
@"landscape": @{@"height": @(width), @"width": @(height)}
195+
@"portrait": @{@"height": @(lWpH), @"width": @(lHpW)},
196+
@"landscape": @{@"height": @(lHpW), @"width": @(lWpH)}
178197
};
179198

180199
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:resolutionDict options:0 error:nil];

CountlyWebViewManager.m

-9
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,6 @@ - (void)createWebViewWithURL:(NSURL *)url
2121
UIViewController *rootViewController = UIApplication.sharedApplication.keyWindow.rootViewController;
2222
CGRect backgroundFrame = rootViewController.view.bounds;
2323

24-
if (@available(iOS 11.0, *)) {
25-
CGFloat top = UIApplication.sharedApplication.keyWindow.safeAreaInsets.top;
26-
backgroundFrame.origin.y += top ? top + 5 : 20.0;
27-
backgroundFrame.size.height -= top ? top + 5 : 20.0;
28-
} else {
29-
backgroundFrame.origin.y += 20.0;
30-
backgroundFrame.size.height -= 20.0;
31-
}
32-
3324
self.backgroundView = [[PassThroughBackgroundView alloc] initWithFrame:backgroundFrame];
3425
self.backgroundView.backgroundColor = [UIColor clearColor];
3526
[rootViewController.view addSubview:self.backgroundView];

PassThroughBackgroundView.m

+45-21
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ - (instancetype)initWithFrame:(CGRect)frame {
1717
#if (TARGET_OS_IOS)
1818
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(handleScreenChange) name:UIDeviceOrientationDidChangeNotification object:nil];
1919
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(handleScreenChange) name:UIScreenModeDidChangeNotification object:nil];
20+
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(handleScreenChange) name:UIApplicationDidBecomeActiveNotification object:nil];
2021
#endif
2122
return self;
2223
}
@@ -47,36 +48,59 @@ - (void)adjustWebViewForTraitCollection:(UITraitCollection *)traitCollection {
4748
}
4849
}
4950

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+
}
6166
}
62-
} else {
63-
screenBounds.origin.y += 20.0;
64-
screenBounds.size.height -= 20.0;
6567
}
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;
6992

7093
NSString *postMessage = [NSString stringWithFormat:
7194
@"javascript:window.postMessage({type: 'resize', width: %f, height: %f}, '*');",
7295
width,
7396
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+
}];
77102
}
78103

79-
80104
// Always remove observers when the view is deallocated
81105
- (void)dealloc {
82106
[[NSNotificationCenter defaultCenter] removeObserver:self];

0 commit comments

Comments
 (0)