|
@@ -134,157 +134,163 @@ static NSDictionary* customCertificatesForHost;
|
134
|
134
|
return nil;
|
135
|
135
|
}
|
136
|
136
|
|
137
|
|
-- (void)didMoveToWindow
|
|
137
|
+- (WKWebViewConfiguration *)setUpWkWebViewConfig
|
138
|
138
|
{
|
139
|
|
- if (self.window != nil && _webView == nil) {
|
140
|
|
- WKWebViewConfiguration *wkWebViewConfig = [WKWebViewConfiguration new];
|
141
|
|
- WKPreferences *prefs = [[WKPreferences alloc]init];
|
142
|
|
- BOOL _prefsUsed = NO;
|
143
|
|
- if (!_javaScriptEnabled) {
|
144
|
|
- prefs.javaScriptEnabled = NO;
|
145
|
|
- _prefsUsed = YES;
|
146
|
|
- }
|
147
|
|
- if (_allowFileAccessFromFileURLs) {
|
148
|
|
- [prefs setValue:@TRUE forKey:@"allowFileAccessFromFileURLs"];
|
149
|
|
- _prefsUsed = YES;
|
150
|
|
- }
|
151
|
|
- if (_prefsUsed) {
|
152
|
|
- wkWebViewConfig.preferences = prefs;
|
153
|
|
- }
|
154
|
|
- if (_incognito) {
|
155
|
|
- wkWebViewConfig.websiteDataStore = [WKWebsiteDataStore nonPersistentDataStore];
|
156
|
|
- } else if (_cacheEnabled) {
|
157
|
|
- wkWebViewConfig.websiteDataStore = [WKWebsiteDataStore defaultDataStore];
|
158
|
|
- }
|
159
|
|
- if(self.useSharedProcessPool) {
|
160
|
|
- wkWebViewConfig.processPool = [[RNCWKProcessPoolManager sharedManager] sharedProcessPool];
|
161
|
|
- }
|
162
|
|
- wkWebViewConfig.userContentController = [WKUserContentController new];
|
|
139
|
+ WKWebViewConfiguration *wkWebViewConfig = [WKWebViewConfiguration new];
|
|
140
|
+ WKPreferences *prefs = [[WKPreferences alloc]init];
|
|
141
|
+ BOOL _prefsUsed = NO;
|
|
142
|
+ if (!_javaScriptEnabled) {
|
|
143
|
+ prefs.javaScriptEnabled = NO;
|
|
144
|
+ _prefsUsed = YES;
|
|
145
|
+ }
|
|
146
|
+ if (_allowFileAccessFromFileURLs) {
|
|
147
|
+ [prefs setValue:@TRUE forKey:@"allowFileAccessFromFileURLs"];
|
|
148
|
+ _prefsUsed = YES;
|
|
149
|
+ }
|
|
150
|
+ if (_prefsUsed) {
|
|
151
|
+ wkWebViewConfig.preferences = prefs;
|
|
152
|
+ }
|
|
153
|
+ if (_incognito) {
|
|
154
|
+ wkWebViewConfig.websiteDataStore = [WKWebsiteDataStore nonPersistentDataStore];
|
|
155
|
+ } else if (_cacheEnabled) {
|
|
156
|
+ wkWebViewConfig.websiteDataStore = [WKWebsiteDataStore defaultDataStore];
|
|
157
|
+ }
|
|
158
|
+ if(self.useSharedProcessPool) {
|
|
159
|
+ wkWebViewConfig.processPool = [[RNCWKProcessPoolManager sharedManager] sharedProcessPool];
|
|
160
|
+ }
|
|
161
|
+ wkWebViewConfig.userContentController = [WKUserContentController new];
|
|
162
|
+
|
|
163
|
+ // Shim the HTML5 history API:
|
|
164
|
+ [wkWebViewConfig.userContentController addScriptMessageHandler:[[RNCWeakScriptMessageDelegate alloc] initWithDelegate:self]
|
|
165
|
+ name:HistoryShimName];
|
|
166
|
+ NSString *source = [NSString stringWithFormat:
|
|
167
|
+ @"(function(history) {\n"
|
|
168
|
+ " function notify(type) {\n"
|
|
169
|
+ " setTimeout(function() {\n"
|
|
170
|
+ " window.webkit.messageHandlers.%@.postMessage(type)\n"
|
|
171
|
+ " }, 0)\n"
|
|
172
|
+ " }\n"
|
|
173
|
+ " function shim(f) {\n"
|
|
174
|
+ " return function pushState() {\n"
|
|
175
|
+ " notify('other')\n"
|
|
176
|
+ " return f.apply(history, arguments)\n"
|
|
177
|
+ " }\n"
|
|
178
|
+ " }\n"
|
|
179
|
+ " history.pushState = shim(history.pushState)\n"
|
|
180
|
+ " history.replaceState = shim(history.replaceState)\n"
|
|
181
|
+ " window.addEventListener('popstate', function() {\n"
|
|
182
|
+ " notify('backforward')\n"
|
|
183
|
+ " })\n"
|
|
184
|
+ "})(window.history)\n", HistoryShimName
|
|
185
|
+ ];
|
|
186
|
+ WKUserScript *script = [[WKUserScript alloc] initWithSource:source injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:YES];
|
|
187
|
+ [wkWebViewConfig.userContentController addUserScript:script];
|
163
|
188
|
|
164
|
|
- // Shim the HTML5 history API:
|
|
189
|
+ if (_messagingEnabled) {
|
165
|
190
|
[wkWebViewConfig.userContentController addScriptMessageHandler:[[RNCWeakScriptMessageDelegate alloc] initWithDelegate:self]
|
166
|
|
- name:HistoryShimName];
|
|
191
|
+ name:MessageHandlerName];
|
|
192
|
+
|
167
|
193
|
NSString *source = [NSString stringWithFormat:
|
168
|
|
- @"(function(history) {\n"
|
169
|
|
- " function notify(type) {\n"
|
170
|
|
- " setTimeout(function() {\n"
|
171
|
|
- " window.webkit.messageHandlers.%@.postMessage(type)\n"
|
172
|
|
- " }, 0)\n"
|
173
|
|
- " }\n"
|
174
|
|
- " function shim(f) {\n"
|
175
|
|
- " return function pushState() {\n"
|
176
|
|
- " notify('other')\n"
|
177
|
|
- " return f.apply(history, arguments)\n"
|
178
|
|
- " }\n"
|
179
|
|
- " }\n"
|
180
|
|
- " history.pushState = shim(history.pushState)\n"
|
181
|
|
- " history.replaceState = shim(history.replaceState)\n"
|
182
|
|
- " window.addEventListener('popstate', function() {\n"
|
183
|
|
- " notify('backforward')\n"
|
184
|
|
- " })\n"
|
185
|
|
- "})(window.history)\n", HistoryShimName
|
|
194
|
+ @"window.%@ = {"
|
|
195
|
+ " postMessage: function (data) {"
|
|
196
|
+ " window.webkit.messageHandlers.%@.postMessage(String(data));"
|
|
197
|
+ " }"
|
|
198
|
+ "};", MessageHandlerName, MessageHandlerName
|
186
|
199
|
];
|
|
200
|
+
|
187
|
201
|
WKUserScript *script = [[WKUserScript alloc] initWithSource:source injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:YES];
|
188
|
202
|
[wkWebViewConfig.userContentController addUserScript:script];
|
189
|
|
-
|
190
|
|
- if (_messagingEnabled) {
|
191
|
|
- [wkWebViewConfig.userContentController addScriptMessageHandler:[[RNCWeakScriptMessageDelegate alloc] initWithDelegate:self]
|
192
|
|
- name:MessageHandlerName];
|
193
|
|
-
|
194
|
|
- NSString *source = [NSString stringWithFormat:
|
195
|
|
- @"window.%@ = {"
|
196
|
|
- " postMessage: function (data) {"
|
197
|
|
- " window.webkit.messageHandlers.%@.postMessage(String(data));"
|
198
|
|
- " }"
|
199
|
|
- "};", MessageHandlerName, MessageHandlerName
|
200
|
|
- ];
|
201
|
|
-
|
202
|
|
- WKUserScript *script = [[WKUserScript alloc] initWithSource:source injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:YES];
|
203
|
|
- [wkWebViewConfig.userContentController addUserScript:script];
|
204
|
|
-
|
205
|
|
- if (_injectedJavaScriptBeforeContentLoaded) {
|
206
|
|
- // If user has provided an injectedJavascript prop, execute it at the start of the document
|
207
|
|
- WKUserScript *injectedScript = [[WKUserScript alloc] initWithSource:_injectedJavaScriptBeforeContentLoaded injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:YES];
|
208
|
|
- [wkWebViewConfig.userContentController addUserScript:injectedScript];
|
209
|
|
- }
|
|
203
|
+
|
|
204
|
+ if (_injectedJavaScriptBeforeContentLoaded) {
|
|
205
|
+ // If user has provided an injectedJavascript prop, execute it at the start of the document
|
|
206
|
+ WKUserScript *injectedScript = [[WKUserScript alloc] initWithSource:_injectedJavaScriptBeforeContentLoaded injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:YES];
|
|
207
|
+ [wkWebViewConfig.userContentController addUserScript:injectedScript];
|
210
|
208
|
}
|
|
209
|
+ }
|
211
|
210
|
|
212
|
|
- wkWebViewConfig.allowsInlineMediaPlayback = _allowsInlineMediaPlayback;
|
|
211
|
+ wkWebViewConfig.allowsInlineMediaPlayback = _allowsInlineMediaPlayback;
|
213
|
212
|
#if WEBKIT_IOS_10_APIS_AVAILABLE
|
214
|
|
- wkWebViewConfig.mediaTypesRequiringUserActionForPlayback = _mediaPlaybackRequiresUserAction
|
215
|
|
- ? WKAudiovisualMediaTypeAll
|
216
|
|
- : WKAudiovisualMediaTypeNone;
|
217
|
|
- wkWebViewConfig.dataDetectorTypes = _dataDetectorTypes;
|
|
213
|
+ wkWebViewConfig.mediaTypesRequiringUserActionForPlayback = _mediaPlaybackRequiresUserAction
|
|
214
|
+ ? WKAudiovisualMediaTypeAll
|
|
215
|
+ : WKAudiovisualMediaTypeNone;
|
|
216
|
+ wkWebViewConfig.dataDetectorTypes = _dataDetectorTypes;
|
218
|
217
|
#else
|
219
|
|
- wkWebViewConfig.mediaPlaybackRequiresUserAction = _mediaPlaybackRequiresUserAction;
|
|
218
|
+ wkWebViewConfig.mediaPlaybackRequiresUserAction = _mediaPlaybackRequiresUserAction;
|
220
|
219
|
#endif
|
221
|
220
|
|
222
|
|
- if (_applicationNameForUserAgent) {
|
223
|
|
- wkWebViewConfig.applicationNameForUserAgent = [NSString stringWithFormat:@"%@ %@", wkWebViewConfig.applicationNameForUserAgent, _applicationNameForUserAgent];
|
224
|
|
- }
|
|
221
|
+ if (_applicationNameForUserAgent) {
|
|
222
|
+ wkWebViewConfig.applicationNameForUserAgent = [NSString stringWithFormat:@"%@ %@", wkWebViewConfig.applicationNameForUserAgent, _applicationNameForUserAgent];
|
|
223
|
+ }
|
225
|
224
|
|
226
|
|
- if(_sharedCookiesEnabled) {
|
227
|
|
- // More info to sending cookies with WKWebView
|
228
|
|
- // https://stackoverflow.com/questions/26573137/can-i-set-the-cookies-to-be-used-by-a-wkwebview/26577303#26577303
|
229
|
|
- if (@available(iOS 11.0, *)) {
|
230
|
|
- // Set Cookies in iOS 11 and above, initialize websiteDataStore before setting cookies
|
231
|
|
- // See also https://forums.developer.apple.com/thread/97194
|
232
|
|
- // check if websiteDataStore has not been initialized before
|
233
|
|
- if(!_incognito && !_cacheEnabled) {
|
234
|
|
- wkWebViewConfig.websiteDataStore = [WKWebsiteDataStore nonPersistentDataStore];
|
235
|
|
- }
|
236
|
|
- for (NSHTTPCookie *cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]) {
|
237
|
|
- [wkWebViewConfig.websiteDataStore.httpCookieStore setCookie:cookie completionHandler:nil];
|
|
225
|
+ if(_sharedCookiesEnabled) {
|
|
226
|
+ // More info to sending cookies with WKWebView
|
|
227
|
+ // https://stackoverflow.com/questions/26573137/can-i-set-the-cookies-to-be-used-by-a-wkwebview/26577303#26577303
|
|
228
|
+ if (@available(iOS 11.0, *)) {
|
|
229
|
+ // Set Cookies in iOS 11 and above, initialize websiteDataStore before setting cookies
|
|
230
|
+ // See also https://forums.developer.apple.com/thread/97194
|
|
231
|
+ // check if websiteDataStore has not been initialized before
|
|
232
|
+ if(!_incognito && !_cacheEnabled) {
|
|
233
|
+ wkWebViewConfig.websiteDataStore = [WKWebsiteDataStore nonPersistentDataStore];
|
|
234
|
+ }
|
|
235
|
+ for (NSHTTPCookie *cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]) {
|
|
236
|
+ [wkWebViewConfig.websiteDataStore.httpCookieStore setCookie:cookie completionHandler:nil];
|
|
237
|
+ }
|
|
238
|
+ } else {
|
|
239
|
+ NSMutableString *script = [NSMutableString string];
|
|
240
|
+
|
|
241
|
+ // Clear all existing cookies in a direct called function. This ensures that no
|
|
242
|
+ // javascript error will break the web content javascript.
|
|
243
|
+ // We keep this code here, if someone requires that Cookies are also removed within the
|
|
244
|
+ // the WebView and want to extends the current sharedCookiesEnabled option with an
|
|
245
|
+ // additional property.
|
|
246
|
+ // Generates JS: document.cookie = "key=; Expires=Thu, 01 Jan 1970 00:00:01 GMT;"
|
|
247
|
+ // for each cookie which is already available in the WebView context.
|
|
248
|
+ /*
|
|
249
|
+ [script appendString:@"(function () {\n"];
|
|
250
|
+ [script appendString:@" var cookies = document.cookie.split('; ');\n"];
|
|
251
|
+ [script appendString:@" for (var i = 0; i < cookies.length; i++) {\n"];
|
|
252
|
+ [script appendString:@" if (cookies[i].indexOf('=') !== -1) {\n"];
|
|
253
|
+ [script appendString:@" document.cookie = cookies[i].split('=')[0] + '=; Expires=Thu, 01 Jan 1970 00:00:01 GMT';\n"];
|
|
254
|
+ [script appendString:@" }\n"];
|
|
255
|
+ [script appendString:@" }\n"];
|
|
256
|
+ [script appendString:@"})();\n\n"];
|
|
257
|
+ */
|
|
258
|
+
|
|
259
|
+ // Set cookies in a direct called function. This ensures that no
|
|
260
|
+ // javascript error will break the web content javascript.
|
|
261
|
+ // Generates JS: document.cookie = "key=value; Path=/; Expires=Thu, 01 Jan 20xx 00:00:01 GMT;"
|
|
262
|
+ // for each cookie which is available in the application context.
|
|
263
|
+ [script appendString:@"(function () {\n"];
|
|
264
|
+ for (NSHTTPCookie *cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]) {
|
|
265
|
+ [script appendFormat:@"document.cookie = %@ + '=' + %@",
|
|
266
|
+ RCTJSONStringify(cookie.name, NULL),
|
|
267
|
+ RCTJSONStringify(cookie.value, NULL)];
|
|
268
|
+ if (cookie.path) {
|
|
269
|
+ [script appendFormat:@" + '; Path=' + %@", RCTJSONStringify(cookie.path, NULL)];
|
238
|
270
|
}
|
239
|
|
- } else {
|
240
|
|
- NSMutableString *script = [NSMutableString string];
|
241
|
|
-
|
242
|
|
- // Clear all existing cookies in a direct called function. This ensures that no
|
243
|
|
- // javascript error will break the web content javascript.
|
244
|
|
- // We keep this code here, if someone requires that Cookies are also removed within the
|
245
|
|
- // the WebView and want to extends the current sharedCookiesEnabled option with an
|
246
|
|
- // additional property.
|
247
|
|
- // Generates JS: document.cookie = "key=; Expires=Thu, 01 Jan 1970 00:00:01 GMT;"
|
248
|
|
- // for each cookie which is already available in the WebView context.
|
249
|
|
- /*
|
250
|
|
- [script appendString:@"(function () {\n"];
|
251
|
|
- [script appendString:@" var cookies = document.cookie.split('; ');\n"];
|
252
|
|
- [script appendString:@" for (var i = 0; i < cookies.length; i++) {\n"];
|
253
|
|
- [script appendString:@" if (cookies[i].indexOf('=') !== -1) {\n"];
|
254
|
|
- [script appendString:@" document.cookie = cookies[i].split('=')[0] + '=; Expires=Thu, 01 Jan 1970 00:00:01 GMT';\n"];
|
255
|
|
- [script appendString:@" }\n"];
|
256
|
|
- [script appendString:@" }\n"];
|
257
|
|
- [script appendString:@"})();\n\n"];
|
258
|
|
- */
|
259
|
|
-
|
260
|
|
- // Set cookies in a direct called function. This ensures that no
|
261
|
|
- // javascript error will break the web content javascript.
|
262
|
|
- // Generates JS: document.cookie = "key=value; Path=/; Expires=Thu, 01 Jan 20xx 00:00:01 GMT;"
|
263
|
|
- // for each cookie which is available in the application context.
|
264
|
|
- [script appendString:@"(function () {\n"];
|
265
|
|
- for (NSHTTPCookie *cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]) {
|
266
|
|
- [script appendFormat:@"document.cookie = %@ + '=' + %@",
|
267
|
|
- RCTJSONStringify(cookie.name, NULL),
|
268
|
|
- RCTJSONStringify(cookie.value, NULL)];
|
269
|
|
- if (cookie.path) {
|
270
|
|
- [script appendFormat:@" + '; Path=' + %@", RCTJSONStringify(cookie.path, NULL)];
|
271
|
|
- }
|
272
|
|
- if (cookie.expiresDate) {
|
273
|
|
- [script appendFormat:@" + '; Expires=' + new Date(%f).toUTCString()",
|
274
|
|
- cookie.expiresDate.timeIntervalSince1970 * 1000
|
275
|
|
- ];
|
276
|
|
- }
|
277
|
|
- [script appendString:@";\n"];
|
|
271
|
+ if (cookie.expiresDate) {
|
|
272
|
+ [script appendFormat:@" + '; Expires=' + new Date(%f).toUTCString()",
|
|
273
|
+ cookie.expiresDate.timeIntervalSince1970 * 1000
|
|
274
|
+ ];
|
278
|
275
|
}
|
279
|
|
- [script appendString:@"})();\n"];
|
280
|
|
-
|
281
|
|
- WKUserScript* cookieInScript = [[WKUserScript alloc] initWithSource:script
|
282
|
|
- injectionTime:WKUserScriptInjectionTimeAtDocumentStart
|
283
|
|
- forMainFrameOnly:YES];
|
284
|
|
- [wkWebViewConfig.userContentController addUserScript:cookieInScript];
|
|
276
|
+ [script appendString:@";\n"];
|
285
|
277
|
}
|
|
278
|
+ [script appendString:@"})();\n"];
|
|
279
|
+
|
|
280
|
+ WKUserScript* cookieInScript = [[WKUserScript alloc] initWithSource:script
|
|
281
|
+ injectionTime:WKUserScriptInjectionTimeAtDocumentStart
|
|
282
|
+ forMainFrameOnly:YES];
|
|
283
|
+ [wkWebViewConfig.userContentController addUserScript:cookieInScript];
|
286
|
284
|
}
|
|
285
|
+ }
|
|
286
|
+
|
|
287
|
+ return wkWebViewConfig;
|
|
288
|
+}
|
287
|
289
|
|
|
290
|
+- (void)didMoveToWindow
|
|
291
|
+{
|
|
292
|
+ if (self.window != nil && _webView == nil) {
|
|
293
|
+ WKWebViewConfiguration *wkWebViewConfig = [self setUpWkWebViewConfig];
|
288
|
294
|
_webView = [[WKWebView alloc] initWithFrame:self.bounds configuration: wkWebViewConfig];
|
289
|
295
|
[self setBackgroundColor: _savedBackgroundColor];
|
290
|
296
|
_webView.scrollView.delegate = self;
|