-
Notifications
You must be signed in to change notification settings - Fork 242
Expand file tree
/
Copy pathCountlyWebViewController.m
More file actions
171 lines (143 loc) · 4.62 KB
/
CountlyWebViewController.m
File metadata and controls
171 lines (143 loc) · 4.62 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// CountlyWebViewController.m
//
// This code is provided under the MIT License.
//
// Please visit www.count.ly for more information.
//
#import "CountlyWebViewController.h"
#import "PassThroughBackgroundView.h"
#import "TouchDelegatingView.h"
#if (TARGET_OS_IOS)
@implementation CountlyWebViewController
{
UIStatusBarStyle _cachedStatusBarStyle;
BOOL _hasCachedStatusBarStyle;
}
- (BOOL)prefersStatusBarHidden
{
return CountlyContentBuilderInternal.sharedInstance.webViewDisplayOption == IMMERSIVE ? YES : NO;
}
- (BOOL)prefersHomeIndicatorAutoHidden
{
return CountlyContentBuilderInternal.sharedInstance.webViewDisplayOption == IMMERSIVE ? YES : NO;
}
- (UIStatusBarStyle)preferredStatusBarStyle
{
if (_hasCachedStatusBarStyle) {
return _cachedStatusBarStyle;
}
UIWindow *keyWindow = [self getKeyWindow];
if (keyWindow && keyWindow.rootViewController) {
_cachedStatusBarStyle = keyWindow.rootViewController.preferredStatusBarStyle;
_hasCachedStatusBarStyle = YES;
return _cachedStatusBarStyle;
}
return UIStatusBarStyleLightContent;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAll;
}
- (UIWindow *)getKeyWindow {
if (@available(iOS 13.0, *)) {
for (UIScene *scene in UIApplication.sharedApplication.connectedScenes) {
if (![scene isKindOfClass:[UIWindowScene class]]) {
continue;
}
UIWindowScene *windowScene = (UIWindowScene *)scene;
if (windowScene.activationState == UISceneActivationStateForegroundActive) {
for (UIWindow *window in windowScene.windows) {
if (window.isKeyWindow) {
return window;
}
}
}
}
} else {
return UIApplication.sharedApplication.keyWindow;
}
return nil;
}
- (BOOL)shouldAutorotate
{
return YES;
}
- (void)loadView
{
UIWindow *keyWindow = [self getKeyWindow];
CGRect bounds = keyWindow.rootViewController.view.bounds;
if (CGRectIsEmpty(bounds)) {
bounds = UIScreen.mainScreen.bounds;
}
self.view = [[TouchDelegatingView alloc] initWithFrame:bounds];
}
- (void)viewDidLoad
{
[super viewDidLoad];
UIWindow *keyWindow = [self getKeyWindow];
if (!_hasCachedStatusBarStyle) {
if (keyWindow && keyWindow.rootViewController) {
_cachedStatusBarStyle = keyWindow.rootViewController.preferredStatusBarStyle;
_hasCachedStatusBarStyle = YES;
}
}
if ([self.view isKindOfClass:[TouchDelegatingView class]])
{
TouchDelegatingView *delegatingView = (TouchDelegatingView *)self.view;
if (keyWindow && keyWindow.rootViewController) {
delegatingView.touchDelegate = keyWindow.rootViewController.view;
}
}
// Fully transparent controller background
self.view.backgroundColor = [UIColor clearColor];
if (@available(iOS 11.0, *))
{
self.view.insetsLayoutMarginsFromSafeArea = NO;
self.view.directionalLayoutMargins = NSDirectionalEdgeInsetsZero;
}
self.extendedLayoutIncludesOpaqueBars = YES;
self.edgesForExtendedLayout = UIRectEdgeAll;
// Ensure underlying app stays visible
self.view.opaque = NO;
if (self.contentView)
{
self.contentView.frame = self.view.bounds;
self.contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:self.contentView];
}
}
- (void)updatePlacementRespectToSafeAreas
{
if (@available(iOS 13.0, *))
{
UIEdgeInsets safeArea = self.view.safeAreaInsets;
UIInterfaceOrientation orientation = self.view.window.windowScene.interfaceOrientation;
if ([self.contentView isKindOfClass:PassThroughBackgroundView.self])
{
PassThroughBackgroundView *content = (PassThroughBackgroundView *)self.contentView;
CGRect frame = content.webView.frame;
if (CountlyContentBuilderInternal.sharedInstance.webViewDisplayOption == SAFE_AREA || [self hasTopNotch:safeArea])
{
frame.origin.y += safeArea.top; // always respect notch if exists
}
if (orientation != UIInterfaceOrientationLandscapeLeft)
{ // regardless of given safe area, if notch is in left act for it
frame.origin.x += MAX(safeArea.left, safeArea.right);
}
content.webView.frame = frame;
}
}
}
- (bool)hasTopNotch:(UIEdgeInsets)safeArea
{
if (@available(iOS 11.0, *))
{
return safeArea.top >= 44;
}
else
{
return NO;
}
}
@end
#endif