WKWebView loadFileURL works only once

2020-07-03 07:57发布

问题:

I need to load a local file in a WKWebView. I'm using the new ios9 method

- (nullable WKNavigation *)loadFileURL:(NSURL *)URL allowingReadAccessToURL:(NSURL *)readAccessURL

It works perfectly for the first load (navigation delegation is properly called), but if I try to load a new and different file, it does nothing.

The URL for the currentItem in the wkwebview instance is modified. But if I force a reload the delegate method didFinishNavigation is called with the previous set URL. I also tried to navigate forward but the file that was supposed to be loaded is the current one, it's not on the backForwardList.

The code I'm using to start the WKWebView and load the file:

self.wk_webview = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
self.wk_webview.scrollView.delegate = self;
self.wk_webview.navigationDelegate = self;
[self.view addSubview:self.wk_webview];

NSURL *url = [NSURL fileURLWithPath:local_path];
[self.wk_webview loadFileURL:url allowingReadAccessToURL:[url URLByDeletingLastPathComponent]];

Am I missing something? I couldn't find anything related to this.

Any help is appreciated, thanks.

回答1:

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.



回答2:

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];


回答3:

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.