WKWebView loadFileURL works only once

后端 未结 3 1904
醉梦人生
醉梦人生 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:33

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

提交回复
热议问题