【问题标题】:UIWebView and an Encoded HTML String?UIWebView 和编码的 HTML 字符串?
【发布时间】:2010-07-23 20:00:40
【问题描述】:

我正在做一个 iPhone 项目。我从 RSS 提要中获取 HTML 片段,并尝试使用 loadHTMLString 方法将其加载到 UIWebView 中。

我从提要收到的字符串是 HTML 编码的。当我将它传递给 webview 时,它会显示解码后的 HTML,这意味着它会显示标签以及页面的内容。

Objective-C 中有没有一种方法可以在将 HTML 传递给 UIWebView 之前对其进行解码?

编辑:添加编码 HTML 的示例:

这是一个很长的段落,但这是一个sn-p:

<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>

【问题讨论】:

  • -loadHTMLString:baseURL: 方法应该可以正常工作。请发布您的 HTML 字符串是什么以及如何将其放入 webview。

标签: iphone objective-c


【解决方案1】:

与@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 提取呈现的文本,然后将呈现的文本作为源管道返回到 webview 中。这可能不会很有效,但它可能会起作用。

还有一些其他选项可以通过谷歌搜索"cocoa decode html entity"找到。

【讨论】:

    【解决方案2】:

    快速而肮脏的方法是使用 -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 片段不是很大,这种方法也可以正常工作。

    【讨论】:

    • 这成功了。我认为在 NSData 或 NSURL 类中可能有一个方便的方法可以为你处理这个问题,但我想不是......谢谢!
    • 有很多 HTML 实体,我们如何为所有实体编写代码?
    猜你喜欢
    • 1970-01-01
    • 2013-03-12
    • 1970-01-01
    • 2013-06-07
    • 1970-01-01
    • 2010-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多