func getHtml(){
guard let filePath = Bundle.main.path(forResource: "demo", ofType: "html") else{return}
guard let data = NSData.init(contentsOfFile: filePath) as Data? else {return}
guard let contentStr = String.init(data: data, encoding: String.Encoding.utf8) else{return}
do{
let attrStr = try NSAttributedString.init(data: contentStr.data(using: String.Encoding.unicode)!, options: [NSAttributedString.DocumentReadingOptionKey.documentType : NSAttributedString.DocumentType.html], documentAttributes: nil)
self.htmlLabel.attributedText = attrStr
}catch{
print(error.localizedDescription)
}
}
Info.plist
中添加NSAppTransportSecurity
类型Dictionary
。NSAppTransportSecurity
下添加NSAllowsArbitraryLoads
类型Boolean
,值设为YES
或者直接添加一下配置
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>