GigaOM Network: GigaOM | WebWorkerDaily | NewTeeVee | Earth2Tech | OStatic | jkOnTheRun | TheAppleBlog | NewTeeVee Live | Jobs | About | Advertise | Contact

iPhone SDK Tutorial: Build a Simple RSS reader for the iPhone

Written on August 04, 2008 by Jason Terhorst and 77 people have commented

With this I’m assuming you have a bit of familiarity with the iPhone SDK - you can download it for free from Apple’s site, and follow along here. We’re going to build an RSS feed reader for a simple feed (from The Apple Blog, no less).

Let’s get started

  1. Open Xcode and choose the “File” menu, in which you’ll click the “New Project…” item.
  2. Click “Application” under “iPhone OS” in the list at left.
  3. On the right, choose “Navigation-Based Application”. Then click the “Choose…” button. You’ll be prompted to pick a name and location. Type in the name “TAB RSS reader”.
  4. Save it wherever you wish.

The Xcode project window will appear, with the standard 3 panes - I recommend pulling the horizontal divider on the right side all the way to the top, since you’ll need that editor area and all the real estate you can give it.

Do you see a “Build and Go” button in the toolbar? Click it, or go to the “Build” menu, and click “Build and Go (Run)” there. It should open the Simulator application and launch a simple iPhone app that displays a blank navigation bar and blank table. Whee! Your first iPhone app. Now let’s sculpt it into something.

The project template that Apple provides has a lot of things already set up to get us started. On the list at the left of the project window, find “MainWindow.xib”, and double-click it. This is the basic framing of your application’s UI. Be careful not to mess around here too much. You just need to do one thing: you should see a “Navigation Controller” window with a basic interface mocked up - double-click on the navigation bar (which has no title in it), and type “The Apple Blog”. Press return. Save and quit Interface Builder. 

Click once on “RootViewController.h” in the list, 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

That’s the declaration file, where we’re telling the compiler what to expect when it runs through the controller logic. Here’s where the real work happens… Open “RootViewController.m“.

You’ll see that there’s more of the basic code to make that table view display - this controller is the table’s “delegate” - the table looks here to find out what it’s supposed to see/display/do in various situations, and sends calls for methods when the user performs various actions.

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

In our declarations, we told it we would have an array (NSMutableArray - a modifiable collection of objects), which we called “stories”. The [brackets] around that bit signify that it’s a message - we’re asking the stories array what its current count is - that is, how many items it has. Our RSS reader will grab as many items as it can (one for each story in the RSS feed), and place them in that array, so this method will tell the table This is how many rows we need: one for each item in the array, or for each item in the feed. Before, it was set to 0, so you’re giving it more information on our array.

Next up, modify the method below the one you just changed, like so:


- (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;
}

As you can see, we used the “setText:” method to tell the cell what the contents will be. Each row in the table is basically a cell, and its properties are set in this method.

There are 4 methods highlighted in green about 3/4 of the way down - you can delete those if you wish, since we won’t be using them. They have to do with adding/deleting items.

If you were to run the program again now, it still wouldn’t do anything: we haven’t added the ability to download the feed and use it yet, so let’s do that now.

Edit the “viewDidAppear:” method to look like this:


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

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

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

This is where we tell the parser which feed to download. It calls a method, which you’ll want to paste in now:


- (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 is a method we’ve added that creates the empty array for stories, creates a parser, and starts downloading the feed. As the parser works, this controller we’re working in will receive the various delegate methods, which you can paste in 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];
}

 

Unfortunately, the NSXMLParser is the only simple XML-parsing tool available on iPhone (some of my favorites from the Mac are missing). So, this means we have to crunch through the file in order from top to bottom. We have a series of strings that we assign values to, and then collect them into story items, which are saved one by one. Once it hits the closing “item” tag, it saves that story, clears out the fields, and starts on the next item until we reach the end of the file. Not my favorite approach, but it works.

Finishing up

We need to shut off any potential memory leaks (it’s a good habit to get into, when you don’t have garbage collection - who needs that anyway?). Drop in this change:


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

	[super dealloc];
}

Next, open up “RootViewController.xib“, and hold down the “control” key on your keyboard, while dragging from the “RootViewController” cube icon over to the table view, and release. You should see a list of three items appear, so click on the “newsTable” item. Save and quit Interface Builder.

Build and Go

If you click “Build and Go”, you’ll see the results we have so far. If you were to run this on an actual iPhone and not in simulator, the results would be different slightly: the hardware is slower, and if you’re on EDGE, the RSS feed will take a very long time to download. But, hey, it works! One thing that doesn’t work yet: when you tap on an item in the table, nothing happens. This is default behavior, but let’s make the stories open in Safari - that’s an easy thing to do. Just change this method:


- (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]];
}

Now, click “Build and Go” again, to see that it works.

Done for Now


You can download the finished project file here if you wish.

Check back here later, and we’ll cover some steps on how to clean up the UI, and add some navigation.

Share This



Comments RSSComments

  1. #1 larry says:

    You may have just violated the NDA! The Apple police will be circling your home about now!

  2. #2 Jon says:

    The NDA is ludicrous, and for beginners like myself, this sort of tutorial is an absolute pile of GOLD!! THANK YOU, THANK YOU, THANK YOU!!!! =D

  3. #3 Joseph says:

    I have been critical of TAB as of late, but kudos to you for telling Apple where it can shove its NDA. Well done!

  4. #4 Stepan Mazurov says:

    Great tutorial, thanks a lot for putting this out, saved on my harddrive ready to be reposted in case apple watchdogs get to you.

  5. #5 Per Ejeklint says:

    Very nice! Thank’s for your tutorial, much appreciated.

  6. #6 Martin says:

    Why would apple not want resources published that help people build applications for their phone?

  7. #7 Sajid Khan says:

    In completely unrelated news, Apple has released iPhone 2.0.1!

  8. #8 Rishi says:

    Dude…SDK violation. Prepare for Apple’s legal team! But, great job! This is amazing. Thanks for ignoring Apple’s idiotic NDA and writing what people have been looking for since the SDK came out.

  9. #9 Jan Erik Paulsen says:

    Damn.. now I might have to check out the price for one of those iPhone thingies. Interesting stuff.

  10. #10 jtbandes says:

    Nice tutorial. Only one problem — NSXML* isn’t available on the iPhone… it’s in the Simulator SDK, though, which is why you can compile it. I suggest using TouchXML; the usage (for non-extravagant uses of NSXML*) is exactly the same, unless your XML document has namespaces, in which case it’s a simple adjustment.

  11. #11 lg says:

    looks to me like there are a lot of memory leaks in that code…

  12. #12 Zed says:

    Can someone clarify jtbandes claim that NSXML is not supported on iPhone? I Googled and found a lot of stuff saying it wasn’t supported but would compile in the simulator as jtbandes claims. I did find one forum post that says it is supported on the phone itself. Would you care to comment Jason?

    To Ig: He specifically cleans up memory so what are you talking about? Care to cite an example and improve this article for everyone? Is your point to be helpful or just rude?

    Thanks very much for taking the time to write this article. I really appreciate it.

  13. #13 Michael N. says:

    Great tutorial, thanks Jason.

    The only issue I had was that the links opened to the wrong website. The line:

    storyLink = [storyLink stringByReplacingOccurrencesOfString:@"n" withString:@""];

    in the last part is removing the n in http://www.feedburner.com so all links open to some spam site named http://feedburer.com/. I commented out this line and it worked fine.

  14. #14 Jason Terhorst says:

    @jtbandes: NSXMLParser is one of the few that *is* available on the iPhone OS, on the device. I’m not sure what you’re talking about, but I have a couple shipping apps that use NSXMLParser for key functions.

    @Michael N.: I made a bit of a typo. Where you see @”n”, it should actually be @”\n” (a newline character)… sorry about that. :) It’s not needed, but in some of my clients’ RSS feeds, there are odd spaces and line breaks, so that helps keep the iPhone from choking on it, so I’ve made it a bit of habit.

  15. #15 Michael N. says:

    @Jason: I thought as much, so I added the “\” to my code right after I left that comment.

    Looking forward to your update to this tutorial, any chance you’ll add the ability to view the stories in the app rather than opening Safari?

  16. #16 Ned Ludd says:

    How is this a tutorial? Just copy and paste some pre-written code into your IDE? Doesn’t really help anyone understand how to actually code for the iPhone.

  17. #17 Geuis says:

    When I first plugged my iPhone in, Xcode asked a couple of times if I wanted to use it as a development device. Now that I have the sample app running in the simulator, when I connect the phone I no longer get the prompt to let Xcode use it. I went through Preferences but I don’t see any options to make Xcode redetect the phone.

  18. #18 larry says:

    Great all these dudes will follow this and we will get another 400 useless apps that crash our phones!

  19. #19 Vincent says:

    I have never programmed in Objective-C but I must say the syntax looks absolutely horrendous. I have a strong CS background including a lot of C++, but I cannot understand half of the constructs at first sight. The use of () [] is uberly inconsistent. And why does it requires a dash before every method declaration? Why not use the same syntax as C/C++ as C# did? I can’t believe Mac developers are forced to write application in that awful language.

    They must feel very 1980 when they see neat programs written in a modern language.

  20. #20 Dan Greenblatt says:

    Thanks for this excellent tutorial. I have a tiny bit of Objective-C programming background, but haven’t used it in about a year. I’ve been wanting to pick up iPhone programming but have been mortified to climb back up that steep Objective-C learning curve. This article was a great (re-)introduction, and I found your small descriptions of each chunk of code very useful in reorienting myself to the disorienting world of Objective-C :)

    It might be a good thing to add in the beginning that this article assumes some familiarity with Objective-C (in addition to the iPhone SDK, as you already point out). To some of the commenters points, this is *not* an Objective-C tutorial nor does it purport to be. IMHO, for a great introduction to (and in-depth coverage of) ObjC, check out Aaron Hillegass’ “Cocoa Programming for Mac OS X” — it’s not specificially about iPhone but it will get you on the right track ….

  21. #21 MobileMii says:

    Nice, but I’m stuck at the part when you have to ctrl drop the box to table view.
    I’m a Super-newbie, so can somebody help me???

  22. #22 MobileMii says:

    Ohw. I see no edit button so:
    If I do the ctrl drop I see only tableview and view.

  23. #23 Michael N says:

    @Geuis It does’t really matter that you can’t set it to use your iPhone, you need to register with Apple and pay the $100 to get a key (or what ever it’s called) that will allow you to both run your app on your iPhone and submit it to the App store. With out that key all you can do is test on the simulator.

    If anyone knows differently then I would love to hear from them.

  24. #24 Geuis says:

    @Michael N Yeah I was coming to that realization after finally getting the app running last night. I’m still confused though, because when I first opened Xcoode and had my phone plugged in, it asked me if I wanted to use the phone as a development device. I clicked no the first couple times, but now I can’t get that prompt to appear again. Even if I can’t deploy to the phone right now, I don’t want to be in a situation later on where I can’t deploy even after I pay the blood toll to Apple.

  25. #25 Jason Terhorst says:

    Yes, you’d need a developer account to create provisions for your device.

    But we don’t discuss that here. Definitely NDA, with good reason. ;)

    @Dan: Yes, that book is a definite recommendation for getting started. This tutorial is meant for those with experience in Objective-C, or those clever enough to work their way through it, with the assumption that it will pique your interest enough that you’ll go out and find materials to get up to speed and figure other things out. :) Personally, I learn best by taking apart existing working code and understanding it that way.

  26. #26 James Frost says:

    @Vincent (#19): Are you essentially saying that Objective-C’s syntax is horrible because it’s *shock* different to things you’ve used before? Perhaps, before slagging off a language for being ‘awful’, you should learn why it’s different, so you can make an informed opinion. I was unsure of Ruby’s syntax when I first saw it (Python being my previous favourite) but I looked into it, took the time to learn some things, and saw that many of its conventions made a lot of sense.

    Objective-C is a fantastic language - I suggest you start with http://cocoadevcentral.com/d/learn_objectivec/ to see if it’s something you could get along with.

  27. #27 James Frost says:

    @Vincent: Also, to response to your questions:

    Square brackets are used in message sending (calling methods) - [foo baz]; sends the message baz to foo (calls the method baz on the object foo. [foo fireEmployee:@"Bob" withReason:@"None needed"]; would call the fireEmployee:withReason method on foo.

    - is used infront of method declarations to denote an instance method. + denotes a class method.

  28. #28 oregon_tony says:

    I’ll chip in a few bucks for your legal defense if needed. Maybe you’ll get 15 minutes of fame from it. More examples like this are needed in order to help developers not familiar with Xcode overcome the initial learning curve of building an application.

    It seems like the current SDK restrictions will also prevent 3rd party books on iPhone development from being published. If you look on Amazon under “iPhone Development” there are several titles in the works, but at the moment it doesn’t seem like they could ever be legally published.

  29. #29 Jon says:

    @Geuis - To provision your iPhone for development, connect your iPhone to your machine and fire up Xcode. From the Window menu, select “Organizer”. You should see your iPhone in the list of devices. Click on the iPhone in that list, and I suspect you’ll be able to figure out the rest from there… =)

    Cheers,
    Jon

  30. #30 James says:

    Very nice tutorial. I’m very much looking forward to seeing how to implement navigation into the next view (instead of launching safari.)

    Thanks again!

  31. #31 MikeT says:

    @Vincent

    I have never programmed in Objective-C but I must say the syntax looks absolutely horrendous. I have a strong CS background including a lot of C++, but I cannot understand half of the constructs at first sight. The use of () [] is uberly inconsistent. And why does it requires a dash before every method declaration? Why not use the same syntax as C/C++ as C# did? I can’t believe Mac developers are forced to write application in that awful language.

    Man, I had to wipe a tear from my eye when I read that. You’re a funny guy, suggesting that C++ is actually readable to the uninitiated when you get beyond syntax that is just warmed over C with objects.

    You might find the language to be interesting if you would, *drum roll*, read the language spec. It’s actually a very powerful, useful language that ranks right up there with Java and C# in terms of what it can do with minimal syntax.

    And dude, you might want to avoid saying “oh that’s so 1980s” when you admit that much of your own background is C/C++. One is 1970s tech, the other is 1980s. Objective-C 2.0, on the other hand, is fairly modern stuff as C-based languages go. You would know that if you ever glanced over the language spec.

  32. #32 Ernest Oporto says:

    Ran fine for me. The only issue is that it gives a warning box:

    ‘RootViewController’ may not respond to ‘-parseXMLFileAtURL:’
    (Messages without a matching method signature will be assumed to return ‘id’ and accept ‘…’ as arguments.)

    Other than that, a nice intro. Off to http://cocoadevcentral.com/d/learn_objectivec/ to learn how to make one of 400 new crappy apps. Thanks for the encouragement, Mr Elitist.

  33. #33 Ahmed Essam says:

    Peace be upon you
    The tutorial is very nice and it goes very smoooooooth with me, Thanks a lot Jason, I hope that you give more samples,

    Thanks for your time.

    BR
    Ahmed Essam

  34. #34 Patty says:

    So how exactly is Apple going to monitor EVERY line, in EVERY blog, every chat, and every webpage, and every email… to make sure 10000s of people that DID sign a NDA are “not speaking”, and 10000s other people, *ARE* allowed to talk freely.

    Just use a handle, instead of your real, full name.

  35. #35 benwulf says:

    @ James Frost
    I have on my side a background in many different languages including C/C++/java/C# but also exotics like lisp/prolog and scripting like javascript/ruby/perl/python.

    I picked up ObjectiveC a couple months ago when starting to program for the iphone first on jailbroken 1.X phones then on the SDK.

    Here is what is my (hopefully objective opinion).

    1) ObjectiveC is very nice to program and definitely nicer than C++ (which I do for a living) and less error prone, once you get past the syntax.
    2) the ObjectiveC syntax is in fact horendous, not because it is different but because it is inconsistent with the C based roots. And with itself… Here are example from my perspective of inconsistencies:
    Usage of (), sometimes (method definitions) they are used to indicate where types are specified for parameters or return values, sometimes (properties) they are used for optional parameters to the keyword @property and not for the type.
    Usage of named parameters in messages. Why is the first parameter special, why does it reuse the message name as its name, it would be more consistent to have the first parameter named the same way as the others.
    Usage of [] for message passing. This is inconsistent with other usages of [] in the same program (indexing), finding another way to specify message passing vs data member access would have been nice
    Usage of . with properties. Objective C objects are pointers, using . after a pointer is inconsistent with the rest.

    This being said. I think that ObjectiveC is easy to pick up for someone who has a base in any C based language (easier than C++, harder than java or C#) and the syntax quirks are just that, quirks which add maybe a day at most 2 in the learning curve, but if you can’t read the message passing syntax after reading a one page primer on ObjectiveC you probably don’t have any place programming.

  36. #36 dominic says:

    Simple clean real tutorial.

  37. #37 Vargonian says:

    I really appreciate the effort on your part for this tutorial, but this tutorial had a great deal of “Type Now, Learn Later” elements to it. Even worse is “Paste Now, Learn Later”. I didn’t know what I was doing half the time; I just followed the instructions. I did very much appreciate the parts where you explained what a View Controller was doing in plain English, and that sort of thing.

    I must say that it perplexes me that Objective-C can’t follow a standard function signature convention. Coming from C#, it’s difficult for me to tolerate even C and C++, but Objective-C is even worse, and it doesn’t help that the default curly brace format places opening braces at the end of a line of code *cringe*. I thought we’d moved beyond that.

    Anyway, I hear a lot of people defend Objective-C, but I’d be very curious to see the same defense from an Objective-C developer who tries C# for a few months and then returns.

  38. #38 Nate says:

    @Vargonian: Having had some previous Obj C experience this code made perfect sense to me. As an excersize I am expanding on this, and it is my gateway into iPhone development.

    @Jason Terhorst: Do you mind if I use this code as a spring board (pun intended) for my first iPhone app?

  39. #39 Louis St-Amour says:

    Not bad, as these things go. I found by typing in what you’d written, I gained a better feel for actually programming in Objective-C. (The first step for me to learn any language is to copy, letter by letter, then figure out what I just wrote, then finally branch out with my own variation.)

    My only qualm with the code itself is that the functions for the XML parser are stored within the Controller, rather than doing something more MVC or Object-Oriented, such as creating a feedModel object or something, and then working with it… Then again, it’s likely if I keep exploring Apple’s sample code, I can figure out how to apply the reference material and better programming practices like MVC to iPhone development…

    I just wish Apple would listen to *******NDA.com and realize its hurting them much more in the prevalence of unstable, poor applications as well as the creation of a real developer community. Hell, just give an exemption to a few books, or offer them as encrypted files to developers only, or even set up a sanctioned forum for swapping iPhone dev tips - I mean, you can’t even talk about it on Cocoa-dev mailing lists now …

  40. #40 Ronnie says:

    In case anyone is wondering, the UIActivityIndicatorView was not being created or used at all. To use it you have to instantiate it and add it to the view.

  41. #41 tu says:

    Very good tutorial but i can’t open link with Safari , Anyone can help me

    - (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]];
    }

  42. #42 Clayton Yocom says:

    @MikeT I Completely Agree, I may be Intruding a Little bit but, here I Go, If @Vincent Wanted Completely Readable Code base For iPhone, Try Making a Ruby on Rails Webapp (Although it Cannot Scale, Just Kidding! @Railsenvy) and Make an Simple Dedicated Web Browser for it, and Put it together as one Beautiful App!

  43. #43 Jason Terhorst says:

    Hi, folks,
    If you’re joining the party, feel free to post any questions here in the comments. Please don’t email me with questions; I don’t have time to answer them all.
    Thanks.

  44. #44 Brian says:

    Love this tutorial, great work! Would love to know how to add a thumbnail image from the feed to the table view and how to link a video link from the XML feed to the media player framework… Got it to play a video when you click the article, but it launches in Safari… Can’t wait for an update/part two of this!

  45. #45 Adrian says:

    In the root view controller you can get the activity indicator to work with the code below, posting this to save someone else the few hours it took me to figure this out…

    Also if you get the warning mentioned in comment #32, you can fix it by moving the definition of that method ahead of the use of it in that file.

    - (void)viewDidLoad {
    // Add the following line if you want the list to be editable
    //self.navigationItem.leftBarButtonItem = self.editButtonItem;

    // make the activity indicator as the right side button
    CGRect frame = CGRectMake(0.0, 0.0, 25.0, 25.0);
    activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:frame];
    [activityIndicator startAnimating];
    [activityIndicator sizeToFit];
    activityIndicator.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin |
    UIViewAutoresizingFlexibleRightMargin |
    UIViewAutoresizingFlexibleTopMargin |
    UIViewAutoresizingFlexibleBottomMargin);

    UIBarButtonItem *loadingView = [[UIBarButtonItem alloc] initWithCustomView:activityIndicator];
    loadingView.target = self;
    self.navigationItem.rightBarButtonItem = loadingView;
    }

  46. #46 koffst3r says:

    Great tutorial for someone like me who is noobish.
    Tnx

  47. #47 Matthew Roberts says:

    How do you change where the RSS feed comes from?

  48. #48 Adrian says:

    The RSS feed address is hardwired in the viewDidAppear method. Easy to change…

  49. #49 Ernie Oporto says:

    I just changed the URL to point to a Flickr Interesting RSS feed I rolled up and it was easy to view. Too bad it’s so hard to get something like that onto my iPhone since I could see myself using that little app.

  50. #50 Arjun says:

    Hey, thank you so much man…wow…I’m a total NOOB, and even I was successful on 2nd try! But can you please tell me how to change the icon in the home screen for this app? Thank you again man…

  51. #51 Adrian says:

    drop the icon in the same folder as info.plist, and edit info.plist to tell it the name of your icon

  52. #52 Matthew Roberts says:

    Whenever I try to run the app, the rss feeds do not display. Help!

  53. #53 ac says:

    Very nice. Thanks.

  54. #54 Matthew Roberts says:

    As to my post #52, I fixed it. A couple of questions:
    1. How do I change the font size?
    2. Is it possible to show the date?

  55. #55 wbvdw says:

    @#54 -

    When I open in simulator it doesn’t display anything either, what did you determine was your problem? I’m assuming I forget something but checked my code twice and still haven’t found anything. I’m new to this so I’ll continue checking but, if anyone has any advice I’d appreciate it.

  56. #56 Ricco831 says:

    This is an awesome tutorial. I have been looking for a tutorial like this for the longest time. I actually found this site on http://www.iphonedevver.com which is an iPhone SDK tutorial search site.

  57. #57 kislay says:

    Thanks a lot . you explain nicely this topic thanks a lot

  58. #58 SANDIP says:

    I am new at i-phone 2.0 application developement….I am not getting at how to access CGrectangles in array!!Plz suggest me any way..Thanks.

  59. #59 james p says:

    How can I change the text size? Excellent tutorial, but I am finding the text too big!

  60. #60 Pierre says:

    Thank you :)

  61. #61 Eddie Wilson says:

    Correct me if I am wrong but shouldnt:

    currentElement = [elementName copy];

    be this?:

    NSMutableString *test = [[NSMutableString alloc] initWithString:elementName];
    self.currentElement = test;
    [test release];

    I might be wrong but the format would create a memory leak, while the later will not. Make sure you run your applications through Instruments.

  62. #62 Podcaster says:

    When you run this application through Instruments it shows memory leaks. I have tried to fix them but I cannot get them all.

    Any ideas? Has anyone else been able to fix the leaks and share when the changes need to be?

  63. #63 Jeff Lilly says:

    This is horrible code. There are memory leaks everywhere.

  64. #64 Jeff Lilly says:

    And no, the little bit of dealloc code at the end doesn’t fix it.

  65. #65 passsingby says:

    Any idea how to modify this zip to get it to work on ppc mac? I get the following when i try and build it.
    Line Location Tool:0: No architectures to compile for (ONLY_ACTIVE_ARCH=YES, active arch=ppc, VALID_ARCHS=i386).

  66. #66 km says:

    hahah
    wow i don’t get it
    well i do ~ but it doens’t work for me
    hahah

  67. #67 Shobi says:

    Very nice tutorial. I’m very much looking forward to seeing how to implement navigation into the next view (instead of launching safari.)

  68. #68 Andrew says:

    Very nice tutorial. Appreciate the work you put into it. One question though. With this parser, is there any reason why it should truncate the description of a feed? I did some digging and I think it may be a problem with RSS2.0. Can I rule this out or is a plausible reason for the problem? Thanks.

  69. #69 matt says:

    A much nicer/neater/standard activity indicator in the top status bar:

    UIApplication* app = [UIApplication sharedApplication];
    app.networkActivityIndicatorVisible = YES;

    and set it to NO when you’re done loading your data.

  70. #70 Matt says:

    You can download the Stanford University’s entire iPhone Development Lecture in PDF format here http://www.cultofiphone.org/forums/showthread.php?t=342

  71. #71 Dan Ribar says:

    Very nice job with all of this. Any ideas on how to add a ‘refresh’ button?

    I took the code and am displaying statistics — very cool way to get at XML data.

    Thanks.

  72. #72 Shobi says:

    @#69 -

    Where I can paste this code I mean which file?

    Thanks!

  73. #73 professor Shah says:

    Thanks for this tutorial. It helped me a great deal to develop my initial application.

  74. #74 Josh says:

    thanks I used this to write a program for my church sermon podcast but my question is 1 when i click on my link it opens another page in safari where i then have to choose the mp3, and if a use a rss.xml file the links dont work? http://bepsermons.libsyn.com/rss is my feed

  75. #75 mohrt says:

    Ugh, this is riddled with memory leaks.
    You’ll want to add this:

    if (currentElement) {
    [currentElement release];
    currentElement = nil;
    }

    before this:

    currentElement = [elementName copy];

    and this:

    if (stories) {
    [stories release];
    stories = nil;
    }

    before this:

    stories = [[NSMutableArray alloc] init];

    That helps a lot of it. Be sure to run this stuff through Instruments.

  76. #76 Sunil Kumar E says:

    Hi,

    I am using XCode3.1 beta. In this version, while starting a new project, under iPhoneOS->Application menu, no ‘Navigation-Based Application’ item is there. Only ‘Cocoa Touch Application’, ‘Cocoa Touch List’ and ‘Cocoa Touch Toolbar’ options are there. Is there anything wrong in these steps ? Or do i have to download any sort of plugins to get ‘Navigation-Based Application’ this option ?

    please direct me. i am in stucked stage.

    thanks and regards,
    sunil

  77. #77 sbwoodside says:

    For an app this size, and given that it runs once, these memory leaks are really irrelevant.

Leave a reply

Avatars
If you'd like an avatar to appear next to your comment, simply signup for a Gravatar. It's free and takes all of about 2 minutes to setup.

Subscribe without commenting

Close
E-mail It