简体   繁体   English

Swift3:如何将HTML文件从文档目录加载到WKWebview

[英]Swift3 : How to load html file to WKWebview from document directory

Can anyone explain how to load local html file to WKWebView? 谁能解释如何将本地html文件加载到WKWebView?

I tried with the below code works well in simulator but not on device 我尝试使用以下代码在模拟器中运行良好,但在设备上不起作用

 let path = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)
 let documentDirectoryPath: String = path[0]
 let fileManager = FileManager()

 let destinationURLForFile = URL(fileURLWithPath: documentDirectoryPath + "/" + "\(NameF.fileName)/\(NameF.Languages)/page_\(i + 1 ).html")
 print("D2\(destinationURLForFile)")
 let request = NSURLRequest(url: destinationURLForFile)
 webView!.load(request as URLRequest)

Any suggestions would be helpful! 任何的意见都将会有帮助!

Thank you for your response guys and for who tried my question.Finally found the answer by opening the local file and read its content and then send that string into a WKWebview 感谢您的答复以及尝试我的问题的人。最后,通过打开本地文件并读取其内容找到了答案,然后将该字符串发送到WKWebview

And the piece of code is 这段代码是

 do{
            let fileName =  try String(contentsOf: destinationURLForFile)
            print("fileName is equal to\(fileName)")
            webView.loadHTMLString(fileName, baseURL: destinationURLForFile)


        }catch{

        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM