首页 > 解决方案 > iOS 12.2 WKWebview:加载 https 或 http url 后无法加载本地 html 页面

问题描述

在应用程序启动时,我正在 WKWebView 中加载https://www.google.com。应用程序有一个按钮,单击该按钮应用程序正在从文档目录加载本地 html 页面。我使用以下代码加载 html 页面。

let destPath = NSSearchPathForDirectoriesInDomains(.documentDirectory,
                                                           .userDomainMask,
                                                           true)[0]
        let fileName = "Demo.html"
        let fullDestPath = URL(fileURLWithPath: destPath)
            .appendingPathComponent(fileName)
            self.webView! .loadFileURL(fullDestPath, allowingReadAccessTo: fullDestPath)

该代码一直工作到 iOS 12.1.4 但是在 iOS 12.2 中它没有加载 html 页面并抛出错误

ProvisionalPageProxy::didFailProvisionalLoadForFrame: pageID = 1, frameID = 1, navigationID = 2

标签: iosswiftwkwebview

解决方案


检查文件Demo.html是否存在于文档目录中。如果不存在,则不会加载 html 页面。

尝试从加载Bundle

if let path = Bundle.main.path(forResource: "Demo", ofType: "html") {
   let htmlURL = URL(fileURLWithPath: path)
       webView.loadFileURL(htmlURL, allowingReadAccessTo: htmlURL)
 } else {
    print("File not found")
 }

推荐阅读