I need to load a local file in a WKWebView. I\'m using the new ios9 method
- (nullable WKNavigation *)loadFileURL:(NSURL *)URL allowingReadAccessToURL:(NSURL *)re
finally! I Know where went wrong!! When you want to load a new and different file, make sure it's in the same directory as the first load file.
eg.
NSString *pathA = "file:///path/to/abc/dirA/A.html";
NSString *pathB = "file:///path/to/abc/dirB/B.html";
NSString *pathC = "file:///path/to/abc/dirC/C.html";
NSURL *url = [NSURL fileURLWithPath:pathA];
NSURL *readAccessToURL = [[url URLByDeletingLastPathComponent] URLByDeletingLastPathComponent];
// readAccessToURL == "file:///path/to/abc/"
[self.wk_webview loadFileURL:url allowingReadAccessToURL:readAccessToURL];
// then you want load pathB
url = [NSURL fileURLWithPath:pathB];
// this will work fine
[self.wk_webview loadFileURL:url allowingReadAccessToURL:readAccessToURL];
I had a very similar problem as yours, but in my case I had a reference to WKWebView objects in UIViewCell objects (I have migrated from UIWebView recently).
I was reusing WKWebView objects because of the performance reasons (standard dequeue reusable thing).
To make a long story short, you have a allowingReadAccessToURL parameter in loadFileURL:allowingReadAccessToURL: method that tells WKWebView what are allowed paths when it loads a local file. From some reason, it doesn't care about this parameter when some page with a different allowingReadAccessToURL parameter is loaded. So I recommend to use the entire Documents path space as a default parameter to this method:
NSURL *documentsURL = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] objectAtIndex:0];
[self loadFileURL:request.URL allowingReadAccessToURL:documentsURL];
Hope it helps.
I also encountered this problem. What worked for me is to simply to refresh the UIView container holding the webView:
[webView loadFileURL:url allowingReadAccessToURL:[url URLByDeletingLastPathComponent]];
[webViewContainer setNeedsDisplay];
[webViewContainer setNeedsLayout];
Hope this helps.