從沙盒加載網(wǎng)頁(yè)和資源文件,第一次加載沒問題添祸,第二次加載的時(shí)候會(huì)加載失敗報(bào)錯(cuò)
2019-08-09 14:20:23.284917+0800 MediaPlay[1041:1359144] ++++++++++file:///var/mobile/Containers/Data/Application/38666562-0E95-4EDF-A4C1-FF37C5465E29/Documents/unzip/52/index.html?currentTime=0
2019-08-09 14:20:23.308443+0800 MediaPlay[1041:1359144] 加載
失敗 -- Error Domain=kCFErrorDomainCFNetwork Code=1 "(null)"
UserInfo{_NSURLErrorFailingURLSessionTaskErrorKey=LocalData
Task <6B857B83-6193-45B9-BFE9-2E2E1C53A292>.<16>,
_WKRecoveryAttempterErrorKey=<WKReloadFrameErrorRecoveryAttempter: 0x2836af1c0>}
經(jīng)過(guò)搜索驗(yàn)證發(fā)現(xiàn)艳馒,猜測(cè)這是WKWebView的一個(gè)內(nèi)部限制,會(huì)鎖定第一次使用資源文件目錄(allowingReadAccessTo)
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:
解決方法:將資源文件目錄的范圍擴(kuò)大匆浙,能找到資源文件安寺,就可以正常加載了
NSURL *documentsURL = [[[NSFileManager defaultManager]
URLsForDirectory:NSDocumentDirectory
inDomains:NSUserDomainMask] objectAtIndex:0];
[self loadFileURL:request.URL allowingReadAccessToURL:documentsURL];
或者
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];