WKWebView loadFileURL works only once

后端 未结 3 1906
醉梦人生
醉梦人生 2021-02-19 16:18

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

3条回答
  •  遥遥无期
    2021-02-19 16:34

    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.

提交回复
热议问题