Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WKWebView loadFileURL works only once

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.

like image 710
Douglas Schmidt Avatar asked Nov 07 '16 19:11

Douglas Schmidt


2 Answers

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.

like image 55
Michal Cichon Avatar answered Sep 22 '22 19:09

Michal Cichon


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];
like image 40
Mapleiny Avatar answered Sep 20 '22 19:09

Mapleiny