From d1bf60981727ae170f490eacbe26d4b31fa6dc2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julio=20C=C3=A9sar?= Date: Wed, 12 Feb 2020 00:39:30 +0100 Subject: [PATCH] Add _app_file_ handling --- .../CDVIntentAndNavigationFilter.m | 2 +- .../CDVWebViewEngine/CDVWebViewEngine.m | 26 ++++++++++++------- .../Classes/Public/CDVURLSchemeHandler.m | 10 ++++--- CordovaLib/cordova.js | 12 +++++++++ .../project/__PROJECT_NAME__/config.xml | 2 -- cordova-js-src/plugin/ios/wkwebkit.js | 12 +++++++++ 6 files changed, 48 insertions(+), 16 deletions(-) diff --git a/CordovaLib/Classes/Private/Plugins/CDVIntentAndNavigationFilter/CDVIntentAndNavigationFilter.m b/CordovaLib/Classes/Private/Plugins/CDVIntentAndNavigationFilter/CDVIntentAndNavigationFilter.m index f7f94222b..026cb233e 100644 --- a/CordovaLib/Classes/Private/Plugins/CDVIntentAndNavigationFilter/CDVIntentAndNavigationFilter.m +++ b/CordovaLib/Classes/Private/Plugins/CDVIntentAndNavigationFilter/CDVIntentAndNavigationFilter.m @@ -46,7 +46,7 @@ - (void)parser:(NSXMLParser*)parser didStartElement:(NSString*)elementName names - (void)parserDidStartDocument:(NSXMLParser*)parser { // file: url 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]; } diff --git a/CordovaLib/Classes/Private/Plugins/CDVWebViewEngine/CDVWebViewEngine.m b/CordovaLib/Classes/Private/Plugins/CDVWebViewEngine/CDVWebViewEngine.m index 2053442a7..5cdc67809 100644 --- a/CordovaLib/Classes/Private/Plugins/CDVWebViewEngine/CDVWebViewEngine.m +++ b/CordovaLib/Classes/Private/Plugins/CDVWebViewEngine/CDVWebViewEngine.m @@ -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"; @@ -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]; diff --git a/CordovaLib/Classes/Public/CDVURLSchemeHandler.m b/CordovaLib/Classes/Public/CDVURLSchemeHandler.m index 3a4b4895f..45599210f 100644 --- a/CordovaLib/Classes/Public/CDVURLSchemeHandler.m +++ b/CordovaLib/Classes/Public/CDVURLSchemeHandler.m @@ -41,10 +41,14 @@ - (void)webView:(WKWebView *)webView startURLSchemeTask:(id )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]; + } } } diff --git a/CordovaLib/cordova.js b/CordovaLib/cordova.js index 872124faf..5ce972657 100644 --- a/CordovaLib/cordova.js +++ b/CordovaLib/cordova.js @@ -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; } }; diff --git a/bin/templates/project/__PROJECT_NAME__/config.xml b/bin/templates/project/__PROJECT_NAME__/config.xml index dc7e8b8e4..3ea6e4707 100644 --- a/bin/templates/project/__PROJECT_NAME__/config.xml +++ b/bin/templates/project/__PROJECT_NAME__/config.xml @@ -34,8 +34,6 @@ - -