Swift开发---UIWebView&UIWKWebView,
2019-07-12 本文已影响0人
闲云悠鹤蝶恋舞
文件层级关系:
image.png1、从文件中读取HTML内容
if let filePath = Bundle.main.path(forResource: "/JJA/demo", ofType: "html") {
let contents = try String(contentsOfFile: filePath)
}
得到contents:
<!DOCTYPE html><html> <head><meta charset="utf-8"><title></title><link rel="stylesheet" href="css/mycss1.css" /></head><body><span class="demo1">这是我们的测试</span></body></html>
2、加载本地HTML文件
let url = Bundle.main.url(forResource: "/JJA/demo", withExtension: "html")
webView.loadRequest(URLRequest(url: url!))
3、直接加载html内容
let content = "<!DOCTYPE html><html> <head><meta charset=\"utf-8\"><title></title><link rel=\"stylesheet\" href=\"css/mycss1.css\" /></head><body><span class=\"demo1\">这是我们的测试</span></body></html>"
let basePath = Bundle.main.url(forResource: "/JJA", withExtension: nil)
// 注:baseURL如果设置为nil的话,html中的css将失效
webView.loadHTMLString(content, baseURL: basePath)