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

Matthew Campbell, May 19th, 2009

Would you like your iPhone app to present fresh information to your users as it is made available on the web?

Doing this on the iPhone is relatively painless, but the procedure may be different than what you are used to. The method used on the iPhone involves using the delegation pattern. You will see later on what this entails. But, first lets describe what we are trying to do.

Background Information on XML and Remote Data Access

What we are trying to do is provide content to our users that is refreshed from our web server on a regular basis. One easy way to do this is to have your server provide data stored as an XML file. XML is a way to store data using a tag scheme – all the meaningful bits of information are enclosed in tags which are in a hierarchy. This makes it easy to organize information.

Example XML

Here is an example of what data in an XML file may look like:

<CATAGLOG>
<CD>
<TITLE>The Dark Side of the Moon</TITLE>
< ARTIST>Pink Floyd</ARTIST>
<CD/>
<CD>
<TITLE>Do or Die</TITLE>
< ARTIST>Dropkick Murphys</ARTIST>
<CD/>
<CATAGLOG/>

Generally, XML is used to store data and information – not anything involving the presentation or behavior of information. Check out the Wikipedia entry on XML to get more information on the details of this subject.

RSS & Twitter Both use XML

If you have a blog with an RSS feed then have a ready made source of XML what you can use in your app. RSS technology uses XML to store data. Twitter also uses RSS to publish each user’s updates. Below, I will be using my Twitter RSS feed in the example.

You can use either your RSS feeds or your Twitter RSS feed to give your users fresh content from the web automatically. In fact, you can adapt this technique to any web based source of XML data.

So, What Are We Doing Again?

In order to keep things simple, I am going to put all the code we need into my app delegate. I will use my Twitter feed to extract everything in the “title” element and write it out to the log.

How it is going to work is that I will be using an object of the NSXMLParser class that will read in the remote XML file. The NSXMLParser class object will have its delegate set to my app delegate where I will implement the methods that are required to parse through the XML file.

What the [Redacted] is Delegation?

When you use the delegation pattern as we are here what you are doing is assigning one object act on behalf of another object. Typically, this means that we are going to need to implement some methods in the object that is acting as a delegate. Here, we are going to assign the app delegate to act as the delegate for an NSXMLParser object.

This means that our app delegate is going to need to implement three methods on behalf of our NSXMLParser object: didStartElement, didEndElement & foundCharacters.

didStartElement

- executes when the parser encounters the beginning of an element in the file.

<TITLE>The Dark Side of the Moon</TITLE>

didEndElement

- executes when the parser encounters the end of an element in the file.

<TITLE>The Dark Side of the Moon</TITLE>

foundCharacters

- executes when text between elements is encountered.

<TITLE>The Dark Side of the Moon</TITLE>

How Do We Get the Data Out?

Essentially, we are going to put code into each of these methods that will store data in appropriate structures as needed. When the parser encounters a start element it will create new storage structures, when it encounters characters it will store them in structure corresponding to the element and when it ends it will insert the data in the correct place.

Since this blog post is starting to get a bit lengthy, I will leave the code for my next post tomorrow.

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

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

[...] 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 [...]

  • 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.