Skip to content

Commit

Permalink
Add _app_file_ handling
Browse files Browse the repository at this point in the history
  • Loading branch information
jcesarmobile committed Feb 11, 2020
1 parent a35a37b commit d1bf609
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ - (void)parser:(NSXMLParser*)parser didStartElement:(NSString*)elementName names
- (void)parserDidStartDocument:(NSXMLParser*)parser
{
// file: url <allow-navigations> are added by default
self.allowNavigations = [[NSMutableArray alloc] initWithArray:@[ @"file://" ]];
self.allowNavigations = [[NSMutableArray alloc] initWithArray:@[ @"file://", @"app://" ]];
// no intents are added by default
self.allowIntents = [[NSMutableArray alloc] init];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,6 @@ - (void)pluginInitialize
// viewController would be available now. we attempt to set all possible delegates to it, by default
NSDictionary* settings = self.commandDelegate.settings;

self.uiDelegate = [[CDVWebViewUIDelegate alloc] initWithTitle:[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDisplayName"]];

CDVWebViewWeakScriptMessageHandler *weakScriptMessageHandler = [[CDVWebViewWeakScriptMessageHandler alloc] initWithScriptMessageHandler:self];

WKUserContentController* userContentController = [[WKUserContentController alloc] init];
[userContentController addScriptMessageHandler:weakScriptMessageHandler name:CDV_BRIDGE_NAME];

WKWebViewConfiguration* configuration = [self createConfigurationFromSettings:settings];
configuration.userContentController = userContentController;

NSString *hostname = [settings cordovaSettingForKey:@"hostname"];
if(hostname == nil){
hostname = @"localhost";
Expand All @@ -108,6 +98,22 @@ - (void)pluginInitialize
}
self.CDV_ASSETS_URL = [NSString stringWithFormat:@"%@://%@", scheme, hostname];

self.uiDelegate = [[CDVWebViewUIDelegate alloc] initWithTitle:[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDisplayName"]];

CDVWebViewWeakScriptMessageHandler *weakScriptMessageHandler = [[CDVWebViewWeakScriptMessageHandler alloc] initWithScriptMessageHandler:self];

WKUserContentController* userContentController = [[WKUserContentController alloc] init];
[userContentController addScriptMessageHandler:weakScriptMessageHandler name:CDV_BRIDGE_NAME];
NSString * scriptCode = [NSString stringWithFormat:@"window.CDV_ASSETS_URL = '%@';", self.CDV_ASSETS_URL];
WKUserScript *wkScript =
[[WKUserScript alloc] initWithSource:scriptCode injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:YES];
if (wkScript) {
[userContentController addUserScript:wkScript];
}

WKWebViewConfiguration* configuration = [self createConfigurationFromSettings:settings];
configuration.userContentController = userContentController;

CDVViewController* vc = (CDVViewController*)self.viewController;
self.schemeHandler = [[CDVURLSchemeHandler alloc] initWithVC:vc andScheme:scheme];
[configuration setURLSchemeHandler:self.schemeHandler forURLScheme:scheme];
Expand Down
10 changes: 7 additions & 3 deletions CordovaLib/Classes/Public/CDVURLSchemeHandler.m
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,14 @@ - (void)webView:(WKWebView *)webView startURLSchemeTask:(id <WKURLSchemeTask>)ur
NSString * scheme = url.scheme;

if ([scheme isEqualToString:self.scheme]) {
if ([stringToLoad isEqualToString:@""] || [url.pathExtension isEqualToString:@""]) {
startPath = [startPath stringByAppendingPathComponent:self.viewController.startPage];
if ([stringToLoad hasPrefix:@"/_app_file_"]) {
startPath = [stringToLoad stringByReplacingOccurrencesOfString:@"/_app_file_" withString:@""];
} else {
startPath = [startPath stringByAppendingPathComponent:stringToLoad];
if ([stringToLoad isEqualToString:@""] || [url.pathExtension isEqualToString:@""]) {
startPath = [startPath stringByAppendingPathComponent:self.viewController.startPage];
} else {
startPath = [startPath stringByAppendingPathComponent:stringToLoad];
}
}
}

Expand Down
12 changes: 12 additions & 0 deletions CordovaLib/cordova.js
Original file line number Diff line number Diff line change
Expand Up @@ -1765,6 +1765,18 @@ var exec = require('cordova/exec');
var WkWebKit = {
allowsBackForwardNavigationGestures: function (allow) {
exec(null, null, 'CDVWebViewEngine', 'allowsBackForwardNavigationGestures', [allow]);
},
convertFilePath: function (path) {
if (!path) {
return path;
}
if (path.startsWith('/')) {
return window.CDV_ASSETS_URL + '/_app_file_' + path;
}
if (path.startsWith('file://')) {
return window.CDV_ASSETS_URL + path.replace('file://', '/_app_file_');
}
return path;
}
};

Expand Down
2 changes: 0 additions & 2 deletions bin/templates/project/__PROJECT_NAME__/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@

<!-- Whitelist docs: https://github.com/apache/cordova-plugin-whitelist -->
<access origin="*" />
<!-- Allow navigation to app scheme -->
<allow-navigation href="app://*"/>
<!-- Grant certain URLs the ability to launch external applications. This
behaviour is set to match that of Cordova versions before 3.6.0, and
should be reviewed before launching an application in production. It
Expand Down
12 changes: 12 additions & 0 deletions cordova-js-src/plugin/ios/wkwebkit.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@ var exec = require('cordova/exec');
var WkWebKit = {
allowsBackForwardNavigationGestures: function (allow) {
exec(null, null, 'CDVWebViewEngine', 'allowsBackForwardNavigationGestures', [allow]);
},
convertFilePath: function (path) {
if (!path) {
return path;
}
if (path.startsWith('/')) {
return window.CDV_ASSETS_URL + '/_app_file_' + path;
}
if (path.startsWith('file://')) {
return window.CDV_ASSETS_URL + path.replace('file://', '/_app_file_');
}
return path;
}
};

Expand Down

0 comments on commit d1bf609

Please sign in to comment.