Skip to content
This repository has been archived by the owner on Oct 1, 2018. It is now read-only.

Commit

Permalink
Added category for CDVWKWebViewEngine to override loadRequest method:…
Browse files Browse the repository at this point in the history
… we need to set the correct readAccessURL, or WKWebView is not gonna load our updates. Work in progress.

#85
  • Loading branch information
nikDemyankov committed Apr 6, 2016
1 parent 0cdd541 commit dcf8c6f
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
4 changes: 4 additions & 0 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@
<source-file src="src/ios/Utils/HCPAssetsFolderHelper.m" target-dir="Utils/"/>
<header-file src="src/ios/Utils/HCPAssetsFolderHelper.h" target-dir="Utils/"/>

<!-- sources for WKWebViewFix folder -->
<source-file src="src/ios/WKWebViewFix/CDVWKWebViewEngine+HCPPlugin_ReadAccessURL.m" target-dir="WKWebViewFix/"/>
<header-file src="src/ios/WKWebViewFix/CDVWKWebViewEngine+HCPPlugin_ReadAccessURL.h" target-dir="WKWebViewFix/"/>

</platform>

<platform name="android">
Expand Down
17 changes: 17 additions & 0 deletions src/ios/WKWebViewFix/CDVWKWebViewEngine+HCPPlugin_ReadAccessURL.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// CDVWKWebViewEngine+HCPPlugin_ReadAccessURL.h
//
// Created by Nikolay Demyankov on 04.04.16.
//

#if WKWebViewIncluded

#import "CDVWKWebViewEngine.h"

@interface CDVWKWebViewEngine (HCPPlugin_ReadAccessURL)

- (id)loadRequest:(NSURLRequest*)request;

@end

#endif
54 changes: 54 additions & 0 deletions src/ios/WKWebViewFix/CDVWKWebViewEngine+HCPPlugin_ReadAccessURL.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//
// CDVWKWebViewEngine+HCPPlugin_ReadAccessURL.m
//
// Created by Nikolay Demyankov on 04.04.16.
//

#if WKWebViewIncluded

#import "CDVWKWebViewEngine+HCPPlugin_ReadAccessURL.h"
#import <objc/message.h>
#import "HCPFilesStructure.h"

#define CDV_WKWEBVIEW_FILE_URL_LOAD_SELECTOR @"loadFileURL:allowingReadAccessToURL:"

@implementation CDVWKWebViewEngine (HCPPlugin_ReadAccessURL)

- (id)loadRequest:(NSURLRequest*)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);

// by default we set allowingReadAccessToURL property to the plugin's root folder,
// so the WKWebView would load our updates from it.
NSURL* readAccessUrl = [HCPFilesStructure pluginRootFolder];

// if we are loading index page from the bundle - we need to go up in the folder structure, so the next load from the external storage would work
if (![request.URL.absoluteString containsString:readAccessUrl.absoluteString]) {
readAccessUrl = [[[request.URL URLByDeletingLastPathComponent] URLByDeletingLastPathComponent] URLByDeletingLastPathComponent];
}

return ((id (*)(id, SEL, id, id))objc_msgSend)(self.engineWebView, wk_sel, request.URL, readAccessUrl);
} else {
return [(WKWebView*)self.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];
}
}

@end

#endif

0 comments on commit dcf8c6f

Please sign in to comment.