开发者

UIWebView and an Encoded HTML String?

I'm working on an iPhone project. I am getting an HTML fragment from an RSS feed and am attempting to load it into a UIWebView using the loadHTMLString method.

The string I am receiving from the feed is HTML encoded. When I pass it to the webview, it displays the decoded HTML, meaning it shows the tags along with the content of the page.

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

EDIT: Adding an example of the encoded HTML:

It's a really long passage, 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 t开发者_如何学JAVAhe 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>


Similar to @Don's answer, but with a few more optimizations:

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])];
}

Some other ideas: Load the original html into the webview, and then use some javascript to extract the rendered text, then pipe the rendered text back into the webview as the source. This probably won't be very efficient, but it might work.

There are also a few other options that can be found by googling "cocoa decode html entity".


The quick and dirty way is to use -stringByReplacingOccurrencesOfString:withString: to replace the encoded XML entities with their equivalent characters:

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.

There are other options: use similar methods on a NSMutableString, regular expressions (iOS 4 or RegexKitLite) or write a simple parser, but this way will work just fine if your HTML fragments aren't huge.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜