Skip to content

fix(ios) issue #349 - navigate to cdvfile with wkwebview #358

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ to config.xml in order for the application to find previously stored files.
<param name="ios-package" value="CDVFile" />
<param name="onload" value="true" />
</feature>
<allow-navigation href="cdvfile:*" />
</config-file>
<header-file src="src/ios/CDVFile.h" />
<source-file src="src/ios/CDVFile.m" />
Expand Down
47 changes: 47 additions & 0 deletions src/ios/CDVFile.m
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,53 @@ @implementation CDVFile

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

// If the url matches the cdvfile:// format,
// will return the equivalent file:// url format if possible
// else, return nil
- (NSString*)nativeUrlFromCdvfileUrl:(NSURL*) uri
{
// If the url matches the cdvfile format (cdvfile://<cdvfile path>)
if ([[uri scheme] isEqualToString:kCDVFilesystemURLPrefix]) {
NSString *cdvfileUrl = [uri absoluteString];
// Try to get the native Url from the cdvfile url
if (cdvfileUrl != nil) {
CDVFilesystemURL* inputURI = [self fileSystemURLforArg:cdvfileUrl];
// If the cdvfile url is valid
if (inputURI != nil && inputURI.fileSystemName != nil) {
// Get the filesystem
NSObject<CDVFileSystem> *fs = [self filesystemForURL:inputURI];
if (fs != nil) {
// Get the file Entry
NSDictionary *entry = [fs makeEntryForLocalURL:inputURI];
if (entry != nil) {
// Return the file URL
return [entry objectForKey:@"nativeURL"];
}
}
}
}
}
return nil;
}
// Checks if we are trying to load a url that we should override.
// If we should override we send out a new load request and
// return NO to stop loading the original request.
// Note: When the lowest cordova supported iOS version is 11.0 this can
// potentially be removed. WKWebview for iOS 11+ supports WKURLSchemeHandler
// and setURLSchemeHandler which should allow support for the cdvfile scheme.
- (BOOL)shouldOverrideLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
{
// check if the url matches the httpsCdvfileUrl and gets the equivalent file:// url
NSString *fileUrl = [self nativeUrlFromCdvfileUrl:[request URL]];
if (fileUrl != nil) {
// Create a new request for the file:// url instead
NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString:fileUrl]];
[[self webViewEngine] loadRequest:req];
return NO;
}
return YES;
}

- (void)registerFilesystem:(NSObject<CDVFileSystem> *)fs {
__weak CDVFile* weakSelf = self;
SEL sel = NSSelectorFromString(@"urlTransformer");
Expand Down
26 changes: 26 additions & 0 deletions tests/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -4160,4 +4160,30 @@ exports.defineManualTests = function (contentEl, createActionButton) {
createActionButton('show-contact-image', function () {
resolveFsContactImage();
}, 'contactButton');

// This tests that we are allowed to navigate to cdvfile:// urls and
// that plugins are allowed to 'exec' on cdvfile urls.
// For exec details, see Issue 329 https://github.com/apache/cordova-plugin-file/issues/329\
div = document.createElement('h2');
div.appendChild(document.createTextNode('cdvfile:// url'));
div.setAttribute('align', 'center');
contentEl.appendChild(div);
div = document.createElement('div');
div.setAttribute('align', 'center');
div.innerHTML = 'clicking this button should re-load the tests page.'
+ '<br>You should be able to run all the tests successfully after the page re-load.'
+ '<br>NOTE: This will not work if you are not on file:// url to start.'
+ '<br>eg. You are using an http server to hot re-load client-side files.';
contentEl.appendChild(div);
div = document.createElement('div');
div.setAttribute('id', 'cdvfilePageTests');
div.setAttribute('align', 'center');
contentEl.appendChild(div);
createActionButton('Navigate to cdvfile:// url', function () {
// Get current page's cdvfile Url
window.resolveLocalFileSystemURL(window.location.href, function (entry) {
var cdvfileUrl = entry.toInternalURL();
window.location.href = cdvfileUrl;
});
}, 'cdvfilePageTests');
};