美文网首页
Swift开发---UIWebView&UIWKWebView,

Swift开发---UIWebView&UIWKWebView,

作者: 闲云悠鹤蝶恋舞 | 来源:发表于2019-07-12 15:58 被阅读0次

    文件层级关系:

    image.png

    1、从文件中读取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)
    

    相关文章

      网友评论

          本文标题:Swift开发---UIWebView&UIWKWebView,

          本文链接:https://www.haomeiwen.com/subject/yvfykctx.html