cordova-plugin-inappbrowser-patch 6.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (33) hide show
  1. package/CONTRIBUTING.md +37 -0
  2. package/LICENSE +202 -0
  3. package/NOTICE +5 -0
  4. package/README.md +722 -0
  5. package/RELEASENOTES.md +801 -0
  6. package/package.json +60 -0
  7. package/plugin.xml +103 -0
  8. package/src/android/InAppBrowser.java +1503 -0
  9. package/src/android/InAppBrowserDialog.java +57 -0
  10. package/src/android/InAppChromeClient.java +190 -0
  11. package/src/android/res/drawable-hdpi/ic_action_next_item.png +0 -0
  12. package/src/android/res/drawable-hdpi/ic_action_previous_item.png +0 -0
  13. package/src/android/res/drawable-hdpi/ic_action_remove.png +0 -0
  14. package/src/android/res/drawable-mdpi/ic_action_next_item.png +0 -0
  15. package/src/android/res/drawable-mdpi/ic_action_previous_item.png +0 -0
  16. package/src/android/res/drawable-mdpi/ic_action_remove.png +0 -0
  17. package/src/android/res/drawable-xhdpi/ic_action_next_item.png +0 -0
  18. package/src/android/res/drawable-xhdpi/ic_action_previous_item.png +0 -0
  19. package/src/android/res/drawable-xhdpi/ic_action_remove.png +0 -0
  20. package/src/android/res/drawable-xxhdpi/ic_action_next_item.png +0 -0
  21. package/src/android/res/drawable-xxhdpi/ic_action_previous_item.png +0 -0
  22. package/src/android/res/drawable-xxhdpi/ic_action_remove.png +0 -0
  23. package/src/browser/InAppBrowserProxy.js +245 -0
  24. package/src/ios/CDVInAppBrowserNavigationController.h +28 -0
  25. package/src/ios/CDVInAppBrowserNavigationController.m +63 -0
  26. package/src/ios/CDVInAppBrowserOptions.h +51 -0
  27. package/src/ios/CDVInAppBrowserOptions.m +90 -0
  28. package/src/ios/CDVWKInAppBrowser.h +80 -0
  29. package/src/ios/CDVWKInAppBrowser.m +1223 -0
  30. package/src/ios/CDVWKInAppBrowserUIDelegate.h +32 -0
  31. package/src/ios/CDVWKInAppBrowserUIDelegate.m +127 -0
  32. package/types/index.d.ts +109 -0
  33. package/www/inappbrowser.js +124 -0
@@ -0,0 +1,1223 @@
1
+ /*
2
+ Licensed to the Apache Software Foundation (ASF) under one
3
+ or more contributor license agreements. See the NOTICE file
4
+ distributed with this work for additional information
5
+ regarding copyright ownership. The ASF licenses this file
6
+ to you under the Apache License, Version 2.0 (the
7
+ "License"); you may not use this file except in compliance
8
+ with the License. You may obtain a copy of the License at
9
+
10
+ http://www.apache.org/licenses/LICENSE-2.0
11
+
12
+ Unless required by applicable law or agreed to in writing,
13
+ software distributed under the License is distributed on an
14
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
+ KIND, either express or implied. See the License for the
16
+ specific language governing permissions and limitations
17
+ under the License.
18
+ */
19
+
20
+ #import "CDVWKInAppBrowser.h"
21
+ #import <Cordova/NSDictionary+CordovaPreferences.h>
22
+ #import <Cordova/CDVWebViewProcessPoolFactory.h>
23
+ #import <Cordova/CDVPluginResult.h>
24
+
25
+ #define kInAppBrowserTargetSelf @"_self"
26
+ #define kInAppBrowserTargetSystem @"_system"
27
+ #define kInAppBrowserTargetBlank @"_blank"
28
+
29
+ #define kInAppBrowserToolbarBarPositionBottom @"bottom"
30
+ #define kInAppBrowserToolbarBarPositionTop @"top"
31
+
32
+ #define IAB_BRIDGE_NAME @"cordova_iab"
33
+
34
+ #define TOOLBAR_HEIGHT 44.0
35
+ #define LOCATIONBAR_HEIGHT 21.0
36
+ #define FOOTER_HEIGHT ((TOOLBAR_HEIGHT) + (LOCATIONBAR_HEIGHT))
37
+
38
+ #pragma mark CDVWKInAppBrowser
39
+
40
+ @implementation CDVWKInAppBrowser
41
+
42
+ static CDVWKInAppBrowser* instance = nil;
43
+
44
+ + (id) getInstance{
45
+ return instance;
46
+ }
47
+
48
+ - (void)pluginInitialize
49
+ {
50
+ instance = self;
51
+ _callbackIdPattern = nil;
52
+ _beforeload = @"";
53
+ _waitForBeforeload = NO;
54
+ }
55
+
56
+ - (void)onReset
57
+ {
58
+ [self close:nil];
59
+ }
60
+
61
+ - (void)close:(CDVInvokedUrlCommand*)command
62
+ {
63
+ if (self.inAppBrowserViewController == nil) {
64
+ NSLog(@"IAB.close() called but it was already closed.");
65
+ return;
66
+ }
67
+
68
+ // Things are cleaned up in browserExit.
69
+ [self.inAppBrowserViewController close];
70
+ }
71
+
72
+ - (BOOL) isSystemUrl:(NSURL*)url
73
+ {
74
+ if ([[url host] isEqualToString:@"itunes.apple.com"]) {
75
+ return YES;
76
+ }
77
+
78
+ return NO;
79
+ }
80
+
81
+ - (void)open:(CDVInvokedUrlCommand*)command
82
+ {
83
+ CDVPluginResult* pluginResult;
84
+
85
+ NSString* url = [command argumentAtIndex:0];
86
+ NSString* target = [command argumentAtIndex:1 withDefault:kInAppBrowserTargetSelf];
87
+ NSString* options = [command argumentAtIndex:2 withDefault:@"" andClass:[NSString class]];
88
+
89
+ self.callbackId = command.callbackId;
90
+
91
+ if (url != nil) {
92
+ NSURL* baseUrl = [self.webViewEngine URL];
93
+ NSURL* absoluteUrl = [[NSURL URLWithString:url relativeToURL:baseUrl] absoluteURL];
94
+
95
+ if ([self isSystemUrl:absoluteUrl]) {
96
+ target = kInAppBrowserTargetSystem;
97
+ }
98
+
99
+ if ([target isEqualToString:kInAppBrowserTargetSelf]) {
100
+ [self openInCordovaWebView:absoluteUrl withOptions:options];
101
+ } else if ([target isEqualToString:kInAppBrowserTargetSystem]) {
102
+ [self openInSystem:absoluteUrl];
103
+ } else { // _blank or anything else
104
+ [self openInInAppBrowser:absoluteUrl withOptions:options];
105
+ }
106
+
107
+ pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
108
+ } else {
109
+ pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"incorrect number of arguments"];
110
+ }
111
+
112
+ [pluginResult setKeepCallback:[NSNumber numberWithBool:YES]];
113
+ [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
114
+ }
115
+
116
+ - (void)openInInAppBrowser:(NSURL*)url withOptions:(NSString*)options
117
+ {
118
+ CDVInAppBrowserOptions* browserOptions = [CDVInAppBrowserOptions parseOptions:options];
119
+
120
+ WKWebsiteDataStore* dataStore = [WKWebsiteDataStore defaultDataStore];
121
+ if (browserOptions.cleardata) {
122
+
123
+ NSDate* dateFrom = [NSDate dateWithTimeIntervalSince1970:0];
124
+ [dataStore removeDataOfTypes:[WKWebsiteDataStore allWebsiteDataTypes] modifiedSince:dateFrom completionHandler:^{
125
+ NSLog(@"Removed all WKWebView data");
126
+ self.inAppBrowserViewController.webView.configuration.processPool = [[WKProcessPool alloc] init]; // create new process pool to flush all data
127
+ }];
128
+ }
129
+
130
+ if (browserOptions.clearcache) {
131
+ // Deletes all cookies
132
+ WKHTTPCookieStore* cookieStore = dataStore.httpCookieStore;
133
+ [cookieStore getAllCookies:^(NSArray* cookies) {
134
+ NSHTTPCookie* cookie;
135
+ for(cookie in cookies){
136
+ [cookieStore deleteCookie:cookie completionHandler:nil];
137
+ }
138
+ }];
139
+ }
140
+
141
+ if (browserOptions.clearsessioncache) {
142
+ // Deletes session cookies
143
+ WKHTTPCookieStore* cookieStore = dataStore.httpCookieStore;
144
+ [cookieStore getAllCookies:^(NSArray* cookies) {
145
+ NSHTTPCookie* cookie;
146
+ for(cookie in cookies){
147
+ if(cookie.sessionOnly){
148
+ [cookieStore deleteCookie:cookie completionHandler:nil];
149
+ }
150
+ }
151
+ }];
152
+ }
153
+
154
+ if (self.inAppBrowserViewController == nil) {
155
+ self.inAppBrowserViewController = [[CDVWKInAppBrowserViewController alloc] initWithBrowserOptions: browserOptions andSettings:self.commandDelegate.settings];
156
+ self.inAppBrowserViewController.navigationDelegate = self;
157
+
158
+ if ([self.viewController conformsToProtocol:@protocol(CDVScreenOrientationDelegate)]) {
159
+ self.inAppBrowserViewController.orientationDelegate = (UIViewController <CDVScreenOrientationDelegate>*)self.viewController;
160
+ }
161
+ }
162
+
163
+ [self.inAppBrowserViewController showLocationBar:browserOptions.location];
164
+ [self.inAppBrowserViewController showToolBar:browserOptions.toolbar :browserOptions.toolbarposition];
165
+ if (browserOptions.closebuttoncaption != nil || browserOptions.closebuttoncolor != nil) {
166
+ int closeButtonIndex = browserOptions.lefttoright ? (browserOptions.hidenavigationbuttons ? 1 : 4) : 0;
167
+ [self.inAppBrowserViewController setCloseButtonTitle:browserOptions.closebuttoncaption :browserOptions.closebuttoncolor :closeButtonIndex];
168
+ }
169
+ // Set Presentation Style
170
+ UIModalPresentationStyle presentationStyle = UIModalPresentationFullScreen; // default
171
+ if (browserOptions.presentationstyle != nil) {
172
+ if ([[browserOptions.presentationstyle lowercaseString] isEqualToString:@"pagesheet"]) {
173
+ presentationStyle = UIModalPresentationPageSheet;
174
+ } else if ([[browserOptions.presentationstyle lowercaseString] isEqualToString:@"formsheet"]) {
175
+ presentationStyle = UIModalPresentationFormSheet;
176
+ }
177
+ }
178
+ self.inAppBrowserViewController.modalPresentationStyle = presentationStyle;
179
+
180
+ // Set Transition Style
181
+ UIModalTransitionStyle transitionStyle = UIModalTransitionStyleCoverVertical; // default
182
+ if (browserOptions.transitionstyle != nil) {
183
+ if ([[browserOptions.transitionstyle lowercaseString] isEqualToString:@"fliphorizontal"]) {
184
+ transitionStyle = UIModalTransitionStyleFlipHorizontal;
185
+ } else if ([[browserOptions.transitionstyle lowercaseString] isEqualToString:@"crossdissolve"]) {
186
+ transitionStyle = UIModalTransitionStyleCrossDissolve;
187
+ }
188
+ }
189
+ self.inAppBrowserViewController.modalTransitionStyle = transitionStyle;
190
+
191
+ //prevent webView from bouncing
192
+ if (browserOptions.disallowoverscroll) {
193
+ if ([self.inAppBrowserViewController.webView respondsToSelector:@selector(scrollView)]) {
194
+ ((UIScrollView*)[self.inAppBrowserViewController.webView scrollView]).bounces = NO;
195
+ } else {
196
+ for (id subview in self.inAppBrowserViewController.webView.subviews) {
197
+ if ([[subview class] isSubclassOfClass:[UIScrollView class]]) {
198
+ ((UIScrollView*)subview).bounces = NO;
199
+ }
200
+ }
201
+ }
202
+ }
203
+
204
+ // use of beforeload event
205
+ if([browserOptions.beforeload isKindOfClass:[NSString class]]){
206
+ _beforeload = browserOptions.beforeload;
207
+ }else{
208
+ _beforeload = @"yes";
209
+ }
210
+ _waitForBeforeload = ![_beforeload isEqualToString:@""];
211
+
212
+ [self.inAppBrowserViewController navigateTo:url];
213
+ if (!browserOptions.hidden) {
214
+ [self show:nil withNoAnimate:browserOptions.hidden];
215
+ }
216
+ }
217
+
218
+ - (void)show:(CDVInvokedUrlCommand*)command{
219
+ [self show:command withNoAnimate:NO];
220
+ }
221
+
222
+ - (void)show:(CDVInvokedUrlCommand*)command withNoAnimate:(BOOL)noAnimate
223
+ {
224
+ BOOL initHidden = NO;
225
+ if(command == nil && noAnimate == YES){
226
+ initHidden = YES;
227
+ }
228
+
229
+ if (self.inAppBrowserViewController == nil) {
230
+ NSLog(@"Tried to show IAB after it was closed.");
231
+ return;
232
+ }
233
+
234
+ __block CDVInAppBrowserNavigationController* nav = [[CDVInAppBrowserNavigationController alloc]
235
+ initWithRootViewController:self.inAppBrowserViewController];
236
+ nav.orientationDelegate = self.inAppBrowserViewController;
237
+ nav.navigationBarHidden = YES;
238
+ nav.modalPresentationStyle = self.inAppBrowserViewController.modalPresentationStyle;
239
+ nav.presentationController.delegate = self.inAppBrowserViewController;
240
+
241
+ __weak CDVWKInAppBrowser* weakSelf = self;
242
+
243
+ // Run later to avoid the "took a long time" log message.
244
+ dispatch_async(dispatch_get_main_queue(), ^{
245
+ if (weakSelf.inAppBrowserViewController != nil) {
246
+ float osVersion = [[[UIDevice currentDevice] systemVersion] floatValue];
247
+ __strong __typeof(weakSelf) strongSelf = weakSelf;
248
+ if (!strongSelf->tmpWindow) {
249
+ CGRect frame = [[UIScreen mainScreen] bounds];
250
+ if(initHidden && osVersion < 11){
251
+ frame.origin.x = -10000;
252
+ }
253
+ strongSelf->tmpWindow = [[UIWindow alloc] initWithFrame:frame];
254
+ }
255
+ UIViewController *tmpController = [[UIViewController alloc] init];
256
+ [strongSelf->tmpWindow setRootViewController:tmpController];
257
+ [strongSelf->tmpWindow setWindowLevel:UIWindowLevelNormal];
258
+
259
+ if(!initHidden || osVersion < 11){
260
+ [self->tmpWindow makeKeyAndVisible];
261
+ }
262
+ [tmpController presentViewController:nav animated:!noAnimate completion:nil];
263
+ }
264
+ });
265
+ }
266
+
267
+ - (void)hide:(CDVInvokedUrlCommand*)command
268
+ {
269
+ // Set tmpWindow to hidden to make main webview responsive to touch again
270
+ // https://stackoverflow.com/questions/4544489/how-to-remove-a-uiwindow
271
+ self->tmpWindow.hidden = YES;
272
+ self->tmpWindow = nil;
273
+
274
+ if (self.inAppBrowserViewController == nil) {
275
+ NSLog(@"Tried to hide IAB after it was closed.");
276
+ return;
277
+
278
+
279
+ }
280
+
281
+ // Run later to avoid the "took a long time" log message.
282
+ dispatch_async(dispatch_get_main_queue(), ^{
283
+ if (self.inAppBrowserViewController != nil) {
284
+ [self.inAppBrowserViewController.presentingViewController dismissViewControllerAnimated:YES completion:nil];
285
+ }
286
+ });
287
+ }
288
+
289
+ - (void)openInCordovaWebView:(NSURL*)url withOptions:(NSString*)options
290
+ {
291
+ NSURLRequest* request = [NSURLRequest requestWithURL:url];
292
+ // the webview engine itself will filter for this according to <allow-navigation> policy
293
+ // in config.xml
294
+ [self.webViewEngine loadRequest:request];
295
+ }
296
+
297
+ - (void)openInSystem:(NSURL*)url
298
+ {
299
+ [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:^(BOOL success) {
300
+ if (!success) {
301
+ [[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:CDVPluginHandleOpenURLNotification object:url]];
302
+ }
303
+ }];
304
+ }
305
+
306
+ - (void)loadAfterBeforeload:(CDVInvokedUrlCommand*)command
307
+ {
308
+ NSString* urlStr = [command argumentAtIndex:0];
309
+
310
+ if ([_beforeload isEqualToString:@""]) {
311
+ NSLog(@"unexpected loadAfterBeforeload called without feature beforeload=get|post");
312
+ }
313
+ if (self.inAppBrowserViewController == nil) {
314
+ NSLog(@"Tried to invoke loadAfterBeforeload on IAB after it was closed.");
315
+ return;
316
+ }
317
+ if (urlStr == nil) {
318
+ NSLog(@"loadAfterBeforeload called with nil argument, ignoring.");
319
+ return;
320
+ }
321
+
322
+ NSURL* url = [NSURL URLWithString:urlStr];
323
+ //_beforeload = @"";
324
+ _waitForBeforeload = NO;
325
+ [self.inAppBrowserViewController navigateTo:url];
326
+ }
327
+
328
+ // This is a helper method for the inject{Script|Style}{Code|File} API calls, which
329
+ // provides a consistent method for injecting JavaScript code into the document.
330
+ //
331
+ // If a wrapper string is supplied, then the source string will be JSON-encoded (adding
332
+ // quotes) and wrapped using string formatting. (The wrapper string should have a single
333
+ // '%@' marker).
334
+ //
335
+ // If no wrapper is supplied, then the source string is executed directly.
336
+
337
+ - (void)injectDeferredObject:(NSString*)source withWrapper:(NSString*)jsWrapper
338
+ {
339
+ // Ensure a message handler bridge is created to communicate with the CDVWKInAppBrowserViewController
340
+ [self evaluateJavaScript: [NSString stringWithFormat:@"(function(w){if(!w._cdvMessageHandler) {w._cdvMessageHandler = function(id,d){w.webkit.messageHandlers.%@.postMessage({d:d, id:id});}}})(window)", IAB_BRIDGE_NAME]];
341
+
342
+ if (jsWrapper != nil) {
343
+ NSData* jsonData = [NSJSONSerialization dataWithJSONObject:@[source] options:0 error:nil];
344
+ NSString* sourceArrayString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
345
+ if (sourceArrayString) {
346
+ NSString* sourceString = [sourceArrayString substringWithRange:NSMakeRange(1, [sourceArrayString length] - 2)];
347
+ NSString* jsToInject = [NSString stringWithFormat:jsWrapper, sourceString];
348
+ [self evaluateJavaScript:jsToInject];
349
+ }
350
+ } else {
351
+ [self evaluateJavaScript:source];
352
+ }
353
+ }
354
+
355
+
356
+ //Synchronus helper for javascript evaluation
357
+ - (void)evaluateJavaScript:(NSString *)script {
358
+ __block NSString* _script = script;
359
+ [self.inAppBrowserViewController.webView evaluateJavaScript:script completionHandler:^(id result, NSError *error) {
360
+ if (error == nil) {
361
+ if (result != nil) {
362
+ NSLog(@"%@", result);
363
+ }
364
+ } else {
365
+ NSLog(@"evaluateJavaScript error : %@ : %@", error.localizedDescription, _script);
366
+ }
367
+ }];
368
+ }
369
+
370
+ - (void)injectScriptCode:(CDVInvokedUrlCommand*)command
371
+ {
372
+ NSString* jsWrapper = nil;
373
+
374
+ if ((command.callbackId != nil) && ![command.callbackId isEqualToString:@"INVALID"]) {
375
+ jsWrapper = [NSString stringWithFormat:@"_cdvMessageHandler('%@',JSON.stringify([eval(%%@)]));", command.callbackId];
376
+ }
377
+ [self injectDeferredObject:[command argumentAtIndex:0] withWrapper:jsWrapper];
378
+ }
379
+
380
+ - (void)injectScriptFile:(CDVInvokedUrlCommand*)command
381
+ {
382
+ NSString* jsWrapper;
383
+
384
+ if ((command.callbackId != nil) && ![command.callbackId isEqualToString:@"INVALID"]) {
385
+ jsWrapper = [NSString stringWithFormat:@"(function(d) { var c = d.createElement('script'); c.src = %%@; c.onload = function() { _cdvMessageHandler('%@'); }; d.body.appendChild(c); })(document)", command.callbackId];
386
+ } else {
387
+ jsWrapper = @"(function(d) { var c = d.createElement('script'); c.src = %@; d.body.appendChild(c); })(document)";
388
+ }
389
+ [self injectDeferredObject:[command argumentAtIndex:0] withWrapper:jsWrapper];
390
+ }
391
+
392
+ - (void)injectStyleCode:(CDVInvokedUrlCommand*)command
393
+ {
394
+ NSString* jsWrapper;
395
+
396
+ if ((command.callbackId != nil) && ![command.callbackId isEqualToString:@"INVALID"]) {
397
+ jsWrapper = [NSString stringWithFormat:@"(function(d) { var c = d.createElement('style'); c.innerHTML = %%@; c.onload = function() { _cdvMessageHandler('%@'); }; d.body.appendChild(c); })(document)", command.callbackId];
398
+ } else {
399
+ jsWrapper = @"(function(d) { var c = d.createElement('style'); c.innerHTML = %@; d.body.appendChild(c); })(document)";
400
+ }
401
+ [self injectDeferredObject:[command argumentAtIndex:0] withWrapper:jsWrapper];
402
+ }
403
+
404
+ - (void)injectStyleFile:(CDVInvokedUrlCommand*)command
405
+ {
406
+ NSString* jsWrapper;
407
+
408
+ if ((command.callbackId != nil) && ![command.callbackId isEqualToString:@"INVALID"]) {
409
+ jsWrapper = [NSString stringWithFormat:@"(function(d) { var c = d.createElement('link'); c.rel='stylesheet'; c.type='text/css'; c.href = %%@; c.onload = function() { _cdvMessageHandler('%@'); }; d.body.appendChild(c); })(document)", command.callbackId];
410
+ } else {
411
+ jsWrapper = @"(function(d) { var c = d.createElement('link'); c.rel='stylesheet', c.type='text/css'; c.href = %@; d.body.appendChild(c); })(document)";
412
+ }
413
+ [self injectDeferredObject:[command argumentAtIndex:0] withWrapper:jsWrapper];
414
+ }
415
+
416
+ - (BOOL)isValidCallbackId:(NSString *)callbackId
417
+ {
418
+ NSError *err = nil;
419
+ // Initialize on first use
420
+ if (self.callbackIdPattern == nil) {
421
+ self.callbackIdPattern = [NSRegularExpression regularExpressionWithPattern:@"^InAppBrowser[0-9]{1,10}$" options:0 error:&err];
422
+ if (err != nil) {
423
+ // Couldn't initialize Regex; No is safer than Yes.
424
+ return NO;
425
+ }
426
+ }
427
+ if ([self.callbackIdPattern firstMatchInString:callbackId options:0 range:NSMakeRange(0, [callbackId length])]) {
428
+ return YES;
429
+ }
430
+ return NO;
431
+ }
432
+
433
+ /**
434
+ * The message handler bridge provided for the InAppBrowser is capable of executing any oustanding callback belonging
435
+ * to the InAppBrowser plugin. Care has been taken that other callbacks cannot be triggered, and that no
436
+ * other code execution is possible.
437
+ */
438
+ - (void)webView:(WKWebView *)theWebView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
439
+
440
+ NSURL* url = navigationAction.request.URL;
441
+ NSURL* mainDocumentURL = navigationAction.request.mainDocumentURL;
442
+ BOOL isTopLevelNavigation = [url isEqual:mainDocumentURL];
443
+ BOOL shouldStart = YES;
444
+ BOOL useBeforeLoad = NO;
445
+ NSString* httpMethod = navigationAction.request.HTTPMethod;
446
+ NSString* errorMessage = nil;
447
+
448
+ if([_beforeload isEqualToString:@"post"]){
449
+ //TODO handle POST requests by preserving POST data then remove this condition
450
+ errorMessage = @"beforeload doesn't yet support POST requests";
451
+ }
452
+ else if(isTopLevelNavigation && (
453
+ [_beforeload isEqualToString:@"yes"]
454
+ || ([_beforeload isEqualToString:@"get"] && [httpMethod isEqualToString:@"GET"])
455
+ // TODO comment in when POST requests are handled
456
+ // || ([_beforeload isEqualToString:@"post"] && [httpMethod isEqualToString:@"POST"])
457
+ )){
458
+ useBeforeLoad = YES;
459
+ }
460
+
461
+ // When beforeload, on first URL change, initiate JS callback. Only after the beforeload event, continue.
462
+ if (_waitForBeforeload && useBeforeLoad) {
463
+ CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
464
+ messageAsDictionary:@{@"type":@"beforeload", @"url":[url absoluteString]}];
465
+ [pluginResult setKeepCallback:[NSNumber numberWithBool:YES]];
466
+
467
+ [self.commandDelegate sendPluginResult:pluginResult callbackId:self.callbackId];
468
+ decisionHandler(WKNavigationActionPolicyCancel);
469
+ return;
470
+ }
471
+
472
+ if(errorMessage != nil){
473
+ NSLog(errorMessage);
474
+ CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR
475
+ messageAsDictionary:@{@"type":@"loaderror", @"url":[url absoluteString], @"code": @"-1", @"message": errorMessage}];
476
+ [pluginResult setKeepCallback:[NSNumber numberWithBool:YES]];
477
+ [self.commandDelegate sendPluginResult:pluginResult callbackId:self.callbackId];
478
+ }
479
+
480
+ //if is an app store, tel, sms, mailto or geo link, let the system handle it, otherwise it fails to load it
481
+ NSArray * allowedSchemes = @[@"itms-appss", @"itms-apps", @"tel", @"sms", @"mailto", @"geo"];
482
+ if ([allowedSchemes containsObject:[url scheme]]) {
483
+ [theWebView stopLoading];
484
+ [self openInSystem:url];
485
+ shouldStart = NO;
486
+ }
487
+ else if ((self.callbackId != nil) && isTopLevelNavigation) {
488
+ // Send a loadstart event for each top-level navigation (includes redirects).
489
+ CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
490
+ messageAsDictionary:@{@"type":@"loadstart", @"url":[url absoluteString]}];
491
+ [pluginResult setKeepCallback:[NSNumber numberWithBool:YES]];
492
+
493
+ [self.commandDelegate sendPluginResult:pluginResult callbackId:self.callbackId];
494
+ }
495
+
496
+ if (useBeforeLoad) {
497
+ _waitForBeforeload = YES;
498
+ }
499
+
500
+ if(shouldStart){
501
+ // Fix GH-417 & GH-424: Handle non-default target attribute
502
+ // Based on https://stackoverflow.com/a/25713070/777265
503
+ if (!navigationAction.targetFrame){
504
+ [theWebView loadRequest:navigationAction.request];
505
+ decisionHandler(WKNavigationActionPolicyCancel);
506
+ }else{
507
+ decisionHandler(WKNavigationActionPolicyAllow);
508
+ }
509
+ }else{
510
+ decisionHandler(WKNavigationActionPolicyCancel);
511
+ }
512
+ }
513
+
514
+ #pragma mark WKScriptMessageHandler delegate
515
+ - (void)userContentController:(nonnull WKUserContentController *)userContentController didReceiveScriptMessage:(nonnull WKScriptMessage *)message {
516
+
517
+ CDVPluginResult* pluginResult = nil;
518
+
519
+ if([message.body isKindOfClass:[NSDictionary class]]){
520
+ NSDictionary* messageContent = (NSDictionary*) message.body;
521
+ NSString* scriptCallbackId = messageContent[@"id"];
522
+
523
+ if([messageContent objectForKey:@"d"]){
524
+ NSString* scriptResult = messageContent[@"d"];
525
+ NSError* __autoreleasing error = nil;
526
+ NSData* decodedResult = [NSJSONSerialization JSONObjectWithData:[scriptResult dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:&error];
527
+ if ((error == nil) && [decodedResult isKindOfClass:[NSArray class]]) {
528
+ pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsArray:(NSArray*)decodedResult];
529
+ } else {
530
+ pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_JSON_EXCEPTION];
531
+ }
532
+ } else {
533
+ pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsArray:@[]];
534
+ }
535
+ [self.commandDelegate sendPluginResult:pluginResult callbackId:scriptCallbackId];
536
+ }else if(self.callbackId != nil){
537
+ // Send a message event
538
+ NSString* messageContent = (NSString*) message.body;
539
+ NSError* __autoreleasing error = nil;
540
+ NSData* decodedResult = [NSJSONSerialization JSONObjectWithData:[messageContent dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:&error];
541
+ if (error == nil) {
542
+ NSMutableDictionary* dResult = [NSMutableDictionary new];
543
+ [dResult setValue:@"message" forKey:@"type"];
544
+ [dResult setObject:decodedResult forKey:@"data"];
545
+ CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dResult];
546
+ [pluginResult setKeepCallback:[NSNumber numberWithBool:YES]];
547
+ [self.commandDelegate sendPluginResult:pluginResult callbackId:self.callbackId];
548
+ }
549
+ }
550
+ }
551
+
552
+ - (void)didStartProvisionalNavigation:(WKWebView*)theWebView
553
+ {
554
+ NSLog(@"didStartProvisionalNavigation");
555
+ // self.inAppBrowserViewController.currentURL = theWebView.URL;
556
+ }
557
+
558
+ - (void)didFinishNavigation:(WKWebView*)theWebView
559
+ {
560
+ if (self.callbackId != nil) {
561
+ NSString* url = [theWebView.URL absoluteString];
562
+ if(url == nil){
563
+ if(self.inAppBrowserViewController.currentURL != nil){
564
+ url = [self.inAppBrowserViewController.currentURL absoluteString];
565
+ }else{
566
+ url = @"";
567
+ }
568
+ }
569
+ CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
570
+ messageAsDictionary:@{@"type":@"loadstop", @"url":url}];
571
+ [pluginResult setKeepCallback:[NSNumber numberWithBool:YES]];
572
+
573
+ [self.commandDelegate sendPluginResult:pluginResult callbackId:self.callbackId];
574
+ }
575
+ }
576
+
577
+ - (void)webView:(WKWebView*)theWebView didFailNavigation:(NSError*)error
578
+ {
579
+ if (self.callbackId != nil) {
580
+ NSString* url = [theWebView.URL absoluteString];
581
+ if(url == nil){
582
+ if(self.inAppBrowserViewController.currentURL != nil){
583
+ url = [self.inAppBrowserViewController.currentURL absoluteString];
584
+ }else{
585
+ url = @"";
586
+ }
587
+ }
588
+ CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR
589
+ messageAsDictionary:@{@"type":@"loaderror", @"url":url, @"code": [NSNumber numberWithInteger:error.code], @"message": error.localizedDescription}];
590
+ [pluginResult setKeepCallback:[NSNumber numberWithBool:YES]];
591
+
592
+ [self.commandDelegate sendPluginResult:pluginResult callbackId:self.callbackId];
593
+ }
594
+ }
595
+
596
+ - (void)browserExit
597
+ {
598
+ if (self.callbackId != nil) {
599
+ CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
600
+ messageAsDictionary:@{@"type":@"exit"}];
601
+ [self.commandDelegate sendPluginResult:pluginResult callbackId:self.callbackId];
602
+ self.callbackId = nil;
603
+ }
604
+
605
+ [self.inAppBrowserViewController.configuration.userContentController removeScriptMessageHandlerForName:IAB_BRIDGE_NAME];
606
+ self.inAppBrowserViewController.configuration = nil;
607
+
608
+ [self.inAppBrowserViewController.webView stopLoading];
609
+ [self.inAppBrowserViewController.webView removeFromSuperview];
610
+ [self.inAppBrowserViewController.webView setUIDelegate:nil];
611
+ [self.inAppBrowserViewController.webView setNavigationDelegate:nil];
612
+ self.inAppBrowserViewController.webView = nil;
613
+
614
+ // Set navigationDelegate to nil to ensure no callbacks are received from it.
615
+ self.inAppBrowserViewController.navigationDelegate = nil;
616
+ self.inAppBrowserViewController = nil;
617
+
618
+ // Set tmpWindow to hidden to make main webview responsive to touch again
619
+ // Based on https://stackoverflow.com/questions/4544489/how-to-remove-a-uiwindow
620
+ self->tmpWindow.hidden = YES;
621
+ self->tmpWindow = nil;
622
+ }
623
+
624
+ @end //CDVWKInAppBrowser
625
+
626
+ #pragma mark CDVWKInAppBrowserViewController
627
+
628
+ @implementation CDVWKInAppBrowserViewController
629
+
630
+ @synthesize currentURL;
631
+
632
+ CGFloat lastReducedStatusBarHeight = 0.0;
633
+ BOOL isExiting = FALSE;
634
+
635
+ - (id)initWithBrowserOptions: (CDVInAppBrowserOptions*) browserOptions andSettings:(NSDictionary *)settings
636
+ {
637
+ self = [super init];
638
+ if (self != nil) {
639
+ _browserOptions = browserOptions;
640
+ _settings = settings;
641
+ self.webViewUIDelegate = [[CDVWKInAppBrowserUIDelegate alloc] initWithTitle:[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDisplayName"]];
642
+ [self.webViewUIDelegate setViewController:self];
643
+
644
+ [self createViews];
645
+ }
646
+
647
+ return self;
648
+ }
649
+
650
+ -(void)dealloc {
651
+ //NSLog(@"dealloc");
652
+ }
653
+
654
+ - (void)createViews
655
+ {
656
+ // We create the views in code for primarily for ease of upgrades and not requiring an external .xib to be included
657
+
658
+ CGRect webViewBounds = self.view.bounds;
659
+ BOOL toolbarIsAtBottom = ![_browserOptions.toolbarposition isEqualToString:kInAppBrowserToolbarBarPositionTop];
660
+ webViewBounds.size.height -= _browserOptions.location ? FOOTER_HEIGHT : TOOLBAR_HEIGHT;
661
+ WKUserContentController* userContentController = [[WKUserContentController alloc] init];
662
+
663
+ WKWebViewConfiguration* configuration = [[WKWebViewConfiguration alloc] init];
664
+
665
+ NSString *userAgent = configuration.applicationNameForUserAgent;
666
+ if (
667
+ [self settingForKey:@"OverrideUserAgent"] == nil &&
668
+ [self settingForKey:@"AppendUserAgent"] != nil
669
+ ) {
670
+ userAgent = [NSString stringWithFormat:@"%@ %@", userAgent, [self settingForKey:@"AppendUserAgent"]];
671
+ }
672
+ configuration.applicationNameForUserAgent = userAgent;
673
+ configuration.userContentController = userContentController;
674
+ #if __has_include(<Cordova/CDVWebViewProcessPoolFactory.h>)
675
+ configuration.processPool = [[CDVWebViewProcessPoolFactory sharedFactory] sharedProcessPool];
676
+ #elif __has_include("CDVWKProcessPoolFactory.h")
677
+ configuration.processPool = [[CDVWKProcessPoolFactory sharedFactory] sharedProcessPool];
678
+ #endif
679
+ [configuration.userContentController addScriptMessageHandler:self name:IAB_BRIDGE_NAME];
680
+
681
+ //WKWebView options
682
+ configuration.allowsInlineMediaPlayback = _browserOptions.allowinlinemediaplayback;
683
+ configuration.ignoresViewportScaleLimits = _browserOptions.enableviewportscale;
684
+ if(_browserOptions.mediaplaybackrequiresuseraction == YES){
685
+ configuration.mediaTypesRequiringUserActionForPlayback = WKAudiovisualMediaTypeAll;
686
+ }else{
687
+ configuration.mediaTypesRequiringUserActionForPlayback = WKAudiovisualMediaTypeNone;
688
+ }
689
+
690
+ if (@available(iOS 13.0, *)) {
691
+ NSString *contentMode = [self settingForKey:@"PreferredContentMode"];
692
+ if ([contentMode isEqual: @"mobile"]) {
693
+ configuration.defaultWebpagePreferences.preferredContentMode = WKContentModeMobile;
694
+ } else if ([contentMode isEqual: @"desktop"]) {
695
+ configuration.defaultWebpagePreferences.preferredContentMode = WKContentModeDesktop;
696
+ }
697
+
698
+ }
699
+
700
+
701
+ self.webView = [[WKWebView alloc] initWithFrame:webViewBounds configuration:configuration];
702
+
703
+ #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 160400
704
+ // With the introduction of iOS 16.4 the webview is no longer inspectable by default.
705
+ // We'll honor that change for release builds, but will still allow inspection on debug builds by default.
706
+ // We also introduce an override option, so consumers can influence this decision in their own build.
707
+ if (@available(iOS 16.4, *)) {
708
+ #ifdef DEBUG
709
+ BOOL allowWebviewInspectionDefault = YES;
710
+ #else
711
+ BOOL allowWebviewInspectionDefault = NO;
712
+ #endif
713
+ self.webView.inspectable = [_settings cordovaBoolSettingForKey:@"InspectableWebview" defaultValue:allowWebviewInspectionDefault];
714
+ }
715
+ #endif
716
+
717
+
718
+ [self.view addSubview:self.webView];
719
+ [self.view sendSubviewToBack:self.webView];
720
+
721
+
722
+ self.webView.navigationDelegate = self;
723
+ self.webView.UIDelegate = self.webViewUIDelegate;
724
+ self.webView.backgroundColor = [UIColor whiteColor];
725
+ if ([self settingForKey:@"OverrideUserAgent"] != nil) {
726
+ self.webView.customUserAgent = [self settingForKey:@"OverrideUserAgent"];
727
+ }
728
+
729
+ self.webView.clearsContextBeforeDrawing = YES;
730
+ self.webView.clipsToBounds = YES;
731
+ self.webView.contentMode = UIViewContentModeScaleToFill;
732
+ self.webView.multipleTouchEnabled = YES;
733
+ self.webView.opaque = YES;
734
+ self.webView.userInteractionEnabled = YES;
735
+ self.automaticallyAdjustsScrollViewInsets = YES ;
736
+ [self.webView setAutoresizingMask:UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth];
737
+ self.webView.allowsLinkPreview = NO;
738
+ self.webView.allowsBackForwardNavigationGestures = NO;
739
+
740
+ [self.webView.scrollView setContentInsetAdjustmentBehavior:UIScrollViewContentInsetAdjustmentNever];
741
+
742
+ self.spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
743
+ self.spinner.alpha = 1.000;
744
+ self.spinner.autoresizesSubviews = YES;
745
+ self.spinner.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleRightMargin);
746
+ self.spinner.clearsContextBeforeDrawing = NO;
747
+ self.spinner.clipsToBounds = NO;
748
+ self.spinner.contentMode = UIViewContentModeScaleToFill;
749
+ self.spinner.frame = CGRectMake(CGRectGetMidX(self.webView.frame), CGRectGetMidY(self.webView.frame), 20.0, 20.0);
750
+ self.spinner.hidden = NO;
751
+ self.spinner.hidesWhenStopped = YES;
752
+ self.spinner.multipleTouchEnabled = NO;
753
+ self.spinner.opaque = NO;
754
+ self.spinner.userInteractionEnabled = NO;
755
+ [self.spinner stopAnimating];
756
+
757
+ self.closeButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(close)];
758
+ self.closeButton.enabled = YES;
759
+
760
+ UIBarButtonItem* flexibleSpaceButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
761
+
762
+ UIBarButtonItem* fixedSpaceButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
763
+ fixedSpaceButton.width = 20;
764
+
765
+ float toolbarY = toolbarIsAtBottom ? self.view.bounds.size.height - TOOLBAR_HEIGHT : 0.0;
766
+ CGRect toolbarFrame = CGRectMake(0.0, toolbarY, self.view.bounds.size.width, TOOLBAR_HEIGHT);
767
+
768
+ self.toolbar = [[UIToolbar alloc] initWithFrame:toolbarFrame];
769
+ self.toolbar.alpha = 1.000;
770
+ self.toolbar.autoresizesSubviews = YES;
771
+ self.toolbar.autoresizingMask = toolbarIsAtBottom ? (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin) : UIViewAutoresizingFlexibleWidth;
772
+ self.toolbar.barStyle = UIBarStyleBlackOpaque;
773
+ self.toolbar.clearsContextBeforeDrawing = NO;
774
+ self.toolbar.clipsToBounds = NO;
775
+ self.toolbar.contentMode = UIViewContentModeScaleToFill;
776
+ self.toolbar.hidden = NO;
777
+ self.toolbar.multipleTouchEnabled = NO;
778
+ self.toolbar.opaque = NO;
779
+ self.toolbar.userInteractionEnabled = YES;
780
+ if (_browserOptions.toolbarcolor != nil) { // Set toolbar color if user sets it in options
781
+ self.toolbar.barTintColor = [self colorFromHexString:_browserOptions.toolbarcolor];
782
+ }
783
+ if (!_browserOptions.toolbartranslucent) { // Set toolbar translucent to no if user sets it in options
784
+ self.toolbar.translucent = NO;
785
+ }
786
+
787
+ CGFloat labelInset = 5.0;
788
+ float locationBarY = toolbarIsAtBottom ? self.view.bounds.size.height - FOOTER_HEIGHT : self.view.bounds.size.height - LOCATIONBAR_HEIGHT;
789
+
790
+ self.addressLabel = [[UILabel alloc] initWithFrame:CGRectMake(labelInset, locationBarY, self.view.bounds.size.width - labelInset, LOCATIONBAR_HEIGHT)];
791
+ self.addressLabel.adjustsFontSizeToFitWidth = NO;
792
+ self.addressLabel.alpha = 1.000;
793
+ self.addressLabel.autoresizesSubviews = YES;
794
+ self.addressLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin;
795
+ self.addressLabel.backgroundColor = [UIColor clearColor];
796
+ self.addressLabel.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
797
+ self.addressLabel.clearsContextBeforeDrawing = YES;
798
+ self.addressLabel.clipsToBounds = YES;
799
+ self.addressLabel.contentMode = UIViewContentModeScaleToFill;
800
+ self.addressLabel.enabled = YES;
801
+ self.addressLabel.hidden = NO;
802
+ self.addressLabel.lineBreakMode = NSLineBreakByTruncatingTail;
803
+
804
+ if ([self.addressLabel respondsToSelector:NSSelectorFromString(@"setMinimumScaleFactor:")]) {
805
+ [self.addressLabel setValue:@(10.0/[UIFont labelFontSize]) forKey:@"minimumScaleFactor"];
806
+ } else if ([self.addressLabel respondsToSelector:NSSelectorFromString(@"setMinimumFontSize:")]) {
807
+ [self.addressLabel setValue:@(10.0) forKey:@"minimumFontSize"];
808
+ }
809
+
810
+ self.addressLabel.multipleTouchEnabled = NO;
811
+ self.addressLabel.numberOfLines = 1;
812
+ self.addressLabel.opaque = NO;
813
+ self.addressLabel.shadowOffset = CGSizeMake(0.0, -1.0);
814
+ self.addressLabel.text = NSLocalizedString(@"Loading...", nil);
815
+ self.addressLabel.textAlignment = NSTextAlignmentLeft;
816
+ self.addressLabel.textColor = [UIColor colorWithWhite:1.000 alpha:1.000];
817
+ self.addressLabel.userInteractionEnabled = NO;
818
+
819
+ NSString* frontArrowString = NSLocalizedString(@"►", nil); // create arrow from Unicode char
820
+ self.forwardButton = [[UIBarButtonItem alloc] initWithTitle:frontArrowString style:UIBarButtonItemStylePlain target:self action:@selector(goForward:)];
821
+ self.forwardButton.enabled = YES;
822
+ self.forwardButton.imageInsets = UIEdgeInsetsZero;
823
+ if (_browserOptions.navigationbuttoncolor != nil) { // Set button color if user sets it in options
824
+ self.forwardButton.tintColor = [self colorFromHexString:_browserOptions.navigationbuttoncolor];
825
+ }
826
+
827
+ NSString* backArrowString = NSLocalizedString(@"◄", nil); // create arrow from Unicode char
828
+ self.backButton = [[UIBarButtonItem alloc] initWithTitle:backArrowString style:UIBarButtonItemStylePlain target:self action:@selector(goBack:)];
829
+ self.backButton.enabled = YES;
830
+ self.backButton.imageInsets = UIEdgeInsetsZero;
831
+ if (_browserOptions.navigationbuttoncolor != nil) { // Set button color if user sets it in options
832
+ self.backButton.tintColor = [self colorFromHexString:_browserOptions.navigationbuttoncolor];
833
+ }
834
+
835
+ // Filter out Navigation Buttons if user requests so
836
+ if (_browserOptions.hidenavigationbuttons) {
837
+ if (_browserOptions.lefttoright) {
838
+ [self.toolbar setItems:@[flexibleSpaceButton, self.closeButton]];
839
+ } else {
840
+ [self.toolbar setItems:@[self.closeButton, flexibleSpaceButton]];
841
+ }
842
+ } else if (_browserOptions.lefttoright) {
843
+ [self.toolbar setItems:@[self.backButton, fixedSpaceButton, self.forwardButton, flexibleSpaceButton, self.closeButton]];
844
+ } else {
845
+ [self.toolbar setItems:@[self.closeButton, flexibleSpaceButton, self.backButton, fixedSpaceButton, self.forwardButton]];
846
+ }
847
+
848
+ self.view.backgroundColor = [UIColor clearColor];
849
+ [self.view addSubview:self.toolbar];
850
+ [self.view addSubview:self.addressLabel];
851
+ [self.view addSubview:self.spinner];
852
+ }
853
+
854
+ - (id)settingForKey:(NSString*)key
855
+ {
856
+ return [_settings objectForKey:[key lowercaseString]];
857
+ }
858
+
859
+ - (void) setWebViewFrame : (CGRect) frame {
860
+ NSLog(@"Setting the WebView's frame to %@", NSStringFromCGRect(frame));
861
+ [self.webView setFrame:frame];
862
+ }
863
+
864
+ - (void)setCloseButtonTitle:(NSString*)title : (NSString*) colorString : (int) buttonIndex
865
+ {
866
+ // the advantage of using UIBarButtonSystemItemDone is the system will localize it for you automatically
867
+ // but, if you want to set this yourself, knock yourself out (we can't set the title for a system Done button, so we have to create a new one)
868
+ self.closeButton = nil;
869
+ // Initialize with title if title is set, otherwise the title will be 'Done' localized
870
+ self.closeButton = title != nil ? [[UIBarButtonItem alloc] initWithTitle:title style:UIBarButtonItemStylePlain target:self action:@selector(close)] : [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(close)];
871
+ self.closeButton.enabled = YES;
872
+ // If color on closebutton is requested then initialize with that that color, otherwise use initialize with default
873
+ self.closeButton.tintColor = colorString != nil ? [self colorFromHexString:colorString] : [UIColor colorWithRed:60.0 / 255.0 green:136.0 / 255.0 blue:230.0 / 255.0 alpha:1];
874
+
875
+ NSMutableArray* items = [self.toolbar.items mutableCopy];
876
+ [items replaceObjectAtIndex:buttonIndex withObject:self.closeButton];
877
+ [self.toolbar setItems:items];
878
+ }
879
+
880
+ - (void)showLocationBar:(BOOL)show
881
+ {
882
+ CGRect locationbarFrame = self.addressLabel.frame;
883
+
884
+ BOOL toolbarVisible = !self.toolbar.hidden;
885
+
886
+ // prevent double show/hide
887
+ if (show == !(self.addressLabel.hidden)) {
888
+ return;
889
+ }
890
+
891
+ if (show) {
892
+ self.addressLabel.hidden = NO;
893
+
894
+ if (toolbarVisible) {
895
+ // toolBar at the bottom, leave as is
896
+ // put locationBar on top of the toolBar
897
+
898
+ CGRect webViewBounds = self.view.bounds;
899
+ webViewBounds.size.height -= FOOTER_HEIGHT;
900
+ [self setWebViewFrame:webViewBounds];
901
+
902
+ locationbarFrame.origin.y = webViewBounds.size.height;
903
+ self.addressLabel.frame = locationbarFrame;
904
+ } else {
905
+ // no toolBar, so put locationBar at the bottom
906
+
907
+ CGRect webViewBounds = self.view.bounds;
908
+ webViewBounds.size.height -= LOCATIONBAR_HEIGHT;
909
+ [self setWebViewFrame:webViewBounds];
910
+
911
+ locationbarFrame.origin.y = webViewBounds.size.height;
912
+ self.addressLabel.frame = locationbarFrame;
913
+ }
914
+ } else {
915
+ self.addressLabel.hidden = YES;
916
+
917
+ if (toolbarVisible) {
918
+ // locationBar is on top of toolBar, hide locationBar
919
+
920
+ // webView take up whole height less toolBar height
921
+ CGRect webViewBounds = self.view.bounds;
922
+ webViewBounds.size.height -= TOOLBAR_HEIGHT;
923
+ [self setWebViewFrame:webViewBounds];
924
+ } else {
925
+ // no toolBar, expand webView to screen dimensions
926
+ [self setWebViewFrame:self.view.bounds];
927
+ }
928
+ }
929
+ }
930
+
931
+ - (void)showToolBar:(BOOL)show : (NSString *) toolbarPosition
932
+ {
933
+ CGRect toolbarFrame = self.toolbar.frame;
934
+ CGRect locationbarFrame = self.addressLabel.frame;
935
+
936
+ BOOL locationbarVisible = !self.addressLabel.hidden;
937
+
938
+ // prevent double show/hide
939
+ if (show == !(self.toolbar.hidden)) {
940
+ return;
941
+ }
942
+
943
+ if (show) {
944
+ self.toolbar.hidden = NO;
945
+ CGRect webViewBounds = self.view.bounds;
946
+
947
+ if (locationbarVisible) {
948
+ // locationBar at the bottom, move locationBar up
949
+ // put toolBar at the bottom
950
+ webViewBounds.size.height -= FOOTER_HEIGHT;
951
+ locationbarFrame.origin.y = webViewBounds.size.height;
952
+ self.addressLabel.frame = locationbarFrame;
953
+ self.toolbar.frame = toolbarFrame;
954
+ } else {
955
+ // no locationBar, so put toolBar at the bottom
956
+ CGRect webViewBounds = self.view.bounds;
957
+ webViewBounds.size.height -= TOOLBAR_HEIGHT;
958
+ self.toolbar.frame = toolbarFrame;
959
+ }
960
+
961
+ if ([toolbarPosition isEqualToString:kInAppBrowserToolbarBarPositionTop]) {
962
+ toolbarFrame.origin.y = 0;
963
+ webViewBounds.origin.y += toolbarFrame.size.height;
964
+ [self setWebViewFrame:webViewBounds];
965
+ } else {
966
+ toolbarFrame.origin.y = (webViewBounds.size.height + LOCATIONBAR_HEIGHT);
967
+ }
968
+ [self setWebViewFrame:webViewBounds];
969
+
970
+ } else {
971
+ self.toolbar.hidden = YES;
972
+
973
+ if (locationbarVisible) {
974
+ // locationBar is on top of toolBar, hide toolBar
975
+ // put locationBar at the bottom
976
+
977
+ // webView take up whole height less locationBar height
978
+ CGRect webViewBounds = self.view.bounds;
979
+ webViewBounds.size.height -= LOCATIONBAR_HEIGHT;
980
+ [self setWebViewFrame:webViewBounds];
981
+
982
+ // move locationBar down
983
+ locationbarFrame.origin.y = webViewBounds.size.height;
984
+ self.addressLabel.frame = locationbarFrame;
985
+ } else {
986
+ // no locationBar, expand webView to screen dimensions
987
+ [self setWebViewFrame:self.view.bounds];
988
+ }
989
+ }
990
+ }
991
+
992
+ - (void)viewDidLoad
993
+ {
994
+ [super viewDidLoad];
995
+ }
996
+
997
+ - (void)viewDidDisappear:(BOOL)animated
998
+ {
999
+ [super viewDidDisappear:animated];
1000
+ if (isExiting && (self.navigationDelegate != nil) && [self.navigationDelegate respondsToSelector:@selector(browserExit)]) {
1001
+ [self.navigationDelegate browserExit];
1002
+ isExiting = FALSE;
1003
+ }
1004
+ }
1005
+
1006
+ - (UIStatusBarStyle)preferredStatusBarStyle
1007
+ {
1008
+ NSString* statusBarStylePreference = [self settingForKey:@"InAppBrowserStatusBarStyle"];
1009
+ if (statusBarStylePreference && [statusBarStylePreference isEqualToString:@"lightcontent"]) {
1010
+ return UIStatusBarStyleLightContent;
1011
+ } else if (statusBarStylePreference && [statusBarStylePreference isEqualToString:@"darkcontent"]) {
1012
+ if (@available(iOS 13.0, *)) {
1013
+ return UIStatusBarStyleDarkContent;
1014
+ } else {
1015
+ return UIStatusBarStyleDefault;
1016
+ }
1017
+ } else {
1018
+ return UIStatusBarStyleDefault;
1019
+ }
1020
+ }
1021
+
1022
+ - (BOOL)prefersStatusBarHidden {
1023
+ return NO;
1024
+ }
1025
+
1026
+ - (void)close
1027
+ {
1028
+ self.currentURL = nil;
1029
+
1030
+ __weak UIViewController* weakSelf = self;
1031
+
1032
+ // Run later to avoid the "took a long time" log message.
1033
+ dispatch_async(dispatch_get_main_queue(), ^{
1034
+ isExiting = TRUE;
1035
+ lastReducedStatusBarHeight = 0.0;
1036
+ if ([weakSelf respondsToSelector:@selector(presentingViewController)]) {
1037
+ [[weakSelf presentingViewController] dismissViewControllerAnimated:YES completion:nil];
1038
+ } else {
1039
+ [[weakSelf parentViewController] dismissViewControllerAnimated:YES completion:nil];
1040
+ }
1041
+ });
1042
+ }
1043
+
1044
+ - (void)navigateTo:(NSURL*)url
1045
+ {
1046
+ if ([url.scheme isEqualToString:@"file"]) {
1047
+ [self.webView loadFileURL:url allowingReadAccessToURL:url];
1048
+ } else {
1049
+ NSURLRequest* request = [NSURLRequest requestWithURL:url];
1050
+ [self.webView loadRequest:request];
1051
+ }
1052
+ }
1053
+
1054
+ - (void)goBack:(id)sender
1055
+ {
1056
+ [self.webView goBack];
1057
+ }
1058
+
1059
+ - (void)goForward:(id)sender
1060
+ {
1061
+ [self.webView goForward];
1062
+ }
1063
+
1064
+ - (void)viewWillAppear:(BOOL)animated
1065
+ {
1066
+ [self rePositionViews];
1067
+
1068
+ [super viewWillAppear:animated];
1069
+ }
1070
+
1071
+ - (float) getStatusBarOffset {
1072
+ return (float) [[UIApplication sharedApplication] statusBarFrame].size.height;
1073
+ }
1074
+
1075
+ - (void) rePositionViews {
1076
+ CGRect viewBounds = [self.webView bounds];
1077
+ CGFloat statusBarHeight = [self getStatusBarOffset];
1078
+
1079
+ // orientation portrait or portraitUpsideDown: status bar is on the top and web view is to be aligned to the bottom of the status bar
1080
+ // orientation landscapeLeft or landscapeRight: status bar height is 0 in but lets account for it in case things ever change in the future
1081
+ viewBounds.origin.y = statusBarHeight;
1082
+
1083
+ // account for web view height portion that may have been reduced by a previous call to this method
1084
+ viewBounds.size.height = viewBounds.size.height - statusBarHeight + lastReducedStatusBarHeight;
1085
+ lastReducedStatusBarHeight = statusBarHeight;
1086
+
1087
+ if ((_browserOptions.toolbar) && ([_browserOptions.toolbarposition isEqualToString:kInAppBrowserToolbarBarPositionTop])) {
1088
+ // if we have to display the toolbar on top of the web view, we need to account for its height
1089
+ viewBounds.origin.y += TOOLBAR_HEIGHT;
1090
+ self.toolbar.frame = CGRectMake(self.toolbar.frame.origin.x, statusBarHeight, self.toolbar.frame.size.width, self.toolbar.frame.size.height);
1091
+ }
1092
+
1093
+ self.webView.frame = viewBounds;
1094
+ }
1095
+
1096
+ // Helper function to convert hex color string to UIColor
1097
+ // Assumes input like "#00FF00" (#RRGGBB).
1098
+ // Taken from https://stackoverflow.com/questions/1560081/how-can-i-create-a-uicolor-from-a-hex-string
1099
+ - (UIColor *)colorFromHexString:(NSString *)hexString {
1100
+ unsigned rgbValue = 0;
1101
+ NSScanner *scanner = [NSScanner scannerWithString:hexString];
1102
+ [scanner setScanLocation:1]; // bypass '#' character
1103
+ [scanner scanHexInt:&rgbValue];
1104
+ return [UIColor colorWithRed:((rgbValue & 0xFF0000) >> 16)/255.0 green:((rgbValue & 0xFF00) >> 8)/255.0 blue:(rgbValue & 0xFF)/255.0 alpha:1.0];
1105
+ }
1106
+
1107
+ #pragma mark WKNavigationDelegate
1108
+
1109
+ - (void)webView:(WKWebView *)theWebView didStartProvisionalNavigation:(WKNavigation *)navigation{
1110
+
1111
+ // loading url, start spinner, update back/forward
1112
+
1113
+ self.addressLabel.text = NSLocalizedString(@"Loading...", nil);
1114
+ self.backButton.enabled = theWebView.canGoBack;
1115
+ self.forwardButton.enabled = theWebView.canGoForward;
1116
+
1117
+ NSLog(_browserOptions.hidespinner ? @"Yes" : @"No");
1118
+ if(!_browserOptions.hidespinner) {
1119
+ [self.spinner startAnimating];
1120
+ }
1121
+
1122
+ return [self.navigationDelegate didStartProvisionalNavigation:theWebView];
1123
+ }
1124
+
1125
+ - (void)webView:(WKWebView *)theWebView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
1126
+ {
1127
+ NSURL *url = navigationAction.request.URL;
1128
+ NSURL *mainDocumentURL = navigationAction.request.mainDocumentURL;
1129
+
1130
+ BOOL isTopLevelNavigation = [url isEqual:mainDocumentURL];
1131
+
1132
+ if (isTopLevelNavigation) {
1133
+ self.currentURL = url;
1134
+ }
1135
+
1136
+ [self.navigationDelegate webView:theWebView decidePolicyForNavigationAction:navigationAction decisionHandler:decisionHandler];
1137
+ }
1138
+
1139
+ - (void)webView:(WKWebView *)theWebView didFinishNavigation:(WKNavigation *)navigation
1140
+ {
1141
+ // update url, stop spinner, update back/forward
1142
+
1143
+ self.addressLabel.text = [self.currentURL absoluteString];
1144
+ self.backButton.enabled = theWebView.canGoBack;
1145
+ self.forwardButton.enabled = theWebView.canGoForward;
1146
+ theWebView.scrollView.contentInset = UIEdgeInsetsZero;
1147
+
1148
+ [self.spinner stopAnimating];
1149
+
1150
+ [self.navigationDelegate didFinishNavigation:theWebView];
1151
+ }
1152
+
1153
+ - (void)webView:(WKWebView*)theWebView failedNavigation:(NSString*) delegateName withError:(nonnull NSError *)error{
1154
+ // log fail message, stop spinner, update back/forward
1155
+ NSLog(@"webView:%@ - %ld: %@", delegateName, (long)error.code, [error localizedDescription]);
1156
+
1157
+ self.backButton.enabled = theWebView.canGoBack;
1158
+ self.forwardButton.enabled = theWebView.canGoForward;
1159
+ [self.spinner stopAnimating];
1160
+
1161
+ self.addressLabel.text = NSLocalizedString(@"Load Error", nil);
1162
+
1163
+ [self.navigationDelegate webView:theWebView didFailNavigation:error];
1164
+ }
1165
+
1166
+ - (void)webView:(WKWebView*)theWebView didFailNavigation:(null_unspecified WKNavigation *)navigation withError:(nonnull NSError *)error
1167
+ {
1168
+ [self webView:theWebView failedNavigation:@"didFailNavigation" withError:error];
1169
+ }
1170
+
1171
+ - (void)webView:(WKWebView*)theWebView didFailProvisionalNavigation:(null_unspecified WKNavigation *)navigation withError:(nonnull NSError *)error
1172
+ {
1173
+ [self webView:theWebView failedNavigation:@"didFailProvisionalNavigation" withError:error];
1174
+ }
1175
+
1176
+ #pragma mark WKScriptMessageHandler delegate
1177
+ - (void)userContentController:(nonnull WKUserContentController *)userContentController didReceiveScriptMessage:(nonnull WKScriptMessage *)message {
1178
+ if (![message.name isEqualToString:IAB_BRIDGE_NAME]) {
1179
+ return;
1180
+ }
1181
+ //NSLog(@"Received script message %@", message.body);
1182
+ [self.navigationDelegate userContentController:userContentController didReceiveScriptMessage:message];
1183
+ }
1184
+
1185
+ #pragma mark CDVScreenOrientationDelegate
1186
+
1187
+ - (BOOL)shouldAutorotate
1188
+ {
1189
+ if ((self.orientationDelegate != nil) && [self.orientationDelegate respondsToSelector:@selector(shouldAutorotate)]) {
1190
+ return [self.orientationDelegate shouldAutorotate];
1191
+ }
1192
+ return YES;
1193
+ }
1194
+
1195
+ - (UIInterfaceOrientationMask)supportedInterfaceOrientations
1196
+ {
1197
+ if ((self.orientationDelegate != nil) && [self.orientationDelegate respondsToSelector:@selector(supportedInterfaceOrientations)]) {
1198
+ return [self.orientationDelegate supportedInterfaceOrientations];
1199
+ }
1200
+
1201
+ return 1 << UIInterfaceOrientationPortrait;
1202
+ }
1203
+
1204
+ - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
1205
+ {
1206
+ [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context)
1207
+ {
1208
+ [self rePositionViews];
1209
+ } completion:^(id<UIViewControllerTransitionCoordinatorContext> context)
1210
+ {
1211
+
1212
+ }];
1213
+
1214
+ [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
1215
+ }
1216
+
1217
+ #pragma mark UIAdaptivePresentationControllerDelegate
1218
+
1219
+ - (void)presentationControllerWillDismiss:(UIPresentationController *)presentationController {
1220
+ isExiting = TRUE;
1221
+ }
1222
+
1223
+ @end //CDVWKInAppBrowserViewController