所以,我已经下载了带有 html 文件的文件夹。
我尝试在WKWebView中打开它。
let documentsUrl = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0]
let url = documentsUrl.appendingPathComponent("\(model.sourceFileId)/index.html")
self.webView.loadFileURL(url, allowingReadAccessTo: url)
在模拟器上运行良好
但不在设备上
看起来只是无法打开 css 和图像文件。但为什么?我该如何解决这个问题?
最佳答案
将文件复制到 NSTemporaryDirectory()
并打开文件 URL,它就可以工作了。我用 Objective-C 风格编写了我的代码。
NSString *basePath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"dirName"];
NSString *indexHtmlPath = [basePath stringByAppendingPathComponent:@"index.html"];
NSString *htmlString = [NSString stringWithContentsOfFile:indexHtmlPath encoding:NSUTF8StringEncoding error:nil];
NSURL *baseURL = [NSURL fileURLWithPath:basePath];
[self.wkWebView loadHTMLString:htmlString baseURL:baseURL];
关于html - WKWebView 从文档目录加载 html 在设备上不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47398977/