html - WKWebView 从文档目录加载 html 在设备上不起作用

标签 html ios swift ios11 wkwebview

所以,我已经下载了带有 html 文件的文件夹。

folders struct

我尝试在WKWebView中打开它。

let documentsUrl = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0]
let url = documentsUrl.appendingPathComponent("\(model.sourceFileId)/index.html") 
self.webView.loadFileURL(url, allowingReadAccessTo: url)

在模拟器上运行良好

screenshot from simulator

但不在设备上

screenshot from device

看起来只是无法打开 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/

相关文章:

html - 您如何在绝对 div 中将绝对 div 居中?

ios - 解释一下 iOS 的构建和编译?

swift - 如何更改节点的枢轴

ios - 漏洞 : MPMediaItem Download Status on iOS & MPMediaPropertyPredicate using assetURL

jquery - CSS Jquery - 事件链接

java - 使用 spring controller 服务时获取 404

IOs 代码签名说明

php - 向 ios 应用程序 : Google cloud 发送推送通知

ios - 在 tableView Swift 中选择 textField 时打开 New ViewController

html - 如何更改 Select 标签中占位符选项的颜色?