-
Notifications
You must be signed in to change notification settings - Fork 266
Expand file tree
/
Copy pathOSInAppMessageView.m
More file actions
257 lines (217 loc) · 10.9 KB
/
OSInAppMessageView.m
File metadata and controls
257 lines (217 loc) · 10.9 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/**
* Modified MIT License
*
* Copyright 2017 OneSignal
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* 1. The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* 2. All copies of substantial portions of the Software may only be used in connection
* with services provided by OneSignal.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#import "OSInAppMessageView.h"
#import <WebKit/WebKit.h>
#import "OSInAppMessageClickResult.h"
#import <OneSignalUser/OneSignalUser.h>
#import "OSMacros.h"
@interface OSInAppMessageView () <UIScrollViewDelegate, WKUIDelegate, WKNavigationDelegate>
@property (strong, nonatomic, nonnull) OSInAppMessageInternal *message;
@property (strong, nonatomic, nonnull) WKWebView *webView;
@property (nonatomic) BOOL loaded;
@property (nonatomic) BOOL isFullscreen;
@end
@implementation OSInAppMessageView
- (instancetype _Nonnull)initWithMessage:(OSInAppMessageInternal *)inAppMessage withScriptMessageHandler:(id<WKScriptMessageHandler>)messageHandler {
if (self = [super init]) {
self.message = inAppMessage;
self.translatesAutoresizingMaskIntoConstraints = false;
[self setupWebviewWithMessageHandler:messageHandler];
}
return self;
}
- (NSString *)getTagsString {
NSError *error;
NSDictionary<NSString *, NSString*> *tags = [OneSignalUserManagerImpl.sharedInstance getTagsInternal];
if (tags == nil || tags.count <= 0 ) {
[OneSignalLog onesignalLog:ONE_S_LL_VERBOSE message:@"[getTagsString] no tags found for the player"];
return nil;
}
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:tags
options:NSJSONWritingPrettyPrinted
error:&error];
NSString *jsonString;
if (!jsonData) {
[OneSignalLog onesignalLog:ONE_S_LL_ERROR message:
[NSString stringWithFormat:@"Error parsing tag dictionary to json: %@", error]];
} else {
jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}
return jsonString;
}
- (NSString *)addTagsToHTML:(NSString *)html {
NSString *tags = [self getTagsString];
if (!tags) {
return html;
}
//Script to set the tags for liquid tag substitution
NSString *newHtml = [NSString stringWithFormat:@"%@ \n\n\
<script> \
setPlayerTags(%@);\
</script>",html, tags];
return newHtml;
}
- (void)loadedHtmlContent:(NSString *)html withBaseURL:(NSURL *)url {
// UI Update must be done on the main thread
NSString *taggedHTML = [self addTagsToHTML:html];
[OneSignalLog onesignalLog:ONE_S_LL_VERBOSE message:[NSString stringWithFormat:@"loadedHtmlContent with Tags: \n%@", taggedHTML]];
[self.webView loadHTMLString:taggedHTML baseURL:url];
}
- (void)setupWebviewWithMessageHandler:(id<WKScriptMessageHandler>)handler {
let configuration = [WKWebViewConfiguration new];
configuration.allowsInlineMediaPlayback = YES;
configuration.mediaTypesRequiringUserActionForPlayback = WKAudiovisualMediaTypeNone;
[configuration.userContentController addScriptMessageHandler:handler name:@"iosListener"];
CGFloat marginSpacing = [OneSignalCoreHelper sizeToScale:MESSAGE_MARGIN];
// WebView should use mainBounds as frame since we need to make sure it spans full possible screen size
// to prevent text wrapping while obtaining true height of message from JS
CGRect mainBounds = UIScreen.mainScreen.bounds;
mainBounds.size.width -= (2.0 * marginSpacing);
// Setup WebView, delegates, and disable scrolling inside of the WebView
self.webView = [[WKWebView alloc] initWithFrame:mainBounds configuration:configuration];
self.webView.backgroundColor = [UIColor clearColor];
self.webView.opaque = NO;
// https://webkit.org/blog/13936/enabling-the-inspection-of-web-content-in-apps/
if (@available(macOS 13.3, iOS 16.4, *)) {
if ([OneSignalLog getLogLevel] >= ONE_S_LL_DEBUG) {
if ([self.webView respondsToSelector:@selector(setInspectable:)]) {
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[self.webView methodSignatureForSelector:@selector(setInspectable:)]];
BOOL value = YES; // Boolean parameters must be captured as a variable before being set as an argument
[invocation setTarget:self.webView];
[invocation setSelector:@selector(setInspectable:)];
[invocation setArgument:&value atIndex:2];
[invocation invoke];
}
}
}
[self addSubview:self.webView];
[self layoutIfNeeded];
}
- (void)setIsFullscreen:(BOOL)isFullscreen {
_isFullscreen = isFullscreen;
[self setWebviewFrame];
}
- (void)setWebviewFrame {
CGRect mainBounds = UIScreen.mainScreen.bounds;
if (!self.isFullscreen) {
CGFloat marginSpacing = [OneSignalCoreHelper sizeToScale:MESSAGE_MARGIN];
mainBounds.size.width -= (2.0 * marginSpacing);
}
[self.webView setFrame:mainBounds];
}
/*
Method for resetting the height of the WebView so the JS can calculate the new height
WebView will have margins accounted for on width, but height just needs to be phone height or larger
The issue is that text wrapping can cause incorrect height issues so width is the real concern here
*/
- (void)resetWebViewToMaxBoundsAndResizeHeight:(void (^) (NSNumber *newHeight)) completion {
[self.webView removeConstraints:[self.webView constraints]];
[self setWebviewFrame];
[self.webView layoutIfNeeded];
// Evaluate JS getPageMetaData() method to obtain the updated height for the messageView to contain the webView contents
[self.webView evaluateJavaScript:OS_JS_GET_PAGE_META_DATA_METHOD completionHandler:^(NSDictionary *result, NSError *error) {
if (error) {
NSString *errorMessage = [NSString stringWithFormat:@"Javascript Method: %@ Evaluated with Error: %@", OS_JS_GET_PAGE_META_DATA_METHOD, error];
[OneSignalLog onesignalLog:ONE_S_LL_ERROR message:errorMessage];
return;
}
NSString *successMessage = [NSString stringWithFormat:@"Javascript Method: %@ Evaluated with Success: %@", OS_JS_GET_PAGE_META_DATA_METHOD, result];
[OneSignalLog onesignalLog:ONE_S_LL_VERBOSE message:successMessage];
[self setupWebViewConstraints];
// Extract the height from the result and pass it to the current messageView
NSNumber *height = [self extractHeightFromMetaDataPayload:result];
completion(height);
}];
}
- (void)updateSafeAreaInsets {
if (@available(iOS 11, *)) {
UIWindow *keyWindow = UIApplication.sharedApplication.keyWindow;
CGFloat top = keyWindow.safeAreaInsets.top;
CGFloat bottom = keyWindow.safeAreaInsets.bottom;
CGFloat right = keyWindow.safeAreaInsets.right;
CGFloat left = keyWindow.safeAreaInsets.left;
NSString *safeAreaInsetsObjectString = [NSString stringWithFormat:OS_JS_SAFE_AREA_INSETS_OBJ,top, bottom, right, left];
NSString *setInsetsString = [NSString stringWithFormat:OS_SET_SAFE_AREA_INSETS_METHOD, safeAreaInsetsObjectString];
[self.webView evaluateJavaScript:setInsetsString completionHandler:^(NSDictionary *result, NSError * _Nullable error) {
if (error) {
NSString *errorMessage = [NSString stringWithFormat:@"Javascript Method: %@ Evaluated with Error: %@", OS_SET_SAFE_AREA_INSETS_METHOD, error];
[OneSignalLog onesignalLog:ONE_S_LL_ERROR message:errorMessage];
return;
}
NSString *successMessage = [NSString stringWithFormat:@"Javascript Method: %@ Evaluated with Success: %@", OS_SET_SAFE_AREA_INSETS_METHOD, result];
[OneSignalLog onesignalLog:ONE_S_LL_VERBOSE message:successMessage];
}];
}
}
- (NSNumber *)extractHeightFromMetaDataPayload:(NSDictionary *)result {
return @([result[@"rect"][@"height"] intValue]);
}
- (void)setupWebViewConstraints {
[OneSignalLog onesignalLog:ONE_S_LL_VERBOSE message:@"Setting up In-App Message WebView Constraints"];
[self.webView removeConstraints:[self.webView constraints]];
self.webView.translatesAutoresizingMaskIntoConstraints = false;
self.webView.UIDelegate = self;
self.webView.navigationDelegate = self;
self.webView.scrollView.delegate = self;
self.webView.scrollView.scrollEnabled = false;
self.webView.layer.cornerRadius = 10.0f;
self.webView.layer.masksToBounds = true;
if (@available(iOS 11, *))
self.webView.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
[self.webView.leadingAnchor constraintEqualToAnchor:self.leadingAnchor].active = true;
[self.webView.trailingAnchor constraintEqualToAnchor:self.trailingAnchor].active = true;
[self.webView.topAnchor constraintEqualToAnchor:self.topAnchor].active = true;
[self.webView.bottomAnchor constraintEqualToAnchor:self.bottomAnchor].active = true;
[self layoutIfNeeded];
}
/*
Make sure to call this method when the message view gets dismissed
Otherwise a memory leak will occur and the entire view controller will be leaked
*/
- (void)removeScriptMessageHandler {
[self.webView.configuration.userContentController removeScriptMessageHandlerForName:@"iosListener"];
}
- (void)loadReplacementURL:(NSURL *)url {
[self.webView loadRequest:[NSURLRequest requestWithURL:url]];
}
#pragma mark WKWebViewNavigationDelegate Methods
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
// WebView finished loading
if (self.loaded)
return;
self.loaded = true;
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return nil;
}
- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView
withView:(UIView *)view {
// Disable pinch zooming
if (scrollView.pinchGestureRecognizer)
scrollView.pinchGestureRecognizer.enabled = false;
}
@end