Reading RSS Feeds on iPhone/iPad with ASIHttpRequest and cocoa-rss

May 28, 2010 | Mike Mayo

Mike Mayo is the mobile applications developer for Rackspace in San Francisco.  He built the iPhone, iPad, and Android apps for Slicehost and the Rackspace Cloud.  He currently maintains those apps, contributes to open source software projects, and helps other developers with the Rackspace Cloud APIs.  Objective-C is his current favorite programming language, but he also loves Ruby, Python, and occasionally Java.  When he is not busy with Rackspace activities, Mike is an avid guitarist, smoked meat practitioner and barbecue aficionado.

The web is no longer only about serving HTML and CSS to your visitors.  Now your visitors are coming from all kinds of devices: desktop computers, netbooks, tablets, cell phones, etc.  Because of that, your web site or app can no longer be an HTML-only thing.  It needs to serve content and offer functionality in a way that is easy for developers to use programatically.

One of the older browser alternatives now is the RSS feed reader.  People get their news from many sources and aggregate them into a single place.  Sites have RSS feeds, which are simple XML representations of the content.  The XML is well-formed, standard(ish) across all sites, and easy to parse in any modern programming environment.

So, in this post, I’m going to show you a very easy way to parse an RSS feed to display on an iPhone OS device (iPhone, iPod Touch, and iPad) using two free open source libraries: ASIHttpRequest and cocoa-rss.

ASIHttpRequest

ASIHttpRequest is a simple wrapper around CFNetwork.  Network programming in Objective-C is annoying, and ASIHttpRequest makes it a pleasure, and almost as easy as AJAX coding in jquery or Prototype.

For our app, we’ll use the Building43 RSS feed, and here’s how simple the ASIHttpRequest code is for that (you can see all the code for this in the Example folder of cocoa-rss):

NSURL *url = [NSURL URLWithString:@"http://building43.com/feed"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
[request startAsynchronous];

Four lines of code to make a request.  First, declare our URL, then declare our request.  After that, we set the delegate and execute the request.

Using a delegate is what makes this so easy.  In this case, we use self, which is a UIViewController subclass.  After that, we simply add two methods that ASIHttpRequest will call after the request is finished.

- (void)requestFinished:(ASIHTTPRequest *)request {
// this is called if the request is successful
}

- (void)requestFailed:(ASIHTTPRequest *)request {
// this is called if the request fails
}

It’s that easy.

cocoa-rss

cocoa-rss is a simple library I created to parse RSS and Atom feeds.  I built this as part of the Rackspace Cloud Pro iPad app, and found that it was easy to reuse, so I released it on github as its own project.  For parsing the Building43 feed, I simply use the library in the requestFinished ASIHttpRequest delegate method:

- (void)requestFinished:(ASIHTTPRequest *)request {
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:[request responseData]];
RSSParser *rssParser = [[RSSParser alloc] init];
xmlParser.delegate = rssParser;
if ([xmlParser parse]) {
[self.feedItems release];
self.feedItems = rssParser.feedItems;
}

[rssParser release];
[xmlParser release];
[self.tableView reloadData];
}

I simply declare an RSSParser object and fire it off.  It figures out whether or not it’s reading an RSS feed or an Atom feed.  After parsing, I save the data, clean up memory, and then reload a table view that lists the content.

I used RSS in this post to keep things simple, but you can use ASIHttpRequest to easily deal with any REST web service out there.  I can’t recommend it enough.

While you develop, I have two tools to recommend for inspecting web services.  The first is HTTP Client for Mac (http://ditchnet.org/httpclient/).  It’s generally easier to use than the unix curl command.  There’s a similar app for iPad called Curly, which I wrote.  It will also let you build HTTP requests and email curl equivalents to yourself.  It’s for sale in the App Store and the source is available at http://github.com/greenisus/curly

Important Links:
ASIHttpRequest – http://allseeing-i.com/ASIHTTPRequest/
cocoa-rss – http://github.com/greenisus/cocoa-rss

If you want to talk about this topic more, you can reach me via email at mike@overhrd.com, on the phone at 901-299-9306, or on Twitter as @greenisus.

This post was tagged:
 

Comments on this entry are closed.

{ 1 trackback }