Skip to content

Commit

Permalink
(ios) Added a NSURLRequestReloadIgnoringLocalCacheData policy when lo…
Browse files Browse the repository at this point in the history
…ading requests
  • Loading branch information
Brett Blashko committed Aug 20, 2024
1 parent d8ebfb3 commit a53af63
Showing 1 changed file with 26 additions and 17 deletions.
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

0 comments on commit a53af63

Please sign in to comment.