@@ -44,6 +44,7 @@ @interface CDVWebViewEngine ()
44
44
@property (nonatomic , weak ) id <WKScriptMessageHandler > weakScriptMessageHandler;
45
45
@property (nonatomic , strong ) CDVURLSchemeHandler * schemeHandler;
46
46
@property (nonatomic , readwrite ) NSString *CDV_ASSETS_URL;
47
+ @property (nonatomic , readwrite ) Boolean cdvIsFileScheme;
47
48
48
49
@end
49
50
@@ -143,35 +144,51 @@ - (void)pluginInitialize
143
144
CDVViewController* vc = (CDVViewController*)self.viewController ;
144
145
NSDictionary * settings = self.commandDelegate .settings ;
145
146
146
- NSString *hostname = [settings cordovaSettingForKey: @" hostname" ];
147
- if (hostname == nil ){
148
- hostname = @" localhost" ;
149
- }
150
147
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];
153
165
}
154
- vc.appScheme = scheme;
155
- self.CDV_ASSETS_URL = [NSString stringWithFormat: @" %@ ://%@ " , scheme, hostname];
156
166
157
167
self.uiDelegate = [[CDVWebViewUIDelegate alloc ] initWithTitle: [[NSBundle mainBundle ] objectForInfoDictionaryKey: @" CFBundleDisplayName" ]];
158
168
159
169
CDVWebViewWeakScriptMessageHandler *weakScriptMessageHandler = [[CDVWebViewWeakScriptMessageHandler alloc ] initWithScriptMessageHandler: self ];
160
170
161
171
WKUserContentController * userContentController = [[WKUserContentController alloc ] init ];
162
172
[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
+ }
168
181
}
169
182
170
183
WKWebViewConfiguration * configuration = [self createConfigurationFromSettings: settings];
171
184
configuration.userContentController = userContentController;
172
185
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
+
175
192
// re-create WKWebView, since we need to update configuration
176
193
WKWebView * wkWebView = [[WKWebView alloc ] initWithFrame: self .engineWebView.frame configuration: configuration];
177
194
wkWebView.UIDelegate = self.uiDelegate ;
@@ -272,7 +289,10 @@ - (BOOL)shouldReloadWebView:(NSURL*)location title:(NSString*)title
272
289
- (id )loadRequest : (NSURLRequest *)request
273
290
{
274
291
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 ) {
276
296
NSURL * startURL = [NSURL URLWithString: ((CDVViewController *)self .viewController).startPage];
277
297
NSString * startFilePath = [self .commandDelegate pathForResource: [startURL path ]];
278
298
NSURL *url = [[NSURL URLWithString: self .CDV_ASSETS_URL] URLByAppendingPathComponent: request.URL.path];
0 commit comments