This repository has been archived by the owner on Oct 1, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 467
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added category for CDVWKWebViewEngine to override loadRequest method:…
… we need to set the correct readAccessURL, or WKWebView is not gonna load our updates. Work in progress. #85
- Loading branch information
1 parent
0cdd541
commit dcf8c6f
Showing
3 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
src/ios/WKWebViewFix/CDVWKWebViewEngine+HCPPlugin_ReadAccessURL.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
54
src/ios/WKWebViewFix/CDVWKWebViewEngine+HCPPlugin_ReadAccessURL.m
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |