代码之家  ›  专栏  ›  技术社区  ›  Mark Struzinski

UIWebView和编码的HTML字符串?

  •  3
  • Mark Struzinski  · 技术社区  · 16 年前

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

    我从feed接收的字符串是HTML编码的。当我把它传递给webview时,它会显示解码的HTML,这意味着它会显示标签和页面内容。

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

    编辑:添加编码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: 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>
    
    2 回复  |  直到 16 年前
        1
  •  4
  •   Don McCaughey    16 年前

    快速而肮脏的方法是使用-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、正则表达式(ios4或RegexKitLite)上使用类似的方法,或者编写一个简单的解析器,但是如果您的HTML片段不是很大的话,这种方法可以很好地工作。

        2
  •  5
  •   Dave DeLong    16 年前

    类似于@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])];
    }
    

    谷歌也可以找到一些其他的选择 "cocoa decode html entity"