How to Develop a Simple RSS reader for the iPhone

By Partho, Gaea News Network
Thursday, May 7, 2009

RSS reader on iphone

With RSS reader on your iPhone you can have every bit of information in a jiffy. After having done with the top 10 tutorials to develop iPhone app, we are up with a step by step guide to build an iPhone app.

Well you might get a NetNewsWire RSS reader for iPhone absolutely free. But why not develop an app by yourself and join the prestigious league of iPhone app builders. Before you begin with developing RSS reader for iPhone app, you must be familiar with the iPhone SDK.

To build an simple RSS reader for iPhone follow the steps below

Step 1

iphoneapp

To begin with open the Xcode. Now choose the File menu click the New Project item.

Step 2

Click the Application under iPhone OS in the list at left

Step 3

On the right side you Navigation Based Application. Now click the Choose button. When you are prompted to pick a name and location. Type in the TAB RSS reader and save it on any location.iphone-xcode

Step 4

After saving you get the Xcode project window with the 3 standard panes. Drag the horizonatal divider on the right to the top. You will need this editor area.

Step 5

Look for the Build and Go button in the toolbar. Click on it, or move to the Build menu and click Build and Go (Run). This will open the Simulator application a simple and launch a simple iPhone app that displays a blank navigation bar and blank table. Great! you have laid the foundation of your first iPhone app.

Step 6

Now it’s time to shape your app.
Look into the project template and seek the MainWindow.xib from the list on the left of project window. Double-click on it. Now you can look for the Navigation Controller window with a basic interface mocked up. Double-click on the navigation bar without a title. Type The Apple Blog and press return. Save and quit Interface Builder.

Step 7

Go to the list and click on the RootViewController.h and see the code on the right. Make it look like this

@interface RootViewController : UITableViewController {
IBOutlet UITableView * newsTable;
UIActivityIndicatorView * activityIndicator;
CGSize cellSize;
NSXMLParser * rssParser;
NSMutableArray * stories;

// a temporary item; added to the “stories” array one at a time, and cleared for the next one
NSMutableDictionary * item;

// it parses through the document, from top to bottom…
// we collect and cache each sub-element value, and then save each item to our array.
// we use these to track each current item, until it’s ready to be added to the “stories” array
NSString * currentElement;
NSMutableString * currentTitle, * currentDate, * currentSummary, * currentLink;
}
@end

It is the declaration file.

Step 8

iphone-xcode2

Open the RootViewController.m. Change the value of

-
(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section to return [stories count];

The method will determine the table -how many rows you need for each item in the array or for each item in the feed.

Modify the method below that you have just changed with

(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = @”MyIdentifier”;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];

if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
}

// Set up the cell
int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];
[cell setText:[[stories objectAtIndex: storyIndex] objectForKey: @”title”]];

return cell;
}

Even now you won’t be able to run the program. Now you add the ability to download the feed and use it.

Step 8

Bring change to the viewDidAppear: method to look like this

(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];

if ([stories count] == 0) {
NSString * path = @”https://feeds.feedburner.com/TheAppleBlog”;
[self parseXMLFileAtURL:path];
}

cellSize = CGSizeMake([newsTable bounds].size.width, 60);
}

Step 9

Now you have to define which feed to download. It calls a method that you need to paste, as below

(void)parseXMLFileAtURL:(NSString *)URL {
stories = [[NSMutableArray alloc] init];

//you must then convert the path to a proper NSURL or it won’t work
NSURL *xmlURL = [NSURL URLWithString:URL];

// here, for some reason you have to use NSClassFromString when trying to alloc NSXMLParser, otherwise you will get an object not found error
// this may be necessary only for the toolchain
rssParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL];

// Set self as the delegate of the parser so that it will receive the parser delegate methods callbacks.
[rssParser setDelegate:self];

// Depending on the XML document you’re parsing, you may want to enable these features of NSXMLParser.
[rssParser setShouldProcessNamespaces:NO];
[rssParser setShouldReportNamespacePrefixes:NO];
[rssParser setShouldResolveExternalEntities:NO];

[rssParser parse];
}

This method creates an empty array, creates a parser and starts downloading the feed.

Step 10

Paste the following now

(void)parserDidStartDocument:(NSXMLParser *)parser {
NSLog(@"found file and started parsing");
}

- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
NSString * errorString = [NSString stringWithFormat:@"Unable to download story feed from web site (Error code %i )", [parseError code]];
NSLog(@”error parsing XML: %@”, errorString);

UIAlertView * errorAlert = [[UIAlertView alloc] initWithTitle:@”Error loading content” message:errorString delegate:self cancelButtonTitle:@”OK” otherButtonTitles:nil];
[errorAlert show];
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
//NSLog(@”found this element: %@”, elementName);
currentElement = [elementName copy];

if ([elementName isEqualToString:@"item"]) {
// clear out our story item caches…
item = [[NSMutableDictionary alloc] init];
currentTitle = [[NSMutableString alloc] init];
currentDate = [[NSMutableString alloc] init];
currentSummary = [[NSMutableString alloc] init];
currentLink = [[NSMutableString alloc] init];
}
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{

//NSLog(@”ended element: %@”, elementName);
if ([elementName isEqualToString:@"item"]) {
// save values to an item, then store that item into the array…
[item setObject:currentTitle forKey:@"title"];
[item setObject:currentLink forKey:@"link"];
[item setObject:currentSummary forKey:@"summary"];
[item setObject:currentDate forKey:@"date"];

[stories addObject:[item copy]];
NSLog(@”adding story: %@”, currentTitle);
}
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
//NSLog(@”found characters: %@”, string);
// save the characters for the current item…
if ([currentElement isEqualToString:@"title"]) {
[currentTitle appendString:string];
} else if ([currentElement isEqualToString:@"link"]) {
[currentLink appendString:string];
} else if ([currentElement isEqualToString:@"description"]) {
[currentSummary appendString:string];
} else if ([currentElement isEqualToString:@"pubDate"]) {
[currentDate appendString:string];
}
}

- (void)parserDidEndDocument:(NSXMLParser *)parser {

[activityIndicator stopAnimating];
[activityIndicator removeFromSuperview];

NSLog(@”all done!”);
NSLog(@”stories array has %d items”, [stories count]);
[newsTable reloadData];

Step 11

iphone-xcode3

Now, its time to alleviate the memory leaks. To accomplish that put this in

(void)dealloc {
[currentElement release];
[rssParser release];
[stories release];
[item release];
[currentTitle release];
[currentDate release];
[currentSummary release];
[currentLink release];

[super dealloc];
}

Step 12

Open the RootViewController.xib. Press the Ctrl key and drag from the RootViewController  cube icon over to the table view, and release. Look at the list of three items that appear and click on the newsTable item. Save and quit the Interface Builder.

Step 13

To see the results of your work click on the Build and Go. It’s not complete yet. To open up the things in Safari you need to modify the method as below

(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Navigation logic

int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];

NSString * storyLink = [[stories objectAtIndex: storyIndex] objectForKey: @”link”];

// clean up the link - get rid of spaces, returns, and tabs…
storyLink = [storyLink stringByReplacingOccurrencesOfString:@" " withString:@""];
storyLink = [storyLink stringByReplacingOccurrencesOfString:@"\n" withString:@""];
storyLink = [storyLink stringByReplacingOccurrencesOfString:@"    " withString:@""];

NSLog(@”link: %@”, storyLink);
// open in Safari
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:storyLink]];
}

Whee! you have created an iPhone app.

Source: the AppleBlog

YOUR VIEW POINT
NAME : (REQUIRED)
MAIL : (REQUIRED)
will not be displayed
WEBSITE : (OPTIONAL)
YOUR
COMMENT :