简体   繁体   中英

iPhone App - Issue with RSS feed and loadHTMLString:baseURL:

I'm parsing an RSS feed (which comes in as an XML) and loading it into a UIWebView - and that works perfectly well. The problem is with the occasional <img> tags that are in the XML - the images they call aren't showing up (although I do get a placeholder bounding-box where they should be appearing.)

The issue is that those <img> tags unfortunately contain only partial paths, with no domain name - for example:

<img src="/news-media/images/football/6.jpg" />

To fix that I tried specifying the default domain name in the baseURL argument:

NSURL *baseURL = [NSURL fileURLWithPath:@"http://www.myDomain.com"];
[theWebView loadHTMLString:tempHtmlString baseURL:baseURL];

This does not work - the HTML is still loading fine, but the images are not showing up. This does however eliminate the "broken image" icons I was getting before on the images (the kind you usually get when a web-page is trying to load an image that's missing.) Also, I sometimes get a "received memory warning" message - but not always. Seems to happen when loading a page that contains multiple photos. But even when a page has only one photo and I do not receive the "memory" warning, the photo itself doesn't get loaded.

Any ideas what to do? Am I gonna have to manually append the base-URL to all occurrences of the <img> tag?

You are creating your base URL as a file URL (ie a URL to a local file on the device). You want the URL To reference the remote server. Try this:

NSURL *baseURL = [NSURL URLWithString:@"http://www.myDomain.com"];
[theWebView loadHTMLString:tempHtmlString baseURL:baseURL];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM