首页 文章

WKWebView加载本地页面

提问于
浏览
-2

如何将本地HTML页面加载到WKWebview中?

我将allowsReadAccessTo设置为包含所有文件的目录 . 但仍然会出错 .

if let testTempUrl = Bundle.main.url(forResource: "index", withExtension: "html", subdirectory:"sub") {
                if FileManager.default.fileExists(atPath: testTempUrl.path) {

                    webViewWK.loadFileURL(testTempUrl, allowingReadAccessTo: testTempUrl.deletingLastPathComponent())


                } else {
                    askSiriWhy()
                }
            }

如果我使用UIWebView而不是WKWebView,HTML页面加载没有问题 .

1 回答

  • 1

    我不确定你为什么使用“allowsReadAccessTo” - 你的文件在哪里?

    if FileManager.default.fileExists(atPath: testTempUrl.path) {
    
                        webViewWK.loadFileURL(testTempUrl, allowingReadAccessTo: testTempUrl.deletingLastPathComponent())
    
     }
    

    为什么不呢:

    if FileManager.default.fileExists(atPath: testTempUrl.path) {
        webViewWK.loadFileURL(testTempUrl)
    }
    

相关问题