How to restore html tags after the app passes the http acl for iOS, and escape html tags for ios
Recently, I used swift to build a small project in a company. I encountered a problem: the tags in html text obtained through APIs have been escaped,
& Lt; p class = & quot; MsoNormal & quot; align = & quot; left & quot; style = & quot; vertical-align: baseline; background: white; & quot; & gt; \ n \ t & lt; br/& gt; \ n & lt;/p & gt; \ n & lt; p class = & quot; MsoNormal & quot; align = & quot; left & quot; style = & quot; vertical-align: baseline; background: white; & quot; & gt; \ n \ t & lt; B & gt; (original title: required for new car owners! Required for car maintenance and repair & lt;/B & gt; & lt; B & gt; 20 & lt;/B & gt; & lt; B & gt; large & lt;/B & gt; & lt; B & gt; "& lt;/B & gt; & lt; B & gt; iron & lt; /B & gt; & lt; B & gt; ") & lt;/B & gt; \ n & lt;/p & gt; \ n & lt; p class = & quot; MsoNormal & quot; align = & quot; left & quot; style = & quot; vertical-align: baseline; background: white; & quot; & gt; \ n \ t2016-06-13 \ n & lt;/p & gt; \ n & lt; p class = & quot; MsoNormal & quot; align = & quot; left & quot; style = & quot; vertical-align: baseline; background: white; & quot; & gt; \ n \ t & lt; B & gt; 1. & lt;/B & gt; & lt; B & gt; the most important of the two guarantees for a new car & lt;/B & gt; \ n & lt;/p & gt; \ n & lt; p class = & quot; MsoNormal & quot; align = & quot; left & quot; style = & quot; vertical ....................................... ......
Directly call webview. loadHTMLString (content !, BaseURL: nil) will be shown in the figure below in webview
Finally, the omnipotent github found a String extension.
import Foundationimport UIKitextension String { var htmlToString:String { return try! NSAttributedString(data: dataUsingEncoding(NSUTF8StringEncoding)!, options: [NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType,NSCharacterEncodingDocumentAttribute:NSUTF8StringEncoding], documentAttributes: nil).string }}
Perfect solution
Mark here