Skip to content

Commit 1eb1846

Browse files
authored
refactor: default to file scheme (#866)
* refactor: default to file scheme * chore: applied feedback suggestions
1 parent 7460737 commit 1eb1846

File tree

4 files changed

+46
-20
lines changed

4 files changed

+46
-20
lines changed

CordovaLib/Classes/Private/Plugins/CDVIntentAndNavigationFilter/CDVIntentAndNavigationFilter.m

+8-2
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,16 @@ - (void)parser:(NSXMLParser*)parser didStartElement:(NSString*)elementName names
4545

4646
- (void)parserDidStartDocument:(NSXMLParser*)parser
4747
{
48-
NSString* scheme = [NSString stringWithFormat:@"%@://",((CDVViewController*)self.viewController).appScheme];
4948
// file: url <allow-navigations> are added by default
5049
// navigation to the scheme used by the app is also allowed
51-
self.allowNavigations = [[NSMutableArray alloc] initWithArray:@[ @"file://", scheme ]];
50+
self.allowNavigations = [[NSMutableArray alloc] initWithArray:@[ @"file://"]];
51+
52+
// If the custom app scheme is defined, append it to the allow navigation as default
53+
NSString* scheme = ((CDVViewController*)self.viewController).appScheme;
54+
if (scheme) {
55+
[self.allowNavigations addObject: [NSString stringWithFormat:@"%@://", scheme]];
56+
}
57+
5258
// no intents are added by default
5359
self.allowIntents = [[NSMutableArray alloc] init];
5460
}

CordovaLib/Classes/Private/Plugins/CDVWebViewEngine/CDVWebViewEngine.m

+36-16
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ @interface CDVWebViewEngine ()
4444
@property (nonatomic, weak) id <WKScriptMessageHandler> weakScriptMessageHandler;
4545
@property (nonatomic, strong) CDVURLSchemeHandler * schemeHandler;
4646
@property (nonatomic, readwrite) NSString *CDV_ASSETS_URL;
47+
@property (nonatomic, readwrite) Boolean cdvIsFileScheme;
4748

4849
@end
4950

@@ -143,35 +144,51 @@ - (void)pluginInitialize
143144
CDVViewController* vc = (CDVViewController*)self.viewController;
144145
NSDictionary* settings = self.commandDelegate.settings;
145146

146-
NSString *hostname = [settings cordovaSettingForKey:@"hostname"];
147-
if(hostname == nil){
148-
hostname = @"localhost";
149-
}
150147
NSString *scheme = [settings cordovaSettingForKey:@"scheme"];
151-
if(scheme == nil || [WKWebView handlesURLScheme:scheme]){
152-
scheme = @"app";
148+
149+
// If scheme is file or nil, then default to file scheme
150+
self.cdvIsFileScheme = [scheme isEqualToString: @"file"] || scheme == nil;
151+
152+
NSString *hostname = @"";
153+
if(!self.cdvIsFileScheme) {
154+
if(scheme == nil || [WKWebView handlesURLScheme:scheme]){
155+
scheme = @"app";
156+
}
157+
vc.appScheme = scheme;
158+
159+
hostname = [settings cordovaSettingForKey:@"hostname"];
160+
if(hostname == nil){
161+
hostname = @"localhost";
162+
}
163+
164+
self.CDV_ASSETS_URL = [NSString stringWithFormat:@"%@://%@", scheme, hostname];
153165
}
154-
vc.appScheme = scheme;
155-
self.CDV_ASSETS_URL = [NSString stringWithFormat:@"%@://%@", scheme, hostname];
156166

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

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

161171
WKUserContentController* userContentController = [[WKUserContentController alloc] init];
162172
[userContentController addScriptMessageHandler:weakScriptMessageHandler name:CDV_BRIDGE_NAME];
163-
NSString * scriptCode = [NSString stringWithFormat:@"window.CDV_ASSETS_URL = '%@';", self.CDV_ASSETS_URL];
164-
WKUserScript *wkScript =
165-
[[WKUserScript alloc] initWithSource:scriptCode injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:YES];
166-
if (wkScript) {
167-
[userContentController addUserScript:wkScript];
173+
174+
if(self.CDV_ASSETS_URL) {
175+
NSString *scriptCode = [NSString stringWithFormat:@"window.CDV_ASSETS_URL = '%@';", self.CDV_ASSETS_URL];
176+
WKUserScript *wkScript = [[WKUserScript alloc] initWithSource:scriptCode injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:YES];
177+
178+
if (wkScript) {
179+
[userContentController addUserScript:wkScript];
180+
}
168181
}
169182

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

173-
self.schemeHandler = [[CDVURLSchemeHandler alloc] initWithVC:vc];
174-
[configuration setURLSchemeHandler:self.schemeHandler forURLScheme:scheme];
186+
// Do not configure the scheme handler if the scheme is default (file)
187+
if(!self.cdvIsFileScheme) {
188+
self.schemeHandler = [[CDVURLSchemeHandler alloc] initWithVC:vc];
189+
[configuration setURLSchemeHandler:self.schemeHandler forURLScheme:scheme];
190+
}
191+
175192
// re-create WKWebView, since we need to update configuration
176193
WKWebView* wkWebView = [[WKWebView alloc] initWithFrame:self.engineWebView.frame configuration:configuration];
177194
wkWebView.UIDelegate = self.uiDelegate;
@@ -272,7 +289,10 @@ - (BOOL)shouldReloadWebView:(NSURL*)location title:(NSString*)title
272289
- (id)loadRequest:(NSURLRequest*)request
273290
{
274291
if ([self canLoadRequest:request]) { // can load, differentiate between file urls and other schemes
275-
if (request.URL.fileURL) {
292+
if(request.URL.fileURL && self.cdvIsFileScheme) {
293+
NSURL* readAccessUrl = [request.URL URLByDeletingLastPathComponent];
294+
return [(WKWebView*)_engineWebView loadFileURL:request.URL allowingReadAccessToURL:readAccessUrl];
295+
} else if (request.URL.fileURL) {
276296
NSURL* startURL = [NSURL URLWithString:((CDVViewController *)self.viewController).startPage];
277297
NSString* startFilePath = [self.commandDelegate pathForResource:[startURL path]];
278298
NSURL *url = [[NSURL URLWithString:self.CDV_ASSETS_URL] URLByAppendingPathComponent:request.URL.path];

CordovaLib/cordova.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1771,7 +1771,7 @@ var WkWebKit = {
17711771
exec(null, null, 'CDVWebViewEngine', 'allowsBackForwardNavigationGestures', [allow]);
17721772
},
17731773
convertFilePath: function (path) {
1774-
if (!path) {
1774+
if (!path || !window.CDV_ASSETS_URL) {
17751775
return path;
17761776
}
17771777
if (path.startsWith('/')) {

cordova-js-src/plugin/ios/wkwebkit.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ var WkWebKit = {
2626
exec(null, null, 'CDVWebViewEngine', 'allowsBackForwardNavigationGestures', [allow]);
2727
},
2828
convertFilePath: function (path) {
29-
if (!path) {
29+
if (!path || !window.CDV_ASSETS_URL) {
3030
return path;
3131
}
3232
if (path.startsWith('/')) {

0 commit comments

Comments
 (0)