Repository: cordova-plugin-wkwebview-engine
Updated Branches:
  refs/heads/master 13b5a0837 -> 76983f1de


CB-10002 - WKWebView should propagate shouldOverrideLoadWithRequest to plugins


Project: 
http://git-wip-us.apache.org/repos/asf/cordova-plugin-wkwebview-engine/repo
Commit: 
http://git-wip-us.apache.org/repos/asf/cordova-plugin-wkwebview-engine/commit/76983f1d
Tree: 
http://git-wip-us.apache.org/repos/asf/cordova-plugin-wkwebview-engine/tree/76983f1d
Diff: 
http://git-wip-us.apache.org/repos/asf/cordova-plugin-wkwebview-engine/diff/76983f1d

Branch: refs/heads/master
Commit: 76983f1de406a2e1aa418053b3fa2053b7bd4630
Parents: 13b5a08
Author: Shazron Abdullah <[email protected]>
Authored: Tue Nov 17 17:04:12 2015 -0800
Committer: Shazron Abdullah <[email protected]>
Committed: Tue Nov 17 17:04:12 2015 -0800

----------------------------------------------------------------------
 src/ios/CDVWKWebViewEngine.m | 49 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 49 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-plugin-wkwebview-engine/blob/76983f1d/src/ios/CDVWKWebViewEngine.m
----------------------------------------------------------------------
diff --git a/src/ios/CDVWKWebViewEngine.m b/src/ios/CDVWKWebViewEngine.m
index 81cd7fe..a399b37 100644
--- a/src/ios/CDVWKWebViewEngine.m
+++ b/src/ios/CDVWKWebViewEngine.m
@@ -256,4 +256,53 @@
     [[NSNotificationCenter defaultCenter] postNotification:[NSNotification 
notificationWithName:CDVPageDidLoadNotification object:webView]];
 }
 
+- (BOOL)defaultResourcePolicyForURL:(NSURL*)url
+{
+    // all file:// urls are allowed
+    if ([url isFileURL]) {
+        return YES;
+    }
+    
+    return NO;
+}
+
+- (void) webView: (WKWebView *) webView decidePolicyForNavigationAction: 
(WKNavigationAction*) navigationAction decisionHandler: (void 
(^)(WKNavigationActionPolicy)) decisionHandler
+{
+    NSURL* url = [navigationAction.request URL];
+    CDVViewController* vc = (CDVViewController*)self.viewController;
+    
+    /*
+     * Give plugins the chance to handle the url
+     */
+    BOOL anyPluginsResponded = NO;
+    BOOL shouldAllowRequest = NO;
+    
+    for (NSString* pluginName in vc.pluginObjects) {
+        CDVPlugin* plugin = [vc.pluginObjects objectForKey:pluginName];
+        SEL selector = 
NSSelectorFromString(@"shouldOverrideLoadWithRequest:navigationType:");
+        if ([plugin respondsToSelector:selector]) {
+            anyPluginsResponded = YES;
+            shouldAllowRequest = (((BOOL (*)(id, SEL, id, 
int))objc_msgSend)(plugin, selector, navigationAction.request, 
navigationAction.navigationType));
+            if (!shouldAllowRequest) {
+                break;
+            }
+        }
+    }
+    
+    if (anyPluginsResponded) {
+        return decisionHandler(shouldAllowRequest);
+    }
+    
+    /*
+     * Handle all other types of urls (tel:, sms:), and requests to load a url 
in the main webview.
+     */
+    BOOL shouldAllowNavigation = [self defaultResourcePolicyForURL:url];
+    if (shouldAllowNavigation) {
+        return decisionHandler(YES);
+    } else {
+        [[NSNotificationCenter defaultCenter] postNotification:[NSNotification 
notificationWithName:CDVPluginHandleOpenURLNotification object:url]];
+    }
+    
+    return decisionHandler(NO);
+}
 @end


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to