【问题标题】:WKWebView does not load local html filesWKWebView 不加载本地 html 文件
【发布时间】:2018-12-26 08:36:29
【问题描述】:

我有一个WKWebView,在给定URLURLRequest 时工作得非常好,但是当我尝试将本地捆绑html 文件加载到其中时,它只是不显示任何内容。

我是这样尝试的:

let webView = WKWebView()

if let bundleURL = Bundle.main.url(forResource: "index", withExtension: "html", subdirectory: "mySubDirectory/v1") {
    webView.loadFileURL(bundleURL, allowingReadAccessTo: bundleURL.deletingLastPathComponent())
}

我测试了bundleURL 实际上返回了正确的路径,并尝试在运行时将其复制+粘贴到我的浏览器中,效果很好: file:///Users/myuser/Library/Developer/Xcode/DerivedData/FireApp-gugmufgdkejtfdhglixebzhokhfe/Build/Products/Debug-iphonesimulator/SteppingIntoFireFramwork.framework/mySubDirectory/v1/index.html

可能是什么问题?

【问题讨论】:

    标签: ios swift wkwebview


    【解决方案1】:

    如果 URL 有效,那么您的 webview 似乎无法加载它。

    我曾经遇到过这个问题...检查您是否只为允许的 URL 方案启用了“http”和“https”

    【讨论】:

    • OMFG,实际上有人启用了httphttps 只是为了加载webview!非常感谢!否则我不会找到它:O
    • 那个选项在哪里?我没找到。
    • @Gerard 我也找不到。它曾经在Info.pList下。
    【解决方案2】:
    import UIKit
    import WebKit
    
    class ViewController: UIViewController, WKNavigationDelegate {
        override func viewDidLoad() {
            super.viewDidLoad()
            let webView = WKWebView()
            let htmlPath = Bundle.main.path(forResource: "index", ofType: "html")
            let htmlUrl = URL(fileURLWithPath: htmlPath!, isDirectory: false)
            webView.loadFileURL(htmlUrl, allowingReadAccessTo: htmlUrl)
            webView.navigationDelegate = self
            view = webView
        }
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
        }
    }
    

    Full solution here

    【讨论】:

      【解决方案3】:

      您的网络视图只是一个本地临时变量。它的大小为零并且不在界面中,因此您永远看不到任何事情发生。

      【讨论】:

        【解决方案4】:

        尝试从您的示例中删除 DeleteLastPathComponent()。 您也可以尝试使用 load(request) 而不是 loadFileURL()。

        if let bundleURL = Bundle.main.url(forResource: "index", withExtension: "html", subdirectory: "mySubDirectory/v1") {
            let request = URLRequest(url: bundleURL)
            webView.load(request)
        }    
        

        【讨论】:

          猜你喜欢
          • 2017-05-24
          • 2018-01-29
          • 1970-01-01
          • 2017-03-22
          • 1970-01-01
          • 2014-09-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多