1

hope someone can help. I appreciate there are a few questions that are similar on here, but not specific enough for me.

I'm teaching myself iOS, so decided to write a simple RSS reader app from a Wordpress site. All is working fine, but now I want to try and store the page within iOS to read when offline. Thought it would be a good way to learn how to encode and store data.

Reading the XML nodes and storing title, description, etc is all fine, that works. The issue I've got is when there's an image embedded in the feed. If I read and store the 'content:encoded' XML info, then view that in a webView that works fine, unless I'm offline when everything will work except for the image, as that's just a HTML reference.

I guess I want to

a) identify the image URL, then remove it from the content:encoded b) download and store in my array, associated with the post c) then read the image back out again.

Or something with a different approach, but similar effect.

If there was a way of adding a new XML 'featuredimage' node or similar, this would be relatively straightforward I think, but I can't see how to do that.

Would be very grateful for any ideas/pointers on this one, many thanks.

Rob
  • 35
  • 4
  • how are you rendering you RSS content? are you loading the `content:encoded` info into a `UIWebView`? – sergio May 23 '11 at 17:20
  • Hi Sergio Yes, that was the idea, so it works fine online, everything cool. When offline though it obviously just displays the text, and a blank image holder where the image is linked. – Rob May 23 '11 at 17:27

1 Answers1

1

I would suggest a slightly different approach, which I think should make things easier:

  1. identify the image:

you can use any RegEx library for iOS to parse your content:encoded info;

  1. download the image:

use ASIHTTPRequest or NSUURLRequest to download the image and store it locally;

  1. modify the content:encoded info:

modify the <img> tag so that its src attribute is set to the locally store image (instead of the external one); you can do this by using the same approach as in point 1. In other words, say you have an image like:

`<img src="http://xxxx.yyy.zzzz/image.png" />`

you can change this to:

`<img src="image.png" />`
  1. appropriately specify baseURL when loading the HTML into the UIWebView:

if you specify a baseURL when calling the loadHTMLString:baseURL: selector, any "local" uri will be resolved against the baseURL you specify. If you decide to store the image in the document directory, then you can do this:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
[_label loadHTMLString:htmlString baseURL:[NSURL fileURLWithPath:documentsDirectory]];

so that any image like:

`<img src="localImage.png" />`

will be retrieved locally.

Hope this helps.

Community
  • 1
  • 1
sergio
  • 68,819
  • 11
  • 102
  • 123
  • Thanks Sergio, that's a help in pointing me in the right direction, much appreciated. I've got the regex bit sorted, little confused on the ASIHTTP bit but I'm sure I'll get there. Thanks! – Rob May 23 '11 at 19:13