当使用WKWebView预览ppt、Excel、word、txt等文件时,关于txt文档乱码情况,不用担心,对链接对应的文档进行UTF8编码、GBK和GB1830编码就OK了,
SWIFT 5版本:
func configData() {guard let url = URL.init(string: urlStr) else { return }if urlStr.hasSuffix(".txt") {// 先进行NSUTF8StringEncoding编码var body = try? String(contentsOf: url, encoding: String.Encoding.utf8)if (body == nil) {// 如果没有编码成功再尝试GBK和GB18030编码let encode = CFStringConvertEncodingToNSStringEncoding(UInt32(CFStringEncodings.GB_18030_2000.rawValue))let encoding = String.Encoding.init(rawValue: encode)body = try? String(contentsOf: url, encoding: encoding)}if let body = body {webView.loadHTMLString(body, baseURL: nil)} else {webView.load(URLRequest.init(url: url))}} else {webView.load(URLRequest.init(url: url))}}