Skip to content

Commit 8329648

Browse files
LindsayLindsay-Needs-Sleep
authored andcommitted
(ios) Fix #349 Allow navigation to cdvfile urls when using wkwebview.
WKWebView does not allow unknown url schemes so we have to intercept the request, stop it, and send out a new request for the equivalent file:// url. Note: When the lowest cordova supported iOS version is 11.0 this can work around can potentially be removed. WKWebview for iOS 11+ supports "WKURLSchemeHandler" and "setURLSchemeHandler" which should allow actual support for the cdvfile scheme.
1 parent d67745a commit 8329648

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

plugin.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ to config.xml in order for the application to find previously stored files.
171171
<param name="ios-package" value="CDVFile" />
172172
<param name="onload" value="true" />
173173
</feature>
174+
<allow-navigation href="cdvfile:*" />
174175
</config-file>
175176
<header-file src="src/ios/CDVFile.h" />
176177
<source-file src="src/ios/CDVFile.m" />

src/ios/CDVFile.m

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,53 @@ @implementation CDVFile
194194

195195
@synthesize rootDocsPath, appDocsPath, appLibraryPath, appTempPath, userHasAllowed, fileSystems=fileSystems_;
196196

197+
// If the url matches the cdvfile:// format,
198+
// will return the equivalent file:// url format if possible
199+
// else, return nil
200+
- (NSString*)nativeUrlFromCdvfileUrl:(NSURL*) uri
201+
{
202+
// If the url matches the cdvfile format (cdvfile://<cdvfile path>)
203+
if ([[uri scheme] isEqualToString:kCDVFilesystemURLPrefix]) {
204+
NSString *cdvfileUrl = [uri absoluteString];
205+
// Try to get the native Url from the cdvfile url
206+
if (cdvfileUrl != nil) {
207+
CDVFilesystemURL* inputURI = [self fileSystemURLforArg:cdvfileUrl];
208+
// If the cdvfile url is valid
209+
if (inputURI != nil && inputURI.fileSystemName != nil) {
210+
// Get the filesystem
211+
NSObject<CDVFileSystem> *fs = [self filesystemForURL:inputURI];
212+
if (fs != nil) {
213+
// Get the file Entry
214+
NSDictionary *entry = [fs makeEntryForLocalURL:inputURI];
215+
if (entry != nil) {
216+
// Return the file URL
217+
return [entry objectForKey:@"nativeURL"];
218+
}
219+
}
220+
}
221+
}
222+
}
223+
return nil;
224+
}
225+
// Checks if we are trying to load a url that we should override.
226+
// If we should override we send out a new load request and
227+
// return NO to stop loading the original request.
228+
// Note: When the lowest cordova supported iOS version is 11.0 this can
229+
// potentially be removed. WKWebview for iOS 11+ supports WKURLSchemeHandler
230+
// and setURLSchemeHandler which should allow support for the cdvfile scheme.
231+
- (BOOL)shouldOverrideLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
232+
{
233+
// check if the url matches the httpsCdvfileUrl and gets the equivalent file:// url
234+
NSString *fileUrl = [self nativeUrlFromCdvfileUrl:[request URL]];
235+
if (fileUrl != nil) {
236+
// Create a new request for the file:// url instead
237+
NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString:fileUrl]];
238+
[[self webViewEngine] loadRequest:req];
239+
return NO;
240+
}
241+
return YES;
242+
}
243+
197244
- (void)registerFilesystem:(NSObject<CDVFileSystem> *)fs {
198245
__weak CDVFile* weakSelf = self;
199246
SEL sel = NSSelectorFromString(@"urlTransformer");

0 commit comments

Comments
 (0)