ios - 如何从 iOS Swift 中的本地目录加载 index.html 文件?
问题描述
我一直在从服务器下载 zip 文件并在本地目录中提取 zip 文件。
即,/var/mobile/Containers/Data/Application/8A7B8DF1-AAA4-442E-99C9-82616FC3E192/Documents/assets.zip
解压路径:/var/mobile/Containers/Data/Application/8A7B8DF1-AAA4-442E-99C9-82616FC3E192/Library/Caches/47B7913E-A0B2-429D-AD91-AA3367EFB2AE
从解压缩的文件夹中,我需要将 index.html 加载到 WkWebView。但我可以找到文件夹但无法加载到 webview。
这是下载 zip 文件并在本地目录中提取 zip 文件的代码:
let url = URL(string: v)
FileDownloader.loadFileAsync(url: url!) { (path, error) in
print("PDF File downloaded to : \(path!)")
guard let unzipPath = self.tempUnzipPath() else {
return
}
print("Unzip path:", unzipPath)
let success: Bool = SSZipArchive.unzipFile(atPath: path!,
toDestination: unzipPath,
preserveAttributes: true,
overwrite: true,
nestedZipLevel: 1,
password: nil,
error: nil,
delegate: nil,
progressHandler: nil,
completionHandler: nil)
if success != false {
print("Success unzip")
} else {
print("No success unzip")
return
}
var items: [String]
do {
items = try FileManager.default.contentsOfDirectory(atPath: unzipPath)
print("array item for unzip", items[0])
} catch {
return
}
for v in items[0] {
print("vv index.html", v)
self.buildAry.append(v)
}
print("build ary", self.buildAry)
let ss = self.buildAry[0]
print("index html", ss)
}
这是我的控制台输出:
Success unzip
array item for unzip build
vv index.html b
vv index.html u
vv index.html i
vv index.html l
vv index.html d
build ary 2020-02-07 19:40:44.480754+0530[9847:2135988] [Process] kill() returned unexpected error 1
["b", "u", "i", "l", "d"]
index html b
如何将 index.html 从解压缩的文件夹加载到 WKWebView 中。非常感谢任何帮助。
解决方案
通常要在 WKWebView 中打开本地文件,您可以调用loadFileURL(URL, allowReadAccessTo: URL)
WKWebView 实例。
在您发表评论后进行编辑:
如果您知道,您的 index.html 将始终位于“build”文件夹中,您可以执行以下操作:
let url = URL(fileURLWithPath: unzipPath, isDirectory: false).appendingPathComponent("build").appendindPathComponent("Index.html", isDirectory: false)
myWKWebView.loadFileURL(url, allowsReadAccessTo: url)
如果您需要更多帮助,请发表评论。
推荐阅读
- python - python sshtunnel not starting server given credentials
- git - 在本地看不到远程分支
- oauth-2.0 - Keycloak - 权限和政策仅在使用“评估”选项卡时有效
- python-3.x - 熊猫重组为长格式
- python - InstaPy 在 MacOs 11 上使用 Python3 失败
- authentication - 验证用户登录后,Feathers Authentication 返回哪个事件
- python - PyQt QWebEngineView 不加载任何 html
- python - 如何使用 Python Bottle 服务器将 oauth2client 身份验证 URL 从 Python 终端显示到浏览器?
- create-react-app - 如何在没有中间件或 SSR 的情况下向网站添加动态元标记
- javascript - 过滤 HTML 元素 Vanilla JS