You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by sh...@apache.org on 2015/09/11 11:45:36 UTC

cordova-plugin-wkwebview-engine git commit: CB-9636 - Plugin should detect at runtime iOS 8 and use of file:// url and present an error

Repository: cordova-plugin-wkwebview-engine
Updated Branches:
  refs/heads/master ce2c1ebdf -> b39df66f5


CB-9636 - Plugin should detect at runtime iOS 8 and use of file:// url and present an error


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/b39df66f
Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-wkwebview-engine/tree/b39df66f
Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-wkwebview-engine/diff/b39df66f

Branch: refs/heads/master
Commit: b39df66f51ff941d242cf525eb9828d9f8b39d29
Parents: ce2c1eb
Author: Shazron Abdullah <sh...@apache.org>
Authored: Fri Sep 11 02:45:30 2015 -0700
Committer: Shazron Abdullah <sh...@apache.org>
Committed: Fri Sep 11 02:45:30 2015 -0700

----------------------------------------------------------------------
 src/ios/CDVWKWebViewEngine.m | 46 +++++++++++++++++++++++++++++----------
 1 file changed, 35 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-plugin-wkwebview-engine/blob/b39df66f/src/ios/CDVWKWebViewEngine.m
----------------------------------------------------------------------
diff --git a/src/ios/CDVWKWebViewEngine.m b/src/ios/CDVWKWebViewEngine.m
index 8c2a926..099f8f7 100644
--- a/src/ios/CDVWKWebViewEngine.m
+++ b/src/ios/CDVWKWebViewEngine.m
@@ -24,6 +24,7 @@
 #import <objc/message.h>
 
 #define CDV_BRIDGE_NAME @"cordova"
+#define CDV_WKWEBVIEW_FILE_URL_LOAD_SELECTOR @"loadFileURL:allowingReadAccessToURL:"
 
 @interface CDVWKWebViewEngine ()
 
@@ -91,17 +92,27 @@
 
 - (id)loadRequest:(NSURLRequest*)request
 {
-    SEL wk_sel = NSSelectorFromString(@"loadFileURL:allowingReadAccessToURL:");
-
-    // the URL needs to be a file reference
-    NSURL* url = request.URL;
-    
-    if ([_engineWebView respondsToSelector:wk_sel] && url.fileURL) {
-        // allow the folder containing the file reference to be read as well
-        NSURL* readAccessUrl = [request.URL URLByDeletingLastPathComponent];
-        return ((id (*)(id, SEL, id, id))objc_msgSend)(_engineWebView, wk_sel, url, readAccessUrl);
-    } else {
-        return [(WKWebView*)_engineWebView loadRequest:request];
+    if ([self canLoadRequest:request]) { // can load, differentiate between file urls and other schemes
+        if (request.URL.fileURL) {
+            SEL wk_sel = NSSelectorFromString(CDV_WKWEBVIEW_FILE_URL_LOAD_SELECTOR);
+            NSURL* readAccessUrl = [request.URL URLByDeletingLastPathComponent];
+            return ((id (*)(id, SEL, id, id))objc_msgSend)(_engineWebView, wk_sel, request.URL, readAccessUrl);
+        } else {
+            return [(WKWebView*)_engineWebView loadRequest:request];
+        }
+    } else { // can't load, print out error
+        NSString* errorHtml = [NSString stringWithFormat:
+                               @"<!doctype html>"
+                               @"<title>Error</title>"
+                               @"<div style='font-size:2em'>"
+                               @"   <p>The WebView engine '%@' is unable to load the request: %@</p>"
+                               @"   <p>Most likely the cause of the error is that the loading of file urls is not supported in iOS %@.</p>"
+                               @"</div>",
+                               NSStringFromClass([self class]),
+                               [request.URL description],
+                               [[UIDevice currentDevice] systemVersion]
+                               ];
+        return [self loadHTMLString:errorHtml baseURL:nil];
     }
 }
 
@@ -115,6 +126,19 @@
     return [(WKWebView*)_engineWebView URL];
 }
 
+- (BOOL) canLoadRequest:(NSURLRequest*)request
+{
+    // See: https://issues.apache.org/jira/browse/CB-9636
+    SEL wk_sel = NSSelectorFromString(CDV_WKWEBVIEW_FILE_URL_LOAD_SELECTOR);
+    
+    // if it's a file URL, check whether WKWebView has the selector (which is in iOS 9 and up only)
+    if (request.URL.fileURL) {
+        return [_engineWebView respondsToSelector:wk_sel];
+    } else {
+        return YES;
+    }
+}
+
 - (void)updateSettings:(NSDictionary*)settings
 {
     WKWebView* wkWebView = (WKWebView*)_engineWebView;


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org
For additional commands, e-mail: commits-help@cordova.apache.org