美文网首页
Swift-UIWebView&UIWKWebView,加载本地

Swift-UIWebView&UIWKWebView,加载本地

作者: 闲云悠鹤蝶恋舞 | 来源:发表于2020-05-20 16:13 被阅读0次

UIWebView

1、加载网络链接
webView?.loadRequest(URLRequest(url: URL(string: urlStr)))
2、加载本地文件
let filePath = Bundle.main.path(forResource: "doc1.doc", ofType: nil)
// 方式一:
webView?.loadRequest(URLRequest(url: URL(string: filePath)))

// 方式二:
let url = URL(fileURLWithPath: filePath)
let data = try! Data(contentsOf: url)
/**
mimeType:文件的类型
加载文本数据:
       1、application/docx
       2、application/doc
       3、application/vnd.openxmlformats-officedocument.wordprocessingml.document

加载xlsx数据:application/xlsx
加载PDF数据:application/pdf
*/
webView?.load(data, mimeType: "application/doc", textEncodingName: "utf-8", baseURL: url)

UIWKWebView

1、加载网络链接
webView?.loadRequest(URLRequest(url: URL(string: urlStr)))
2、加载本地文件
let filePath = Bundle.main.path(forResource: "doc1.doc", ofType: nil)
let url = URL(fileURLWithPath: filePath)
wkWebView?.loadFileURL(url, allowingReadAccessTo: url)

相关文章

网友评论

      本文标题:Swift-UIWebView&UIWKWebView,加载本地

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