@@ -477,7 +477,7 @@ - (CDVWebViewPermissionGrantType)parsePermissionGrantType:(NSString*)optionStrin
477
477
return result;
478
478
}
479
479
480
- #pragma mark WKScriptMessageHandler implementation
480
+ #pragma mark - WKScriptMessageHandler implementation
481
481
482
482
- (void )userContentController : (WKUserContentController *)userContentController didReceiveScriptMessage : (WKScriptMessage *)message
483
483
{
@@ -513,7 +513,7 @@ - (void)userContentController:(WKUserContentController*)userContentController di
513
513
}
514
514
}
515
515
516
- #pragma mark WKNavigationDelegate implementation
516
+ #pragma mark - WKNavigationDelegate implementation
517
517
518
518
- (void )webView : (WKWebView *)webView didStartProvisionalNavigation : (WKNavigation *)navigation
519
519
{
@@ -561,45 +561,80 @@ - (BOOL)defaultResourcePolicyForURL:(NSURL*)url
561
561
return NO ;
562
562
}
563
563
564
- - (void ) webView : (WKWebView *) webView decidePolicyForNavigationAction : (WKNavigationAction *) navigationAction decisionHandler : (void (^)(WKNavigationActionPolicy )) decisionHandler
564
+ - (void )webView : (WKWebView *)webView decidePolicyForNavigationAction : (WKNavigationAction *) navigationAction decisionHandler : (void (^)(WKNavigationActionPolicy ))decisionHandler
565
565
{
566
- NSURL * url = [navigationAction.request URL ];
567
- CDVViewController* vc = (CDVViewController*)self.viewController ;
566
+ CDVViewController *vc = (CDVViewController *)self.viewController ;
568
567
569
- /*
570
- * Give plugins the chance to handle the url
571
- */
572
- BOOL anyPluginsResponded = NO ;
573
- BOOL shouldAllowRequest = NO ;
568
+ NSURLRequest *request = navigationAction.request ;
569
+ CDVWebViewNavigationType navType = (CDVWebViewNavigationType)navigationAction.navigationType ;
570
+ NSMutableDictionary *info = [NSMutableDictionary dictionary ];
571
+ info[@" sourceFrame" ] = navigationAction.sourceFrame ;
572
+ info[@" targetFrame" ] = navigationAction.targetFrame ;
573
+ #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 140500
574
+ if (@available (iOS 14.5 , *)) {
575
+ info[@" shouldPerformDownload" ] = [NSNumber numberWithBool: navigationAction.shouldPerformDownload];
576
+ }
577
+ #endif
574
578
575
- for (CDVPlugin *plugin in vc.enumerablePlugins ) {
576
- SEL selector = NSSelectorFromString (@" shouldOverrideLoadWithRequest:navigationType:" );
577
- if ([plugin respondsToSelector: selector]) {
578
- anyPluginsResponded = YES ;
579
- // https://issues.apache.org/jira/browse/CB-12497
580
- int navType = (int )navigationAction.navigationType ;
581
- shouldAllowRequest = (((BOOL (*)(id , SEL , id , int ))objc_msgSend)(plugin, selector, navigationAction.request , navType));
582
- if (!shouldAllowRequest) {
583
- break ;
579
+ // Give plugins the chance to handle the url, as long as this WebViewEngine is still the WKNavigationDelegate.
580
+ // This allows custom delegates to choose to call this method for `default` cordova behavior without querying all plugins.
581
+ if (webView.navigationDelegate == self) {
582
+ BOOL anyPluginsResponded = NO ;
583
+ BOOL shouldAllowRequest = NO ;
584
+
585
+ for (CDVPlugin *plugin in vc.enumerablePlugins ) {
586
+ if ([plugin respondsToSelector: @selector (shouldOverrideLoadWithRequest:navigationType:info: )] || [plugin respondsToSelector: @selector (shouldOverrideLoadWithRequest:navigationType: )]) {
587
+ CDVPlugin <CDVPluginNavigationHandler> *navPlugin = (CDVPlugin <CDVPluginNavigationHandler> *)plugin;
588
+ anyPluginsResponded = YES ;
589
+
590
+ if ([navPlugin respondsToSelector: @selector (shouldOverrideLoadWithRequest:navigationType:info: )]) {
591
+ shouldAllowRequest = [navPlugin shouldOverrideLoadWithRequest: request navigationType: navType info: info];
592
+ } else {
593
+ #pragma clang diagnostic push
594
+ #pragma clang diagnostic ignored "-Wdeprecated-declarations"
595
+ shouldAllowRequest = [navPlugin shouldOverrideLoadWithRequest: request navigationType: navType];
596
+ #pragma clang diagnostic pop
597
+ }
598
+
599
+ if (!shouldAllowRequest) {
600
+ break ;
601
+ }
584
602
}
585
603
}
604
+
605
+ if (anyPluginsResponded) {
606
+ return decisionHandler (shouldAllowRequest ? WKNavigationActionPolicyAllow : WKNavigationActionPolicyCancel );
607
+ }
608
+ } else {
609
+ CDVPlugin <CDVPluginNavigationHandler> *intentAndNavFilter = (CDVPlugin <CDVPluginNavigationHandler> *)[vc getCommandInstance: @" IntentAndNavigationFilter" ];
610
+ if (intentAndNavFilter) {
611
+ BOOL shouldAllowRequest = [intentAndNavFilter shouldOverrideLoadWithRequest: request navigationType: navType info: info];
612
+ return decisionHandler (shouldAllowRequest ? WKNavigationActionPolicyAllow : WKNavigationActionPolicyCancel );
613
+ }
586
614
}
587
615
588
- if (anyPluginsResponded) {
589
- return decisionHandler (shouldAllowRequest);
616
+ // Handle all other types of urls (tel:, sms:), and requests to load a url in the main webview.
617
+ BOOL shouldAllowNavigation = [self defaultResourcePolicyForURL: request.URL];
618
+ if (!shouldAllowNavigation) {
619
+ [[NSNotificationCenter defaultCenter ] postNotification: [NSNotification notificationWithName: CDVPluginHandleOpenURLNotification object: request.URL userInfo: @{}]];
590
620
}
621
+ return decisionHandler (shouldAllowNavigation ? WKNavigationActionPolicyAllow : WKNavigationActionPolicyCancel );
622
+ }
591
623
592
- /*
593
- * Handle all other types of urls (tel:, sms:), and requests to load a url in the main webview.
594
- */
595
- BOOL shouldAllowNavigation = [self defaultResourcePolicyForURL: url];
596
- if (shouldAllowNavigation) {
597
- return decisionHandler (YES );
598
- } else {
599
- [[NSNotificationCenter defaultCenter ] postNotification: [NSNotification notificationWithName: CDVPluginHandleOpenURLNotification object: url]];
624
+ - (void )webView : (WKWebView *)webView didReceiveAuthenticationChallenge : (NSURLAuthenticationChallenge *)challenge completionHandler : (void (^)(NSURLSessionAuthChallengeDisposition , NSURLCredential * _Nullable))completionHandler
625
+ {
626
+ CDVViewController* vc = (CDVViewController*)self.viewController ;
627
+
628
+ for (CDVPlugin *plugin in vc.enumerablePlugins ) {
629
+ if ([plugin respondsToSelector: @selector (willHandleAuthenticationChallenge:completionHandler: )]) {
630
+ CDVPlugin <CDVPluginAuthenticationHandler> *challengePlugin = (CDVPlugin <CDVPluginAuthenticationHandler> *)plugin;
631
+ if ([challengePlugin willHandleAuthenticationChallenge: challenge completionHandler: completionHandler]) {
632
+ return ;
633
+ }
634
+ }
600
635
}
601
636
602
- return decisionHandler ( NO );
637
+ completionHandler ( NSURLSessionAuthChallengePerformDefaultHandling , nil );
603
638
}
604
639
605
640
#pragma mark - Plugin interface
0 commit comments