UIWebView and HTML encoded string?

I am working on an iPhone project. I get an HTML snippet from an RSS feed and try to load it into a UIWebView using the loadHTMLString method.

The string that I get from the feed is HTML encoded. When I pass it to webview, it displays the decoded HTML, i.e. it shows the tags along with the content of the page.

Is there a way in Objective-C to decode HTML before passing it to a UIWebView?

EDIT: adding an example of encoded HTML:

This is a really long pass, but here's a snippet:

<li>Wireless Data: Wi-Fi (802.11b/g), Nike + iPod support built in,  Maps location-based service, Bluetooth 2.1 + EDR</li>
    <li>Audio Frequency Response: 20Hz to 20,000Hz</li>
    <li>Audio Formats Supported: <span>AAC </span>(16 to 320 Kbps), Protected <span>AAC  </span>(from iTunes Store), <span>MP3 </span>(16 to 320 Kbps), <span>MP3  VBR</span>, Audible (formats 2, 3, and 4), Apple Lossless, <span>WAV</span>,  and <span>AIFF</span></li>
    <li>Photo Support: Syncs iPod-viewable photos in <span>JPEG</span>,  BMP, <span>GIF</span>, TIFF, <span>PSD </span>(Mac only), and <span>PNG</span>  formats</li>
    <li>TV Out Support: 480p and 576p</li>
    <li>Video Support: H.264 video, up to 1.5 Mbps, 640 by 480 pixels, 30  frames per second, Low-Complexity version of the H.264 Baseline Profile  with <span>AAC</span>-LC audio up to 160 Kbps, 48kHz, stereo audio in  .m4v, .mp4, and .mov file formats; H.264 video, up to 2.5 Mbps, 640 by  480 pixels, 30 frames per second, Baseline Profile up to Level 3.0 with <span>AAC</span>-LC  audio up to 160 Kbps, 48kHz, stereo audio in .m4v, .mp4, and .mov file  formats; <span>MPEG</span>-4 video, up to 2.5 Mbps, 640 by 480 pixels,  30 frames per second, Simple Profile with <span>AAC</span>-LC audio up  to 160 Kbps, 48kHz, stereo audio in .m4v, .mp4, and .mov file formats</li>
    <li>Environmental Requirements: Operating temperature: 32° to 95° F  (0° to 35° C); Nonoperating temperature: -4° to 113° F (-20° to 45° C), Relative  humidity: 5% to 95% noncondensing</li>
+3
source share
2 answers

- -stringByReplacingOccurrencesOfString: withString: XML- :

NSString *fragment = ...; // get fragment from RSS feed
[fragment autorelease]; // if not already autoreleased to prevent a memory leak

fragment = [fragment stringByReplacingOccurrencesOfString:@"&lt;" withString:@"<"];
// repeat for &gt; &amp; &quot; etc.

: NSMutableString, (iOS 4 RegexKitLite) , , HTML .

+4

@Don, :

NSMutableString * html = [NSMutableString stringWithString:originalHTML];
NSDictionary * replacements = [NSDictionary dictionaryWithObjectsAndKeys:
                               @"<", @"&lt;",
                               @">", @"&gt;",
                               ....,
                               nil];
for (NSString * htmlEntity in replacements) {
  [html replaceOccurrencesOfString:htmlEntity withString:[replacements objectForKey:htmlEntity] options:0 range:NSMakeRange(0, [html length])];
}

: html webview, javascript , - . , , .

, googling cocoa decode html entity.

+5

Source: https://habr.com/ru/post/1756265/


All Articles