How to Put Your Twitter Updates in your iPhone Apps, Part 2

Matthew Campbell, May 20th

Yesterday, I discussed the overall technique to use to add your Twitter updates to an iPhone app in code using XML.

Today, I will wrap-up by showing you how to implement this in code. If you need a refresher on what we are doing read through How to Put Your Twitter Updates in your iPhone Apps, Part 1 before moving on.

Lets See the Code!

The first thing we need to do is add the NSXMLParser object:

NSXMLParser * rssParser;

This is what will load and parse through the XML data.

Next, we need an array to store each RSS story:

NSMutableArray * stories;

Then, a dictionary to store data that corresponds to each item in a story. These items will correspond to elements.

NSMutableDictionary * item;

A string to keep track of what element we are currently parsing.

NSString * currentElement;

A string to keep track of what title we are currently parsing. Title here refers to the element “title” in an RSS feed.

NSMutableString * currentTitle;

We are going add of these items to the top level of our app delegate so that they remain in place through the entire process. They will be released in the dealloc method. Here is what we have so far.

#import "TipOfTheWeekAppDelegate.h"

@implementation TipOfTheWeekAppDelegate
@synthesize window;

@end

The next thing we need to do is implement the three methods we talked about in yesterday’s post: didStartElement, didEndElement & foundCharacter. An XML file can have many elements in it – these three methods will execute each time your parser encounters an element once we set the parser into motion.

Example of the RSS Twitter Feed

It will probably help clear things up for you if you can see the actual contents of the Twitter RSS feed we will be parsing.

<?xml version=”1.0″ encoding=”UTF-8″?>
<rss version=”2.0″ xmlns:atom=”http://www.w3.org/2005/Atom”>
<channel>
<title>Twitter / MattjDrake</title>
<link>http://twitter.com/MattjDrake</link>
<atom:link type=”application/rss+xml” rel=”self” href=”https://twitter.com/statuses/user_timeline/14079004.rss”/>
<description>Twitter updates from Matt Campbell / MattjDrake.</description>
<language>en-us</language>
<ttl>40</ttl>
<item>
<title>MattjDrake: [Blog] How to Put Your Twitter Updates in your iPhone Apps, Part 1: Would you like your iPhone app to present fresh … http://s3nt.com/g5ty</title>
<description>MattjDrake: [Blog] How to Put Your Twitter Updates in your iPhone Apps, Part 1: Would you like your iPhone app to present fresh … http://s3nt.com/g5ty</description>
<pubDate>Tue, 19 May 2009 17:08:02 +0000</pubDate>
<guid>http://twitter.com/MattjDrake/statuses/1849201709</guid>
<link>http://twitter.com/MattjDrake/statuses/1849201709</link>
</item>

…and so on, until we run out of items and close the channel and rss tags:

</channel>

</rss>

didStartElement

Every time the parser encounters an element we want to set the currentElement string to the name of the element. If the element happens to be called “item” we know that this means a new story is starting. In that case we will reset all the data storage objects we are using.

}

Item above is a dictionary that can hold the data from different, unique, elements in the XML file that are included under the item element. currentTitle is a mutable string that we can add characters to as the parser goes through the file.

foundCharacters

Every time the parser encounters a string the foundCharacters method will execute. Below, we use method to build the currentTitle string. We are only collecting information for the “title” element here, but you could use this to collect data for any element you are interested in.

didEndElement

Now, we need to do a few things each time we reach the end of an element. Really, we only need to worry about each time we reach the end of an “item” element here. When we do we will add the currentTitle string (which has been filled out at this point) to the item dictionary object. Then we will add the dictionary to the stories array.

In this way we are mirroring the structure of the RSS feed by using our data collection objects – stories array that hold a list of item dictionaries each of which hold a range of elements (we are only using currentTitle here).

}

Are You Ready to Parse?

Ok, we have the delegate methods all ready to go – once you set your parser into motion it will use these methods to collect our tweets from the RSS feed remotely.

I am going to put the code to set up and start the parser on its way in the applicationDidFinishLaunching method. First, lets initialize the stories array and create a NSURL that references my Twitter feed.

- (void)applicationDidFinishLaunching:(UIApplication *)application {

NSURL is often used when we need to get to web based data. Now, initialize the parser with the NSURL we just created:

rssParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL];

Set the rssParser’s delegate to self. This is how rssParser will know to use the methods we coded above.

rssParser.delegate = self;

Finally, let’s get to parsing by sending the “parse” message to the rssParser object. When you do this the entire XML file will be gone through element by element. Each time the conditions above are met (start of element, end of element and characters found) the delegate methods will execute filling up our data storage objects.

[rssParser parse];

Just for clarity, there is the entire method:

- (void)applicationDidFinishLaunching:(UIApplication *)application {

[window makeKeyAndVisible];
}

Discussion

If you build and run this app and examine the stories array you will see your data (or really my tweets) ready for use. That is it – the simplest demonstration that I could come up with for remote XML took two lengthy blog posts. Let me know in the comments if you have any questions about this post.

Also, I would be remiss if I did not give credit to Jason Terhorst of The Apple Blog; most of the code from this article is based on his tutorial, iPhone SDK Tutorial: Build a Simple RSS reader for the iPhone. If you follow the link to Jason’s article you will find an example of using a table view to and NSXMLParser to build an RSS reader.

[...] iPhone Apps, Part 2 May 20, 2009, 6:19 pm Filed under: Uncategorized (My Original Blog Post: http://howtomakeiphoneapps.com/2009/05/how-to-put-your-twitter-updates-in-your-iphone-apps-part-2/) Yesterday, I discussed the overall technique to use to add your Twitter updates to an iPhone app [...]

How to Put Your Twitter Updates in your iPhone Apps, Part 2…

You’ve been kicked (a good thing) – Trackback from iPhoneKicks.com – iPhone SDK links, community driven…

[...] How to Put Your Twitter Updates in your iPhone Apps, Part 2 | How to Make iPhone Apps (tags: iphone twitter viapssteam) [...]

  • Hours of videos, demos & screen casts
  • SOURCE CODE
  • Detailed HANDS-ON Exercises
  • Much Much Much More
  • UIKit
  • Objective-C
  • Core Data
  • More!
  • Learn How To Make An iPhone App right now using my proven system.

    PS: Tons of us are having a blast living off our own code, join us now. Click here to get all the details.