我正在做一个 iPhone 项目。我正在从 RSS 提要中获取一个 HTML 片段,并尝试使用 loadHTMLString 方法将其加载到 UIWebView 中。
我从提要中收到的字符串是 HTML 编码的。当我将它传递给 webview 时,它会显示解码后的 HTML,这意味着它会显示标签以及页面内容。
在将 HTML 传递给 UIWebView 之前,在 Objective-C 中有没有办法对 HTML 进行解码?
编辑:添加编码 HTML 的示例:
这是一段很长的文章,但这里有一个片段:
<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:&#160;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>
最佳答案
类似于@Don 的回答,但有更多的优化:
NSMutableString * html = [NSMutableString stringWithString:originalHTML];
NSDictionary * replacements = [NSDictionary dictionaryWithObjectsAndKeys:
@"<", @"<",
@">", @">",
....,
nil];
for (NSString * htmlEntity in replacements) {
[html replaceOccurrencesOfString:htmlEntity withString:[replacements objectForKey:htmlEntity] options:0 range:NSMakeRange(0, [html length])];
}
一些其他想法:将原始 html 加载到 webview 中,然后使用一些 javascript 提取呈现的文本,然后将呈现的文本作为源管道返回到 webview 中。这可能不会很有效,但它可能会起作用。
还有一些其他选项可以通过谷歌搜索找到 "cocoa decode html entity" .
关于iphone - UIWebView 和编码的 HTML 字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3321908/