forked from apache/cordova-plugin-inappbrowser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCDVWKInAppBrowser.m
1232 lines (1025 loc) · 51.2 KB
/
CDVWKInAppBrowser.m
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/
#import "CDVWKInAppBrowser.h"
#import <Cordova/NSDictionary+CordovaPreferences.h>
#import <Cordova/CDVWebViewProcessPoolFactory.h>
#import <Cordova/CDVPluginResult.h>
#define kInAppBrowserTargetSelf @"_self"
#define kInAppBrowserTargetSystem @"_system"
#define kInAppBrowserTargetBlank @"_blank"
#define kInAppBrowserToolbarBarPositionBottom @"bottom"
#define kInAppBrowserToolbarBarPositionTop @"top"
#define IAB_BRIDGE_NAME @"cordova_iab"
#define TOOLBAR_HEIGHT 44.0
#define LOCATIONBAR_HEIGHT 21.0
#define FOOTER_HEIGHT ((TOOLBAR_HEIGHT) + (LOCATIONBAR_HEIGHT))
#pragma mark CDVWKInAppBrowser
@implementation CDVWKInAppBrowser
static CDVWKInAppBrowser* instance = nil;
+ (id) getInstance{
return instance;
}
- (void)pluginInitialize
{
instance = self;
_callbackIdPattern = nil;
_beforeload = @"";
_waitForBeforeload = NO;
}
- (void)onReset
{
[self close:nil];
}
- (void)close:(CDVInvokedUrlCommand*)command
{
if (self.inAppBrowserViewController == nil) {
NSLog(@"IAB.close() called but it was already closed.");
return;
}
// Things are cleaned up in browserExit.
[self.inAppBrowserViewController close];
}
- (BOOL) isSystemUrl:(NSURL*)url
{
if ([[url host] isEqualToString:@"itunes.apple.com"]) {
return YES;
}
return NO;
}
- (void)open:(CDVInvokedUrlCommand*)command
{
CDVPluginResult* pluginResult;
NSString* url = [command argumentAtIndex:0];
NSString* target = [command argumentAtIndex:1 withDefault:kInAppBrowserTargetSelf];
NSString* options = [command argumentAtIndex:2 withDefault:@"" andClass:[NSString class]];
self.callbackId = command.callbackId;
if (url != nil) {
NSURL* baseUrl = [self.webViewEngine URL];
NSURL* absoluteUrl = [[NSURL URLWithString:url relativeToURL:baseUrl] absoluteURL];
if ([self isSystemUrl:absoluteUrl]) {
target = kInAppBrowserTargetSystem;
}
if ([target isEqualToString:kInAppBrowserTargetSelf]) {
[self openInCordovaWebView:absoluteUrl withOptions:options];
} else if ([target isEqualToString:kInAppBrowserTargetSystem]) {
[self openInSystem:absoluteUrl];
} else { // _blank or anything else
[self openInInAppBrowser:absoluteUrl withOptions:options];
}
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
} else {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"incorrect number of arguments"];
}
[pluginResult setKeepCallback:[NSNumber numberWithBool:YES]];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
- (void)openInInAppBrowser:(NSURL*)url withOptions:(NSString*)options
{
CDVInAppBrowserOptions* browserOptions = [CDVInAppBrowserOptions parseOptions:options];
WKWebsiteDataStore* dataStore = [WKWebsiteDataStore defaultDataStore];
if (browserOptions.cleardata) {
NSDate* dateFrom = [NSDate dateWithTimeIntervalSince1970:0];
[dataStore removeDataOfTypes:[WKWebsiteDataStore allWebsiteDataTypes] modifiedSince:dateFrom completionHandler:^{
NSLog(@"Removed all WKWebView data");
self.inAppBrowserViewController.webView.configuration.processPool = [[WKProcessPool alloc] init]; // create new process pool to flush all data
}];
}
if (browserOptions.clearcache) {
// Deletes all cookies
WKHTTPCookieStore* cookieStore = dataStore.httpCookieStore;
[cookieStore getAllCookies:^(NSArray* cookies) {
NSHTTPCookie* cookie;
for(cookie in cookies){
[cookieStore deleteCookie:cookie completionHandler:nil];
}
}];
}
if (browserOptions.clearsessioncache) {
// Deletes session cookies
WKHTTPCookieStore* cookieStore = dataStore.httpCookieStore;
[cookieStore getAllCookies:^(NSArray* cookies) {
NSHTTPCookie* cookie;
for(cookie in cookies){
if(cookie.sessionOnly){
[cookieStore deleteCookie:cookie completionHandler:nil];
}
}
}];
}
if (self.inAppBrowserViewController == nil) {
self.inAppBrowserViewController = [[CDVWKInAppBrowserViewController alloc] initWithBrowserOptions: browserOptions andSettings:self.commandDelegate.settings];
self.inAppBrowserViewController.navigationDelegate = self;
if ([self.viewController conformsToProtocol:@protocol(CDVScreenOrientationDelegate)]) {
self.inAppBrowserViewController.orientationDelegate = (UIViewController <CDVScreenOrientationDelegate>*)self.viewController;
}
}
[self.inAppBrowserViewController showLocationBar:browserOptions.location];
[self.inAppBrowserViewController showToolBar:browserOptions.toolbar :browserOptions.toolbarposition];
if (browserOptions.closebuttoncaption != nil || browserOptions.closebuttoncolor != nil) {
int closeButtonIndex = browserOptions.lefttoright ? (browserOptions.hidenavigationbuttons ? 1 : 4) : 0;
[self.inAppBrowserViewController setCloseButtonTitle:browserOptions.closebuttoncaption :browserOptions.closebuttoncolor :closeButtonIndex];
}
// Set Presentation Style
UIModalPresentationStyle presentationStyle = UIModalPresentationFullScreen; // default
if (browserOptions.presentationstyle != nil) {
if ([[browserOptions.presentationstyle lowercaseString] isEqualToString:@"pagesheet"]) {
presentationStyle = UIModalPresentationPageSheet;
} else if ([[browserOptions.presentationstyle lowercaseString] isEqualToString:@"formsheet"]) {
presentationStyle = UIModalPresentationFormSheet;
}
}
self.inAppBrowserViewController.modalPresentationStyle = presentationStyle;
// Set Transition Style
UIModalTransitionStyle transitionStyle = UIModalTransitionStyleCoverVertical; // default
if (browserOptions.transitionstyle != nil) {
if ([[browserOptions.transitionstyle lowercaseString] isEqualToString:@"fliphorizontal"]) {
transitionStyle = UIModalTransitionStyleFlipHorizontal;
} else if ([[browserOptions.transitionstyle lowercaseString] isEqualToString:@"crossdissolve"]) {
transitionStyle = UIModalTransitionStyleCrossDissolve;
}
}
self.inAppBrowserViewController.modalTransitionStyle = transitionStyle;
//prevent webView from bouncing
if (browserOptions.disallowoverscroll) {
if ([self.inAppBrowserViewController.webView respondsToSelector:@selector(scrollView)]) {
((UIScrollView*)[self.inAppBrowserViewController.webView scrollView]).bounces = NO;
} else {
for (id subview in self.inAppBrowserViewController.webView.subviews) {
if ([[subview class] isSubclassOfClass:[UIScrollView class]]) {
((UIScrollView*)subview).bounces = NO;
}
}
}
}
// use of beforeload event
if([browserOptions.beforeload isKindOfClass:[NSString class]]){
_beforeload = browserOptions.beforeload;
}else{
_beforeload = @"yes";
}
_waitForBeforeload = ![_beforeload isEqualToString:@""];
[self.inAppBrowserViewController navigateTo:url];
if (!browserOptions.hidden) {
[self show:nil withNoAnimate:browserOptions.hidden];
}
}
- (void)show:(CDVInvokedUrlCommand*)command{
[self show:command withNoAnimate:NO];
}
- (void)show:(CDVInvokedUrlCommand*)command withNoAnimate:(BOOL)noAnimate
{
BOOL initHidden = NO;
if(command == nil && noAnimate == YES){
initHidden = YES;
}
if (self.inAppBrowserViewController == nil) {
NSLog(@"Tried to show IAB after it was closed.");
return;
}
__block CDVInAppBrowserNavigationController* nav = [[CDVInAppBrowserNavigationController alloc]
initWithRootViewController:self.inAppBrowserViewController];
nav.orientationDelegate = self.inAppBrowserViewController;
nav.navigationBarHidden = YES;
nav.modalPresentationStyle = self.inAppBrowserViewController.modalPresentationStyle;
nav.presentationController.delegate = self.inAppBrowserViewController;
__weak CDVWKInAppBrowser* weakSelf = self;
// Run later to avoid the "took a long time" log message.
dispatch_async(dispatch_get_main_queue(), ^{
if (weakSelf.inAppBrowserViewController != nil) {
float osVersion = [[[UIDevice currentDevice] systemVersion] floatValue];
__strong __typeof(weakSelf) strongSelf = weakSelf;
if (!strongSelf->tmpWindow) {
if (@available(iOS 13.0, *)) {
UIWindowScene *scene = strongSelf.viewController.view.window.windowScene;
if (scene) {
strongSelf->tmpWindow = [[UIWindow alloc] initWithWindowScene:scene];
}
}
if (!strongSelf->tmpWindow) {
CGRect frame = [[UIScreen mainScreen] bounds];
if(initHidden && osVersion < 11){
frame.origin.x = -10000;
}
strongSelf->tmpWindow = [[UIWindow alloc] initWithFrame:frame];
}
}
UIViewController *tmpController = [[UIViewController alloc] init];
[strongSelf->tmpWindow setRootViewController:tmpController];
[strongSelf->tmpWindow setWindowLevel:UIWindowLevelNormal];
if(!initHidden || osVersion < 11){
[self->tmpWindow makeKeyAndVisible];
}
[tmpController presentViewController:nav animated:!noAnimate completion:nil];
}
});
}
- (void)hide:(CDVInvokedUrlCommand*)command
{
// Set tmpWindow to hidden to make main webview responsive to touch again
// https://stackoverflow.com/questions/4544489/how-to-remove-a-uiwindow
self->tmpWindow.hidden = YES;
self->tmpWindow = nil;
if (self.inAppBrowserViewController == nil) {
NSLog(@"Tried to hide IAB after it was closed.");
return;
}
// Run later to avoid the "took a long time" log message.
dispatch_async(dispatch_get_main_queue(), ^{
if (self.inAppBrowserViewController != nil) {
[self.inAppBrowserViewController.presentingViewController dismissViewControllerAnimated:YES completion:nil];
}
});
}
- (void)openInCordovaWebView:(NSURL*)url withOptions:(NSString*)options
{
NSURLRequest* request = [NSURLRequest requestWithURL:url];
// the webview engine itself will filter for this according to <allow-navigation> policy
// in config.xml
[self.webViewEngine loadRequest:request];
}
- (void)openInSystem:(NSURL*)url
{
[[UIApplication sharedApplication] openURL:url options:@{} completionHandler:^(BOOL success) {
if (!success) {
[[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:CDVPluginHandleOpenURLNotification object:url]];
}
}];
}
- (void)loadAfterBeforeload:(CDVInvokedUrlCommand*)command
{
NSString* urlStr = [command argumentAtIndex:0];
if ([_beforeload isEqualToString:@""]) {
NSLog(@"unexpected loadAfterBeforeload called without feature beforeload=get|post");
}
if (self.inAppBrowserViewController == nil) {
NSLog(@"Tried to invoke loadAfterBeforeload on IAB after it was closed.");
return;
}
if (urlStr == nil) {
NSLog(@"loadAfterBeforeload called with nil argument, ignoring.");
return;
}
NSURL* url = [NSURL URLWithString:urlStr];
//_beforeload = @"";
_waitForBeforeload = NO;
[self.inAppBrowserViewController navigateTo:url];
}
// This is a helper method for the inject{Script|Style}{Code|File} API calls, which
// provides a consistent method for injecting JavaScript code into the document.
//
// If a wrapper string is supplied, then the source string will be JSON-encoded (adding
// quotes) and wrapped using string formatting. (The wrapper string should have a single
// '%@' marker).
//
// If no wrapper is supplied, then the source string is executed directly.
- (void)injectDeferredObject:(NSString*)source withWrapper:(NSString*)jsWrapper
{
// Ensure a message handler bridge is created to communicate with the CDVWKInAppBrowserViewController
[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]];
if (jsWrapper != nil) {
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:@[source] options:0 error:nil];
NSString* sourceArrayString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
if (sourceArrayString) {
NSString* sourceString = [sourceArrayString substringWithRange:NSMakeRange(1, [sourceArrayString length] - 2)];
NSString* jsToInject = [NSString stringWithFormat:jsWrapper, sourceString];
[self evaluateJavaScript:jsToInject];
}
} else {
[self evaluateJavaScript:source];
}
}
//Synchronus helper for javascript evaluation
- (void)evaluateJavaScript:(NSString *)script {
__block NSString* _script = script;
[self.inAppBrowserViewController.webView evaluateJavaScript:script completionHandler:^(id result, NSError *error) {
if (error == nil) {
if (result != nil) {
NSLog(@"%@", result);
}
} else {
NSLog(@"evaluateJavaScript error : %@ : %@", error.localizedDescription, _script);
}
}];
}
- (void)injectScriptCode:(CDVInvokedUrlCommand*)command
{
NSString* jsWrapper = nil;
if ((command.callbackId != nil) && ![command.callbackId isEqualToString:@"INVALID"]) {
jsWrapper = [NSString stringWithFormat:@"_cdvMessageHandler('%@',JSON.stringify([eval(%%@)]));", command.callbackId];
}
[self injectDeferredObject:[command argumentAtIndex:0] withWrapper:jsWrapper];
}
- (void)injectScriptFile:(CDVInvokedUrlCommand*)command
{
NSString* jsWrapper;
if ((command.callbackId != nil) && ![command.callbackId isEqualToString:@"INVALID"]) {
jsWrapper = [NSString stringWithFormat:@"(function(d) { var c = d.createElement('script'); c.src = %%@; c.onload = function() { _cdvMessageHandler('%@'); }; d.body.appendChild(c); })(document)", command.callbackId];
} else {
jsWrapper = @"(function(d) { var c = d.createElement('script'); c.src = %@; d.body.appendChild(c); })(document)";
}
[self injectDeferredObject:[command argumentAtIndex:0] withWrapper:jsWrapper];
}
- (void)injectStyleCode:(CDVInvokedUrlCommand*)command
{
NSString* jsWrapper;
if ((command.callbackId != nil) && ![command.callbackId isEqualToString:@"INVALID"]) {
jsWrapper = [NSString stringWithFormat:@"(function(d) { var c = d.createElement('style'); c.innerHTML = %%@; c.onload = function() { _cdvMessageHandler('%@'); }; d.body.appendChild(c); })(document)", command.callbackId];
} else {
jsWrapper = @"(function(d) { var c = d.createElement('style'); c.innerHTML = %@; d.body.appendChild(c); })(document)";
}
[self injectDeferredObject:[command argumentAtIndex:0] withWrapper:jsWrapper];
}
- (void)injectStyleFile:(CDVInvokedUrlCommand*)command
{
NSString* jsWrapper;
if ((command.callbackId != nil) && ![command.callbackId isEqualToString:@"INVALID"]) {
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];
} else {
jsWrapper = @"(function(d) { var c = d.createElement('link'); c.rel='stylesheet', c.type='text/css'; c.href = %@; d.body.appendChild(c); })(document)";
}
[self injectDeferredObject:[command argumentAtIndex:0] withWrapper:jsWrapper];
}
- (BOOL)isValidCallbackId:(NSString *)callbackId
{
NSError *err = nil;
// Initialize on first use
if (self.callbackIdPattern == nil) {
self.callbackIdPattern = [NSRegularExpression regularExpressionWithPattern:@"^InAppBrowser[0-9]{1,10}$" options:0 error:&err];
if (err != nil) {
// Couldn't initialize Regex; No is safer than Yes.
return NO;
}
}
if ([self.callbackIdPattern firstMatchInString:callbackId options:0 range:NSMakeRange(0, [callbackId length])]) {
return YES;
}
return NO;
}
/**
* The message handler bridge provided for the InAppBrowser is capable of executing any oustanding callback belonging
* to the InAppBrowser plugin. Care has been taken that other callbacks cannot be triggered, and that no
* other code execution is possible.
*/
- (void)webView:(WKWebView *)theWebView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
NSURL* url = navigationAction.request.URL;
NSURL* mainDocumentURL = navigationAction.request.mainDocumentURL;
BOOL isTopLevelNavigation = [url isEqual:mainDocumentURL];
BOOL shouldStart = YES;
BOOL useBeforeLoad = NO;
NSString* httpMethod = navigationAction.request.HTTPMethod;
NSString* errorMessage = nil;
if([_beforeload isEqualToString:@"post"]){
//TODO handle POST requests by preserving POST data then remove this condition
errorMessage = @"beforeload doesn't yet support POST requests";
}
else if(isTopLevelNavigation && (
[_beforeload isEqualToString:@"yes"]
|| ([_beforeload isEqualToString:@"get"] && [httpMethod isEqualToString:@"GET"])
// TODO comment in when POST requests are handled
// || ([_beforeload isEqualToString:@"post"] && [httpMethod isEqualToString:@"POST"])
)){
useBeforeLoad = YES;
}
// When beforeload, on first URL change, initiate JS callback. Only after the beforeload event, continue.
if (_waitForBeforeload && useBeforeLoad) {
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
messageAsDictionary:@{@"type":@"beforeload", @"url":[url absoluteString]}];
[pluginResult setKeepCallback:[NSNumber numberWithBool:YES]];
[self.commandDelegate sendPluginResult:pluginResult callbackId:self.callbackId];
decisionHandler(WKNavigationActionPolicyCancel);
return;
}
if(errorMessage != nil){
NSLog(errorMessage);
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR
messageAsDictionary:@{@"type":@"loaderror", @"url":[url absoluteString], @"code": @"-1", @"message": errorMessage}];
[pluginResult setKeepCallback:[NSNumber numberWithBool:YES]];
[self.commandDelegate sendPluginResult:pluginResult callbackId:self.callbackId];
}
//if is an app store, tel, sms, mailto or geo link, let the system handle it, otherwise it fails to load it
NSArray * allowedSchemes = @[@"itms-appss", @"itms-apps", @"tel", @"sms", @"mailto", @"geo"];
if ([allowedSchemes containsObject:[url scheme]]) {
[theWebView stopLoading];
[self openInSystem:url];
shouldStart = NO;
}
else if ((self.callbackId != nil) && isTopLevelNavigation) {
// Send a loadstart event for each top-level navigation (includes redirects).
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
messageAsDictionary:@{@"type":@"loadstart", @"url":[url absoluteString]}];
[pluginResult setKeepCallback:[NSNumber numberWithBool:YES]];
[self.commandDelegate sendPluginResult:pluginResult callbackId:self.callbackId];
}
if (useBeforeLoad) {
_waitForBeforeload = YES;
}
if(shouldStart){
// Fix GH-417 & GH-424: Handle non-default target attribute
// Based on https://stackoverflow.com/a/25713070/777265
if (!navigationAction.targetFrame){
[theWebView loadRequest:navigationAction.request];
decisionHandler(WKNavigationActionPolicyCancel);
}else{
decisionHandler(WKNavigationActionPolicyAllow);
}
}else{
decisionHandler(WKNavigationActionPolicyCancel);
}
}
#pragma mark WKScriptMessageHandler delegate
- (void)userContentController:(nonnull WKUserContentController *)userContentController didReceiveScriptMessage:(nonnull WKScriptMessage *)message {
CDVPluginResult* pluginResult = nil;
if([message.body isKindOfClass:[NSDictionary class]]){
NSDictionary* messageContent = (NSDictionary*) message.body;
NSString* scriptCallbackId = messageContent[@"id"];
if([messageContent objectForKey:@"d"]){
NSString* scriptResult = messageContent[@"d"];
NSError* __autoreleasing error = nil;
NSData* decodedResult = [NSJSONSerialization JSONObjectWithData:[scriptResult dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:&error];
if ((error == nil) && [decodedResult isKindOfClass:[NSArray class]]) {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsArray:(NSArray*)decodedResult];
} else {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_JSON_EXCEPTION];
}
} else {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsArray:@[]];
}
[self.commandDelegate sendPluginResult:pluginResult callbackId:scriptCallbackId];
}else if(self.callbackId != nil){
// Send a message event
NSString* messageContent = (NSString*) message.body;
NSError* __autoreleasing error = nil;
NSData* decodedResult = [NSJSONSerialization JSONObjectWithData:[messageContent dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:&error];
if (error == nil) {
NSMutableDictionary* dResult = [NSMutableDictionary new];
[dResult setValue:@"message" forKey:@"type"];
[dResult setObject:decodedResult forKey:@"data"];
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dResult];
[pluginResult setKeepCallback:[NSNumber numberWithBool:YES]];
[self.commandDelegate sendPluginResult:pluginResult callbackId:self.callbackId];
}
}
}
- (void)didStartProvisionalNavigation:(WKWebView*)theWebView
{
NSLog(@"didStartProvisionalNavigation");
// self.inAppBrowserViewController.currentURL = theWebView.URL;
}
- (void)didFinishNavigation:(WKWebView*)theWebView
{
if (self.callbackId != nil) {
NSString* url = [theWebView.URL absoluteString];
if(url == nil){
if(self.inAppBrowserViewController.currentURL != nil){
url = [self.inAppBrowserViewController.currentURL absoluteString];
}else{
url = @"";
}
}
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
messageAsDictionary:@{@"type":@"loadstop", @"url":url}];
[pluginResult setKeepCallback:[NSNumber numberWithBool:YES]];
[self.commandDelegate sendPluginResult:pluginResult callbackId:self.callbackId];
}
}
- (void)webView:(WKWebView*)theWebView didFailNavigation:(NSError*)error
{
if (self.callbackId != nil) {
NSString* url = [theWebView.URL absoluteString];
if(url == nil){
if(self.inAppBrowserViewController.currentURL != nil){
url = [self.inAppBrowserViewController.currentURL absoluteString];
}else{
url = @"";
}
}
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR
messageAsDictionary:@{@"type":@"loaderror", @"url":url, @"code": [NSNumber numberWithInteger:error.code], @"message": error.localizedDescription}];
[pluginResult setKeepCallback:[NSNumber numberWithBool:YES]];
[self.commandDelegate sendPluginResult:pluginResult callbackId:self.callbackId];
}
}
- (void)browserExit
{
if (self.callbackId != nil) {
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
messageAsDictionary:@{@"type":@"exit"}];
[self.commandDelegate sendPluginResult:pluginResult callbackId:self.callbackId];
self.callbackId = nil;
}
[self.inAppBrowserViewController.configuration.userContentController removeScriptMessageHandlerForName:IAB_BRIDGE_NAME];
self.inAppBrowserViewController.configuration = nil;
[self.inAppBrowserViewController.webView stopLoading];
[self.inAppBrowserViewController.webView removeFromSuperview];
[self.inAppBrowserViewController.webView setUIDelegate:nil];
[self.inAppBrowserViewController.webView setNavigationDelegate:nil];
self.inAppBrowserViewController.webView = nil;
// Set navigationDelegate to nil to ensure no callbacks are received from it.
self.inAppBrowserViewController.navigationDelegate = nil;
self.inAppBrowserViewController = nil;
// Set tmpWindow to hidden to make main webview responsive to touch again
// Based on https://stackoverflow.com/questions/4544489/how-to-remove-a-uiwindow
self->tmpWindow.hidden = YES;
self->tmpWindow = nil;
}
@end //CDVWKInAppBrowser
#pragma mark CDVWKInAppBrowserViewController
@implementation CDVWKInAppBrowserViewController
@synthesize currentURL;
CGFloat lastReducedStatusBarHeight = 0.0;
BOOL isExiting = FALSE;
- (id)initWithBrowserOptions: (CDVInAppBrowserOptions*) browserOptions andSettings:(NSDictionary *)settings
{
self = [super init];
if (self != nil) {
_browserOptions = browserOptions;
_settings = settings;
self.webViewUIDelegate = [[CDVWKInAppBrowserUIDelegate alloc] initWithTitle:[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDisplayName"]];
[self.webViewUIDelegate setViewController:self];
[self createViews];
}
return self;
}
-(void)dealloc {
//NSLog(@"dealloc");
}
- (void)createViews
{
// We create the views in code for primarily for ease of upgrades and not requiring an external .xib to be included
CGRect webViewBounds = self.view.bounds;
BOOL toolbarIsAtBottom = ![_browserOptions.toolbarposition isEqualToString:kInAppBrowserToolbarBarPositionTop];
webViewBounds.size.height -= _browserOptions.location ? FOOTER_HEIGHT : TOOLBAR_HEIGHT;
WKUserContentController* userContentController = [[WKUserContentController alloc] init];
WKWebViewConfiguration* configuration = [[WKWebViewConfiguration alloc] init];
NSString *userAgent = configuration.applicationNameForUserAgent;
if (
[self settingForKey:@"OverrideUserAgent"] == nil &&
[self settingForKey:@"AppendUserAgent"] != nil
) {
userAgent = [NSString stringWithFormat:@"%@ %@", userAgent, [self settingForKey:@"AppendUserAgent"]];
}
configuration.applicationNameForUserAgent = userAgent;
configuration.userContentController = userContentController;
#if __has_include(<Cordova/CDVWebViewProcessPoolFactory.h>)
configuration.processPool = [[CDVWebViewProcessPoolFactory sharedFactory] sharedProcessPool];
#elif __has_include("CDVWKProcessPoolFactory.h")
configuration.processPool = [[CDVWKProcessPoolFactory sharedFactory] sharedProcessPool];
#endif
[configuration.userContentController addScriptMessageHandler:self name:IAB_BRIDGE_NAME];
//WKWebView options
configuration.allowsInlineMediaPlayback = _browserOptions.allowinlinemediaplayback;
configuration.ignoresViewportScaleLimits = _browserOptions.enableviewportscale;
if(_browserOptions.mediaplaybackrequiresuseraction == YES){
configuration.mediaTypesRequiringUserActionForPlayback = WKAudiovisualMediaTypeAll;
}else{
configuration.mediaTypesRequiringUserActionForPlayback = WKAudiovisualMediaTypeNone;
}
if (@available(iOS 13.0, *)) {
NSString *contentMode = [self settingForKey:@"PreferredContentMode"];
if ([contentMode isEqual: @"mobile"]) {
configuration.defaultWebpagePreferences.preferredContentMode = WKContentModeMobile;
} else if ([contentMode isEqual: @"desktop"]) {
configuration.defaultWebpagePreferences.preferredContentMode = WKContentModeDesktop;
}
}
self.webView = [[WKWebView alloc] initWithFrame:webViewBounds configuration:configuration];
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 160400
// With the introduction of iOS 16.4 the webview is no longer inspectable by default.
// We'll honor that change for release builds, but will still allow inspection on debug builds by default.
// We also introduce an override option, so consumers can influence this decision in their own build.
if (@available(iOS 16.4, *)) {
#ifdef DEBUG
BOOL allowWebviewInspectionDefault = YES;
#else
BOOL allowWebviewInspectionDefault = NO;
#endif
self.webView.inspectable = [_settings cordovaBoolSettingForKey:@"InspectableWebview" defaultValue:allowWebviewInspectionDefault];
}
#endif
[self.view addSubview:self.webView];
[self.view sendSubviewToBack:self.webView];
self.webView.navigationDelegate = self;
self.webView.UIDelegate = self.webViewUIDelegate;
self.webView.backgroundColor = [UIColor whiteColor];
if ([self settingForKey:@"OverrideUserAgent"] != nil) {
self.webView.customUserAgent = [self settingForKey:@"OverrideUserAgent"];
}
self.webView.clearsContextBeforeDrawing = YES;
self.webView.clipsToBounds = YES;
self.webView.contentMode = UIViewContentModeScaleToFill;
self.webView.multipleTouchEnabled = YES;
self.webView.opaque = YES;
self.webView.userInteractionEnabled = YES;
self.automaticallyAdjustsScrollViewInsets = YES ;
[self.webView setAutoresizingMask:UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth];
self.webView.allowsLinkPreview = NO;
self.webView.allowsBackForwardNavigationGestures = NO;
[self.webView.scrollView setContentInsetAdjustmentBehavior:UIScrollViewContentInsetAdjustmentNever];
self.spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
self.spinner.alpha = 1.000;
self.spinner.autoresizesSubviews = YES;
self.spinner.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleRightMargin);
self.spinner.clearsContextBeforeDrawing = NO;
self.spinner.clipsToBounds = NO;
self.spinner.contentMode = UIViewContentModeScaleToFill;
self.spinner.frame = CGRectMake(CGRectGetMidX(self.webView.frame), CGRectGetMidY(self.webView.frame), 20.0, 20.0);
self.spinner.hidden = NO;
self.spinner.hidesWhenStopped = YES;
self.spinner.multipleTouchEnabled = NO;
self.spinner.opaque = NO;
self.spinner.userInteractionEnabled = NO;
[self.spinner stopAnimating];
self.closeButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(close)];
self.closeButton.enabled = YES;
UIBarButtonItem* flexibleSpaceButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem* fixedSpaceButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
fixedSpaceButton.width = 20;
float toolbarY = toolbarIsAtBottom ? self.view.bounds.size.height - TOOLBAR_HEIGHT : 0.0;
CGRect toolbarFrame = CGRectMake(0.0, toolbarY, self.view.bounds.size.width, TOOLBAR_HEIGHT);
self.toolbar = [[UIToolbar alloc] initWithFrame:toolbarFrame];
self.toolbar.alpha = 1.000;
self.toolbar.autoresizesSubviews = YES;
self.toolbar.autoresizingMask = toolbarIsAtBottom ? (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin) : UIViewAutoresizingFlexibleWidth;
self.toolbar.barStyle = UIBarStyleBlackOpaque;
self.toolbar.clearsContextBeforeDrawing = NO;
self.toolbar.clipsToBounds = NO;
self.toolbar.contentMode = UIViewContentModeScaleToFill;
self.toolbar.hidden = NO;
self.toolbar.multipleTouchEnabled = NO;
self.toolbar.opaque = NO;
self.toolbar.userInteractionEnabled = YES;
if (_browserOptions.toolbarcolor != nil) { // Set toolbar color if user sets it in options
self.toolbar.barTintColor = [self colorFromHexString:_browserOptions.toolbarcolor];
}
if (!_browserOptions.toolbartranslucent) { // Set toolbar translucent to no if user sets it in options
self.toolbar.translucent = NO;
}
CGFloat labelInset = 5.0;
float locationBarY = toolbarIsAtBottom ? self.view.bounds.size.height - FOOTER_HEIGHT : self.view.bounds.size.height - LOCATIONBAR_HEIGHT;
self.addressLabel = [[UILabel alloc] initWithFrame:CGRectMake(labelInset, locationBarY, self.view.bounds.size.width - labelInset, LOCATIONBAR_HEIGHT)];
self.addressLabel.adjustsFontSizeToFitWidth = NO;
self.addressLabel.alpha = 1.000;
self.addressLabel.autoresizesSubviews = YES;
self.addressLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin;
self.addressLabel.backgroundColor = [UIColor clearColor];
self.addressLabel.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
self.addressLabel.clearsContextBeforeDrawing = YES;
self.addressLabel.clipsToBounds = YES;
self.addressLabel.contentMode = UIViewContentModeScaleToFill;
self.addressLabel.enabled = YES;
self.addressLabel.hidden = NO;
self.addressLabel.lineBreakMode = NSLineBreakByTruncatingTail;
if ([self.addressLabel respondsToSelector:NSSelectorFromString(@"setMinimumScaleFactor:")]) {
[self.addressLabel setValue:@(10.0/[UIFont labelFontSize]) forKey:@"minimumScaleFactor"];
} else if ([self.addressLabel respondsToSelector:NSSelectorFromString(@"setMinimumFontSize:")]) {
[self.addressLabel setValue:@(10.0) forKey:@"minimumFontSize"];
}
self.addressLabel.multipleTouchEnabled = NO;
self.addressLabel.numberOfLines = 1;
self.addressLabel.opaque = NO;
self.addressLabel.shadowOffset = CGSizeMake(0.0, -1.0);
self.addressLabel.text = NSLocalizedString(@"Loading...", nil);
self.addressLabel.textAlignment = NSTextAlignmentLeft;
self.addressLabel.textColor = [UIColor colorWithWhite:1.000 alpha:1.000];
self.addressLabel.userInteractionEnabled = NO;
NSString* frontArrowString = NSLocalizedString(@"►", nil); // create arrow from Unicode char
self.forwardButton = [[UIBarButtonItem alloc] initWithTitle:frontArrowString style:UIBarButtonItemStylePlain target:self action:@selector(goForward:)];
self.forwardButton.enabled = YES;
self.forwardButton.imageInsets = UIEdgeInsetsZero;
if (_browserOptions.navigationbuttoncolor != nil) { // Set button color if user sets it in options
self.forwardButton.tintColor = [self colorFromHexString:_browserOptions.navigationbuttoncolor];
}
NSString* backArrowString = NSLocalizedString(@"◄", nil); // create arrow from Unicode char
self.backButton = [[UIBarButtonItem alloc] initWithTitle:backArrowString style:UIBarButtonItemStylePlain target:self action:@selector(goBack:)];
self.backButton.enabled = YES;
self.backButton.imageInsets = UIEdgeInsetsZero;
if (_browserOptions.navigationbuttoncolor != nil) { // Set button color if user sets it in options
self.backButton.tintColor = [self colorFromHexString:_browserOptions.navigationbuttoncolor];
}
// Filter out Navigation Buttons if user requests so
if (_browserOptions.hidenavigationbuttons) {
if (_browserOptions.lefttoright) {
[self.toolbar setItems:@[flexibleSpaceButton, self.closeButton]];
} else {
[self.toolbar setItems:@[self.closeButton, flexibleSpaceButton]];
}
} else if (_browserOptions.lefttoright) {
[self.toolbar setItems:@[self.backButton, fixedSpaceButton, self.forwardButton, flexibleSpaceButton, self.closeButton]];
} else {
[self.toolbar setItems:@[self.closeButton, flexibleSpaceButton, self.backButton, fixedSpaceButton, self.forwardButton]];
}
self.view.backgroundColor = [UIColor clearColor];
[self.view addSubview:self.toolbar];
[self.view addSubview:self.addressLabel];
[self.view addSubview:self.spinner];
}
- (id)settingForKey:(NSString*)key
{
return [_settings objectForKey:[key lowercaseString]];
}
- (void) setWebViewFrame : (CGRect) frame {
NSLog(@"Setting the WebView's frame to %@", NSStringFromCGRect(frame));
[self.webView setFrame:frame];
}
- (void)setCloseButtonTitle:(NSString*)title : (NSString*) colorString : (int) buttonIndex
{
// the advantage of using UIBarButtonSystemItemDone is the system will localize it for you automatically
// 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)
self.closeButton = nil;
// Initialize with title if title is set, otherwise the title will be 'Done' localized
self.closeButton = title != nil ? [[UIBarButtonItem alloc] initWithTitle:title style:UIBarButtonItemStylePlain target:self action:@selector(close)] : [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(close)];
self.closeButton.enabled = YES;
// If color on closebutton is requested then initialize with that that color, otherwise use initialize with default
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];
NSMutableArray* items = [self.toolbar.items mutableCopy];
[items replaceObjectAtIndex:buttonIndex withObject:self.closeButton];
[self.toolbar setItems:items];
}
- (void)showLocationBar:(BOOL)show
{
CGRect locationbarFrame = self.addressLabel.frame;
BOOL toolbarVisible = !self.toolbar.hidden;
// prevent double show/hide
if (show == !(self.addressLabel.hidden)) {
return;
}
if (show) {
self.addressLabel.hidden = NO;
if (toolbarVisible) {
// toolBar at the bottom, leave as is
// put locationBar on top of the toolBar
CGRect webViewBounds = self.view.bounds;
webViewBounds.size.height -= FOOTER_HEIGHT;
[self setWebViewFrame:webViewBounds];
locationbarFrame.origin.y = webViewBounds.size.height;
self.addressLabel.frame = locationbarFrame;
} else {
// no toolBar, so put locationBar at the bottom
CGRect webViewBounds = self.view.bounds;
webViewBounds.size.height -= LOCATIONBAR_HEIGHT;
[self setWebViewFrame:webViewBounds];
locationbarFrame.origin.y = webViewBounds.size.height;
self.addressLabel.frame = locationbarFrame;
}
} else {
self.addressLabel.hidden = YES;
if (toolbarVisible) {
// locationBar is on top of toolBar, hide locationBar
// webView take up whole height less toolBar height
CGRect webViewBounds = self.view.bounds;
webViewBounds.size.height -= TOOLBAR_HEIGHT;
[self setWebViewFrame:webViewBounds];
} else {
// no toolBar, expand webView to screen dimensions
[self setWebViewFrame:self.view.bounds];
}
}
}
- (void)showToolBar:(BOOL)show : (NSString *) toolbarPosition
{
CGRect toolbarFrame = self.toolbar.frame;
CGRect locationbarFrame = self.addressLabel.frame;
BOOL locationbarVisible = !self.addressLabel.hidden;
// prevent double show/hide
if (show == !(self.toolbar.hidden)) {
return;
}
if (show) {
self.toolbar.hidden = NO;
CGRect webViewBounds = self.view.bounds;
if (locationbarVisible) {
// locationBar at the bottom, move locationBar up
// put toolBar at the bottom
webViewBounds.size.height -= FOOTER_HEIGHT;
locationbarFrame.origin.y = webViewBounds.size.height;
self.addressLabel.frame = locationbarFrame;
self.toolbar.frame = toolbarFrame;
} else {
// no locationBar, so put toolBar at the bottom
CGRect webViewBounds = self.view.bounds;
webViewBounds.size.height -= TOOLBAR_HEIGHT;
self.toolbar.frame = toolbarFrame;
}
if ([toolbarPosition isEqualToString:kInAppBrowserToolbarBarPositionTop]) {
toolbarFrame.origin.y = 0;
webViewBounds.origin.y += toolbarFrame.size.height;
[self setWebViewFrame:webViewBounds];
} else {
toolbarFrame.origin.y = (webViewBounds.size.height + LOCATIONBAR_HEIGHT);
}
[self setWebViewFrame:webViewBounds];
} else {
self.toolbar.hidden = YES;
if (locationbarVisible) {
// locationBar is on top of toolBar, hide toolBar
// put locationBar at the bottom
// webView take up whole height less locationBar height
CGRect webViewBounds = self.view.bounds;
webViewBounds.size.height -= LOCATIONBAR_HEIGHT;
[self setWebViewFrame:webViewBounds];
// move locationBar down
locationbarFrame.origin.y = webViewBounds.size.height;
self.addressLabel.frame = locationbarFrame;
} else {
// no locationBar, expand webView to screen dimensions
[self setWebViewFrame:self.view.bounds];
}
}
}