使用webView加载网页标签转码的问题

433 阅读1分钟
    // 将字符值引用转位普通字符串
    var htmlToString: String {
        
        do {
            let encodedData = self.data(using: String.Encoding.utf8)!
            let attributedOptions:[NSAttributedString.DocumentReadingOptionKey : Any] = [
                NSAttributedString.DocumentReadingOptionKey(rawValue: NSAttributedString.DocumentAttributeKey.documentType.rawValue) :NSAttributedString.DocumentType.html,
                NSAttributedString.DocumentReadingOptionKey(rawValue: NSAttributedString.DocumentAttributeKey.characterEncoding.rawValue): String.Encoding.utf8.rawValue
            ]
            let attributedString = try NSAttributedString(data: encodedData,
                                                          options: attributedOptions, documentAttributes: nil)
            
            return attributedString.string
        } catch {
            fatalError("Unhandled error: \(error)")
        }
        return ""
    }
    ```