【问题标题】:How to load index.html file from local directory in iOS Swift?如何在 iOS Swift 中从本地目录加载 index.html 文件?
【发布时间】:2020-05-23 16:48:14
【问题描述】:

我一直在从服务器下载 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。非常感谢任何帮助..

【问题讨论】:

    标签: ios swift webview zip wkwebview


    【解决方案1】:

    通常要在 WKWebView 中打开本地文件,您可以在 WKWebView 实例上调用 loadFileURL(URL, allowReadAccessTo: URL)。

    在您发表评论后进行编辑:
    如果您知道,您的 index.html 将始终位于“build”文件夹中,您可以这样做:

    let url = URL(fileURLWithPath: unzipPath, isDirectory: false).appendingPathComponent("build").appendindPathComponent("Index.html", isDirectory: false)   
    myWKWebView.loadFileURL(url, allowsReadAccessTo: url)
    

    如果您需要更多帮助,请发表评论。

    【讨论】:

    • 解压后,在 build 文件夹中找到名为 'build' 的文件夹名称,有 index.html 文件。如何从构建文件夹加载 index.html 文件。
    • 它显示这样的主线程日志主线程检查器:在后台线程上调用的 UI API:-[WKUserContentController addScriptMessageHandler:name:] PID:10494,TID:2491477,线程名称:(无),队列名称:NSOperationQueue 0x137eeaa10(QOS:未指定),QoS:0
    • DispatchQueue.main.async { print("md5 storage form url", self.md5FormURL) self.webView.loadHTMLString(contents as String, baseURL: self.md5FormURL) }
    • 使用DispatchQueue.main.async 是对的。您需要更多帮助还是解决了您的问题?
    • 是的,从解压后的文件夹中加载 html 文件的时间超过 5 秒
    猜你喜欢
    • 1970-01-01
    • 2021-05-18
    • 2021-05-08
    • 1970-01-01
    • 1970-01-01
    • 2018-03-02
    • 2015-10-20
    • 1970-01-01
    • 2018-10-19
    相关资源
    最近更新 更多