Skip to content
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

(ios) Added a NSURLRequestReloadIgnoringLocalCacheData policy when lo… #1471

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ - (nullable instancetype)initWithFrame:(CGRect)frame configuration:(nullable WKW
if (NSClassFromString(@"WKWebView") == nil) {
return nil;
}

self.configuration = configuration;
self.engineWebView = configuration ? [[WKWebView alloc] initWithFrame:frame configuration:configuration] : [[WKWebView alloc] initWithFrame:frame];
}
Expand All @@ -85,7 +85,7 @@ - (WKWebViewConfiguration*) createConfigurationFromSettings:(NSDictionary*)setti
configuration = [[WKWebViewConfiguration alloc] init];
configuration.processPool = [[CDVWebViewProcessPoolFactory sharedFactory] sharedProcessPool];
}

if (settings == nil) {
return configuration;
}
Expand Down Expand Up @@ -155,7 +155,7 @@ - (WKWebViewConfiguration*) createConfigurationFromSettings:(NSDictionary*)setti
} else if ([contentMode isEqual: @"desktop"]) {
configuration.defaultWebpagePreferences.preferredContentMode = WKContentModeDesktop;
}

}

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 140000
Expand Down Expand Up @@ -333,26 +333,35 @@ - (BOOL)shouldReloadWebView:(NSURL*)location title:(NSString*)title

- (id)loadRequest:(NSURLRequest*)request
{
if ([self canLoadRequest:request]) { // can load, differentiate between file urls and other schemes
if(request.URL.fileURL && self.cdvIsFileScheme) {
NSURL* readAccessUrl = [request.URL URLByDeletingLastPathComponent];
return [(WKWebView*)_engineWebView loadFileURL:request.URL allowingReadAccessToURL:readAccessUrl];
} else if (request.URL.fileURL) {
// Set the cache policy to not use the cache, so that we don't have diskcache issues when updating cordova versions, removing plugins, etc.
NSURL *url = request.URL;
NSURLRequest *noCacheRequest = [NSURLRequest requestWithURL:url
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:request.timeoutInterval];

// Log the cache policy
NSLog(@"Cache policy set to: %lu", (unsigned long)noCacheRequest.cachePolicy);

if ([self canLoadRequest:noCacheRequest]) { // can load, differentiate between file urls and other schemes
if(noCacheRequest.URL.fileURL && self.cdvIsFileScheme) {
NSURL* readAccessUrl = [noCacheRequest.URL URLByDeletingLastPathComponent];
return [(WKWebView*)_engineWebView loadFileURL:noCacheRequest.URL allowingReadAccessToURL:readAccessUrl];
} else if (noCacheRequest.URL.fileURL) {
NSURL* startURL = [NSURL URLWithString:((CDVViewController *)self.viewController).startPage];
NSString* startFilePath = [self.commandDelegate pathForResource:[startURL path]];
NSURL *url = [[NSURL URLWithString:self.CDV_ASSETS_URL] URLByAppendingPathComponent:request.URL.path];
if ([request.URL.path isEqualToString:startFilePath]) {
NSURL *url = [[NSURL URLWithString:self.CDV_ASSETS_URL] URLByAppendingPathComponent:noCacheRequest.URL.path];
if ([noCacheRequest.URL.path isEqualToString:startFilePath]) {
url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/%@", self.CDV_ASSETS_URL, startURL]];
}
if(request.URL.query) {
url = [NSURL URLWithString:[@"?" stringByAppendingString:request.URL.query] relativeToURL:url];
if(noCacheRequest.URL.query) {
url = [NSURL URLWithString:[@"?" stringByAppendingString:noCacheRequest.URL.query] relativeToURL:url];
}
if(request.URL.fragment) {
url = [NSURL URLWithString:[@"#" stringByAppendingString:request.URL.fragment] relativeToURL:url];
if(noCacheRequest.URL.fragment) {
url = [NSURL URLWithString:[@"#" stringByAppendingString:noCacheRequest.URL.fragment] relativeToURL:url];
}
request = [NSURLRequest requestWithURL:url];
noCacheRequest = [NSURLRequest requestWithURL:url];
}
return [(WKWebView*)_engineWebView loadRequest:request];
return [(WKWebView*)_engineWebView loadRequest:noCacheRequest];
} else { // can't load, print out error
NSString* errorHtml = [NSString stringWithFormat:
@"<!doctype html>"
Expand All @@ -362,7 +371,7 @@ - (id)loadRequest:(NSURLRequest*)request
@" <p>Most likely the cause of the error is that the loading of file urls is not supported in iOS %@.</p>"
@"</div>",
NSStringFromClass([self class]),
[request.URL description],
[noCacheRequest.URL description],
[[UIDevice currentDevice] systemVersion]
];
return [self loadHTMLString:errorHtml baseURL:nil];
Expand Down