iPhone RSS Parser

I’ve made a simple parser class for reading RSS feeds and a simple demo application for the iPhone that implements the iPhoneRSS class.

The following example shows the usage for the RSS class. Basically, the class creates an NSArray of news items, and each object in the array is an NSDictionary. The dictionary contains the element name as the key.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/* load feed from URL */
iPhoneRSS *rss = [[iPhoneRSS alloc] initWithURL:[NSURL URLWithString:@"http://www.reddit.com/.rss"]];
   
/* array contains all posts in feed */
items = [rss items];
   
NSLog(@"Number of items: %d", [items count]);
   
/* loop through all posts in the feed */
NSEnumerator *enumerator = [items objectEnumerator];
NSDictionary *dict;
   
while(dict = [enumerator nextObject]) {
    NSLog(@"---------------------------------------------------------");
    NSLog(@"Title: %@", [dict objectForKey:@"title"]);
    NSLog(@"Description: %@", [dict objectForKey:@"description"]);
   
    /* to get comments, use the rss reader to read the comment RSS for this post, contained in wfw:commentRss */
    NSString *commentURL = [dict objectForKey:@"wfw:commentRss"];
       
    if(commentURL != nil) {
        iPhoneRSS *commentRss = [[iPhoneRSS alloc] initWithURL:[NSURL URLWithString:[dict objectForKey:@"wfw:commentRss"]]];
        NSLog(@"Number of comments: %d", [[commentRss items] count]);
    }      
    NSLog(@"---------------------------------------------------------");
}

You can download the sample project and the RSS class files here: http://www.tech9computers.com/iPhoneRSS.zip

Posted Thursday, July 17th, 2008 under Technology.

Tags: , ,

Similar Posts

  • Michael

    I am just entering the field of iPhone application development, and am not very knowledgeable in the subject. I was wondering how to go about using this parser for a different site’s feed. I made the obvious change to the URL used, but when I ran the app in the iPhone simulator, it did not yield any results. I tried using feeds from Digg, Reddit, and XKCD to try it out.

    I was wondering if I missed a crucial change or step, or if these sites were incompatible (although the site coding seemed almost identical).
    These were the URL’s I used:
    feed://digg.com/rss/index.xml
    feed://www.reddit.com/.rss
    feed://xkcd.com/rss.xml

    If you could help me out, I would be very thankful!

  • chrismoos

    Hey Michael,

    I’ve looked at digg’s RSS and they don’t use wfw:commentRss, which is what most RSS feeds use for the comments. I’ve updated the code on the blog entry to reflect checking to make sure that the commentRss exists. Also, make sure you change feed:// to http://(i'm sure you did that though).

    Chris

  • Ethan

    Hey, can you check out my problem here?

    http://forums.macrumors.com/showthread.php?t=541051

    It has to do with strings being pulled back correctly from RSS feeds using the class you made.

    Get back to me ASAP if you can.

    Thanks!

    Ethan
    ethanwa@comcast.net