<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>iPhone Programming Tutorials &#187; twitter</title>
	<atom:link href="/tag/twitter/feed/" rel="self" type="application/rss+xml" />
	<link>http://icodeblog.com</link>
	<description>iPhone Programming Tutorials</description>
	<lastBuildDate>Tue, 19 Nov 2013 19:34:46 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=4.1.1</generator>
	<item>
		<title>Dealing with the Twitter Oauth-Apocalypse</title>
		<link>http://icodeblog.com/2010/09/16/dealing-with-the-twitter-oauth-apocalypse/</link>
		<comments>http://icodeblog.com/2010/09/16/dealing-with-the-twitter-oauth-apocalypse/#comments</comments>
		<pubDate>Thu, 16 Sep 2010 15:00:34 +0000</pubDate>
		<dc:creator><![CDATA[Collin]]></dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Advanced]]></category>
		<category><![CDATA[Oauth]]></category>
		<category><![CDATA[twitter]]></category>

		<guid isPermaLink="false">http://icodeblog.com/?p=2362</guid>
		<description><![CDATA[As many of you may have seen in recent weeks, Twitter changed its access policies and now <a href="http://www.geek.com/articles/news/twitter-flips-the-oauth-switch-for-third-parties-update-your-apps-today-20100831/" target="_blank">requires OAuth from all third party applications</a> that access Twitter user accounts. This is a large change from how many iPhone developers having been incorporating Twitter into their applications. What is OAuth exactly? How can iPhone developers get their apps up to date so they don&#8217;t break their Twitter incorporation? Well we have all the info you need to know about  ...]]></description>
				<content:encoded><![CDATA[<p>As many of you may have seen in recent weeks, Twitter changed its access policies and now <a href="http://www.geek.com/articles/news/twitter-flips-the-oauth-switch-for-third-parties-update-your-apps-today-20100831/" target="_blank">requires OAuth from all third party applications</a> that access Twitter user accounts. This is a large change from how many iPhone developers having been incorporating Twitter into their applications. What is OAuth exactly? How can iPhone developers get their apps up to date so they don&#8217;t break their Twitter incorporation? Well we have all the info you need to know about the OAuth-Apocalypse.</p>
<h2><span style="color: #ff6600;">What is Oauth?</span></h2>
<p><span style="color: #000000;">Twitter has a brief explanation of the difference between Basic Authentication and OAuth. They use the example of a letter and how it is addressed as their metaphor, and I think this gets the idea across clearly. You can see their entire explanation along with pros and cons <a href="http://dev.twitter.com/pages/basic_to_oauth" target="_blank">here</a>. </span></p>
<p><span style="color: #000000;">TL;DR With Basic Auth you would make every request and would include a username and password with each. This method is very insecure because it allows applications to actively hold user&#8217;s credentials and does not have any accountability for which application is performing what action on who&#8217;s account. If you imagine your Twitter account as a room, this is like having access to the room be granted based on a single key that many people have copies of. OAuth makes access to your room be granted by a key pad. And everyone that has access to the room has a different code they input into the keypad. You can see who came in and when and revoke anyone&#8217;s code at any time.</span></p>
<p><span style="color: #000000;">Twitter is evolving and now requiring that applications register. As a result, user&#8217;s gain more control over what services have access to their account. Additionally, Twitter can be more effective at targeting malicious applications, and developers can get more accurate feedback on the frequency of their app&#8217;s use throughout Twitter. With all this said, OAuth is tough to implement from scratch. So today we are going to go through the installation and use of a collection of classes that takes care of the dirty work for us, and let developers update the Twitter functionality of their application easily.</span></p>
<h2><span style="color: #ff6600;">Required Classes</span></h2>
<p><span style="color: #000000;">Today we are going to be building off a collection of classes that were created by several different people. The main portion of the class is the MGTwitterEngine which was created by Matt Legend Gemmell who&#8217;s website can be found <a href="http://mattgemmell.com" target="_blank">here</a>. From here, Ben Gottlieb took the classes and added his own drop in view controller to them. With all this together we have a simple view controller that will perform login and OAuth, and from there an engine that will perform any type of Twitter request we are looking to do. You can get a zip file of the root folder that contains all of these files <a href="/wp-content/uploads/2010/09/Twitter+OAuth1.zip" target="_blank">here</a>. </span></p>
<h2><span style="color: #ff6600;">Installation</span></h2>
<p>We are going to start out with a blank, view based iPhone project called iCodeOAuth. Once the project has come up, take the folder you downloaded called &#8220;Twitter+OAuth&#8221; and drag it into the &#8220;Other Sources&#8221; folder within Xcode. Make sure you check the box to copy the sources into the project folder. If we do a build now, you will get a ton of errors. That is because these classes require that libXML be a target of the project build as well. This can be accomplished by clicking the arrow next to Targets in the left column of the Xcode project. Here there will be an application called iCodeOAuth. If we right click on this and select Get Info we will see the info about our target. From here we click the build tab and search for the field &#8220;Header Search Paths&#8221;. You need to add the following into the Header Search Paths:</p>
<p>$(SDKROOT)/usr/include/libxml2</p>
<p><strong>EDIT: I forgot to include here that you must also include a special XML library for the build to be successful. The Framework is called libxml2.dylib. You can find it at</strong></p>
<p><strong>/Macintosh HD/Developer/Platforms/iPhoneOS.platform/Developer/SDK/iPhoneOS4.1SDK/usr/lib</strong></p>
<p>If you build again, you should see no errors and we can move forward with using this awesome set of classes.</p>
<h2><span style="color: #ff6600;">Getting your Creds from Twitter</span></h2>
<p><span style="color: #000000;">Now that we have these classes properly installed, it is time to take care of some registration requirements for OAuth. In order to use OAuth you must identify your app to Twitter. Once you do so, Twitter will provide you with an OAuthConsumerKey and an OAuthConsumerSecretKey. These are going to need to be provided to the classes we have just added into our project in order to talk with Twitter&#8217;s OAuth system. To register your application and get these creds go to <a href="http://dev.twitter.com/apps/new" target="_blank">http://dev.twitter.com/apps/new</a>. I have created a application called the iCodeBlog OAuth Demo, whose credentials are included in the sample app which I have provided. For your own personal app you will need to go create your own Twitter Application and get your own keys.</span></p>
<h2><span style="color: #ff6600;">Using SA_OAuthTwitter Engine</span></h2>
<p>Ben Gottlieb used a great design pattern to create a very easy to use access point for the more complex MGTwitterEngine which lies underneath. In order to use these classes we will go into out view controller and add the following code to the header:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &quot;SA_OAuthTwitterEngine.h&quot;</span>
<span style="color: #6e371a;">#import &quot;SA_OAuthTwitterController.h&quot;</span>
&nbsp;
<span style="color: #a61390;">@interface</span> iCodeOauthViewController <span style="color: #002200;">:</span> UIViewController  <span style="color: #002200;">&#123;</span>
&nbsp;
	IBOutlet UITableView <span style="color: #002200;">*</span>tableView;
	IBOutlet UITextField <span style="color: #002200;">*</span>textfield;
&nbsp;
	SA_OAuthTwitterEngine <span style="color: #002200;">*</span>_engine;
	<span style="color: #400080;">NSMutableArray</span> <span style="color: #002200;">*</span>tweets;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, retain<span style="color: #002200;">&#41;</span> IBOutlet UITableView <span style="color: #002200;">*</span>tableView;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, retain<span style="color: #002200;">&#41;</span> IBOutlet UITextField <span style="color: #002200;">*</span>textfield;
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span>IBAction<span style="color: #002200;">&#41;</span>updateStream<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>sender;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span>IBAction<span style="color: #002200;">&#41;</span>tweet<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>sender;
&nbsp;
<span style="color: #a61390;">@end</span></pre></td></tr></table></div>

<p>And add the following into the Main</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>viewDidAppear<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span>animated <span style="color: #002200;">&#123;</span>
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>_engine<span style="color: #002200;">&#41;</span> <span style="color: #a61390;">return</span>;
&nbsp;
	_engine <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>SA_OAuthTwitterEngine alloc<span style="color: #002200;">&#93;</span> initOAuthWithDelegate<span style="color: #002200;">:</span>self<span style="color: #002200;">&#93;</span>;
	_engine.consumerKey <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;PzkZj9g57ah2bcB58mD4Q&quot;</span>;
	_engine.consumerSecret <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;OvogWpara8xybjMUDGcLklOeZSF12xnYHLE37rel2g&quot;</span>;
&nbsp;
	UIViewController <span style="color: #002200;">*</span>controller <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>SA_OAuthTwitterController controllerToEnterCredentialsWithTwitterEngine<span style="color: #002200;">:</span> _engine delegate<span style="color: #002200;">:</span> self<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>controller<span style="color: #002200;">&#41;</span>
		<span style="color: #002200;">&#91;</span>self presentModalViewController<span style="color: #002200;">:</span> controller animated<span style="color: #002200;">:</span> <span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">else</span> <span style="color: #002200;">&#123;</span>
		tweets <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableArray</span> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span>self updateStream<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>This will instantiate our engine with the appropriate consumer and consumer secret keys. With this done we will create a controller. If you run the app now you will see a modal web view come up and lead to a sign in page for Twitter. This is a web view, but the great classes written by Ben are set up to programmatically handle the progression of these web views as the user signs in.</p>
<p><a href="/wp-content/uploads/2010/09/Screen-shot-2010-09-15-at-4.25.08-PM1.png"><img class="aligncenter size-full wp-image-2374" title="Screen shot 2010-09-15 at 4.25.08 PM" src="/wp-content/uploads/2010/09/Screen-shot-2010-09-15-at-4.25.08-PM1.png" alt="" width="414" height="770" /></a>Scrolling to the bottom of this page there will be a username and password field to fill out. Don&#8217;t put in your Twitter credentials yet. We need to fill out a few delegate methods to handle the callback from SA_OAuthTwitterEngine.</p>
<h2><span style="color: #ff6600;">Handling Login Callbacks</span></h2>
<p>Insert the following into your main class.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;">&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span>IBAction<span style="color: #002200;">&#41;</span>updateStream<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>sender <span style="color: #002200;">&#123;</span>
&nbsp;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span>IBAction<span style="color: #002200;">&#41;</span>tweet<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>sender <span style="color: #002200;">&#123;</span>
&nbsp;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #6e371a;">#pragma mark SA_OAuthTwitterEngineDelegate</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> storeCachedTwitterOAuthData<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> data forUsername<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> username <span style="color: #002200;">&#123;</span>
&nbsp;
	<span style="color: #400080;">NSUserDefaults</span>	<span style="color: #002200;">*</span>defaults <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSUserDefaults</span> standardUserDefaults<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #002200;">&#91;</span>defaults setObject<span style="color: #002200;">:</span> data forKey<span style="color: #002200;">:</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;authData&quot;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>defaults synchronize<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> cachedTwitterOAuthDataForUsername<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> username <span style="color: #002200;">&#123;</span>
&nbsp;
	<span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSUserDefaults</span> standardUserDefaults<span style="color: #002200;">&#93;</span> objectForKey<span style="color: #002200;">:</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;authData&quot;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #6e371a;">#pragma mark SA_OAuthTwitterController Delegate</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> OAuthTwitterController<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span>SA_OAuthTwitterController <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> controller authenticatedWithUsername<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> username <span style="color: #002200;">&#123;</span>
&nbsp;
	NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Authenticated with user %@&quot;</span>, username<span style="color: #002200;">&#41;</span>;
&nbsp;
	tweets <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableArray</span> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>self updateStream<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> OAuthTwitterControllerFailed<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span>SA_OAuthTwitterController <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> controller <span style="color: #002200;">&#123;</span>
&nbsp;
	NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Authentication Failure&quot;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> OAuthTwitterControllerCanceled<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span>SA_OAuthTwitterController <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> controller <span style="color: #002200;">&#123;</span>
&nbsp;
	NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Authentication Canceled&quot;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>We just implemented the SA_OAuthTwitterControllerDelegate and the SA_OAuthTwitterEngineDelegate. The SA_OAuthTwitterEngineDelegate methods take care of storing the OAuth data string in a plist so that when the app is launched again the user will not have to sign in. SA_OAuthTwitterControllerDelegate methods are callbacks depending on what happens upon sign in. In this case when sign in is successful another method in our class called updateTweets will fire. For now we have those methods defined but we don&#8217;t have them filled in. We will get to that in a few steps. If you run the application and login using some Twitter credentials, you should see a successful authentication message appear in your debug screen. With this done, let&#8217;s add some interface elements to our view controller XIB so that we can start interacting with Twitter. Our final product is going to look like this:<br />
<a href="http://i.imgur.com/cOHPh.png"><img class="aligncenter size-full wp-image-2374" title="Screen shot 2010-09-15 at 4.25.08 PM" src="http://i.imgur.com/cOHPh.png" alt="" width="414" height="770" /></a></p>
<h2><span style="color: #ff6600;">Building the Interface</span></h2>
<p>To Build the interface open up the XIB for your view controller. We are going to be putting in 2 buttons, a UITextField and a UITableView. Lay the elements out like so.<br />
<a href="http://i.imgur.com/hEJ10.png"><img class="aligncenter size-full wp-image-2374" title="Screen shot 2010-09-15 at 4.25.08 PM" src="http://i.imgur.com/hEJ10.png" alt="" width="400" height="582" /></a></p>
<p>Make sure to connect the delegate and data source of the table view to the file owner. Also connect the &#8220;Tweet This&#8221; button to the tweet method and the Update Tweets method to the updateStream method. Finally, make sure the IBOutlets for the UITextField and the UITableView are set. With these in place we can fill in the final methods to take advantage of our Twitter engine.</p>
<h2><span style="color: #ff6600;">Filling in our IBActions</span></h2>
<p>Put the following code in for the IBActions which we defied before.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#pragma mark IBActions</span>
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span>IBAction<span style="color: #002200;">&#41;</span>updateStream<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>sender <span style="color: #002200;">&#123;</span>
&nbsp;
	<span style="color: #002200;">&#91;</span>_engine getFollowedTimelineSinceID<span style="color: #002200;">:</span><span style="color: #2400d9;">1</span> startingAtPage<span style="color: #002200;">:</span><span style="color: #2400d9;">1</span> count<span style="color: #002200;">:</span><span style="color: #2400d9;">100</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span>IBAction<span style="color: #002200;">&#41;</span>tweet<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>sender <span style="color: #002200;">&#123;</span>
&nbsp;
	<span style="color: #002200;">&#91;</span>textfield resignFirstResponder<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>_engine sendUpdate<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>textfield text<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>self updateStream<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>The update stream method will ask our engine to get the Twitter timeline of the people you follow. It will retrieve the first page of the first 100 tweets. There is a delegate method that fires off on this request completing that we will fill out in a moment. The tweet method dismisses the keyboard and then uses our engine to send an update. Once the update is send we update the tweet view below.</p>
<h2><span style="color: #ff6600;">Making a really simple Tweet Object</span></h2>
<p>To help us with displaying tweets we are going to make a very quick Tweet object. This will be a simple NSObject subclass. Use this code for the header:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> Tweet <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span> <span style="color: #002200;">&#123;</span>
&nbsp;
	<span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span>contents;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>tweet;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>author;
&nbsp;
<span style="color: #a61390;">@end</span></pre></td></tr></table></div>

<p>And this code for the main</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@implementation</span> Tweet
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>initWithTweetDictionary<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSDictionary</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>_contents <span style="color: #002200;">&#123;</span>
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>self <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>super init<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
&nbsp;
		contents <span style="color: #002200;">=</span> _contents;
		<span style="color: #002200;">&#91;</span>contents retain<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #a61390;">return</span> self;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>tweet <span style="color: #002200;">&#123;</span>
&nbsp;
	<span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span>contents objectForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;text&quot;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>author <span style="color: #002200;">&#123;</span>
&nbsp;
	<span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>contents objectForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;user&quot;</span><span style="color: #002200;">&#93;</span> objectForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;screen_name&quot;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
<span style="color: #a61390;">@end</span></pre></td></tr></table></div>

<p>Finally make sure to import the class within the main of your view controller class. These will simply give easy methods to get the info we want out of each tweet dictionary that the MGTwitterEngine will return to us.</p>
<h2><span style="color: #ff6600;">Filling in our MGTwitterEngineDelegate Methods</span></h2>
<p>The MGTwitterEngine is doing most of the heavy lifting here when it comes to interacting with Twitter. The methods we are using to get tweets and to send tweets are all defined within the MGTwitterEngine. There is also a defined MGTwtterEngineDelegate which defines the callback methods that fire upon these requests finishing. For the sake of completeness, I have filled out all the methods, although only a few of them will be called in the case of our application working properly. Insert the following code into your main.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#pragma mark MGTwitterEngineDelegate Methods</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>requestSucceeded<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>connectionIdentifier <span style="color: #002200;">&#123;</span>
&nbsp;
	NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Request Suceeded: %@&quot;</span>, connectionIdentifier<span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>statusesReceived<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>statuses forRequest<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>connectionIdentifier <span style="color: #002200;">&#123;</span>
&nbsp;
	tweets <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableArray</span> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span>d <span style="color: #a61390;">in</span> statuses<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
&nbsp;
		NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;See dictionary: %@&quot;</span>, d<span style="color: #002200;">&#41;</span>;
&nbsp;
		Tweet <span style="color: #002200;">*</span>tweet <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>Tweet alloc<span style="color: #002200;">&#93;</span> initWithTweetDictionary<span style="color: #002200;">:</span>d<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span>tweets addObject<span style="color: #002200;">:</span>tweet<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span>tweet release<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #002200;">&#91;</span>self.tableView reloadData<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>receivedObject<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>dictionary forRequest<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>connectionIdentifier <span style="color: #002200;">&#123;</span>
&nbsp;
	NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Recieved Object: %@&quot;</span>, dictionary<span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>directMessagesReceived<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>messages forRequest<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>connectionIdentifier <span style="color: #002200;">&#123;</span>
&nbsp;
	NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Direct Messages Received: %@&quot;</span>, messages<span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>userInfoReceived<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>userInfo forRequest<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>connectionIdentifier <span style="color: #002200;">&#123;</span>
&nbsp;
	NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;User Info Received: %@&quot;</span>, userInfo<span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>miscInfoReceived<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>miscInfo forRequest<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>connectionIdentifier <span style="color: #002200;">&#123;</span>
&nbsp;
	NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Misc Info Received: %@&quot;</span>, miscInfo<span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>These methods are all very straightforward in their naming. The only one we fill out significantly is the statusesReceived:forRequest method. Here is where tweets will be returned to us, each as a separate dictionary when we request the timeline for a user. We will clear the tweets array that we have defined for our class and create a Tweet object for each of the dictionaries we have representing a tweet. From there we will ask our table view to reload. The only task we have left to perform is to fill in our table view data source methods to show all the tweets.</p>
<h2><span style="color: #ff6600;">Table View Data Source and Delegate Methods</span></h2>
<p>Now we have everything in place. We just need to create a UITableViewCell for every tweet we have. We will also do some tweaking of the size of each cell and the number of lines of each UITextField within each table view cell. These methods are very common so I wont go into much detail on them. Here are the necessary Data Source and Delegate Methods.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#pragma mark UITableViewDataSource Methods</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span>tableView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UITableView <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>tableView numberOfRowsInSection<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span>section <span style="color: #002200;">&#123;</span>
&nbsp;
	<span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span>tweets count<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>UITableViewCell <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>tableView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UITableView <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>tableView cellForRowAtIndexPath<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSIndexPath</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>indexPath <span style="color: #002200;">&#123;</span>
&nbsp;
	<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>identifier <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;cell&quot;</span>;
	UITableViewCell <span style="color: #002200;">*</span>cell <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>tableView dequeueReusableCellWithIdentifier<span style="color: #002200;">:</span>identifier<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span>cell<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
&nbsp;
		cell <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UITableViewCell alloc<span style="color: #002200;">&#93;</span> initWithStyle<span style="color: #002200;">:</span>UITableViewStyleGrouped reuseIdentifier<span style="color: #002200;">:</span>identifier<span style="color: #002200;">&#93;</span>;
		<span style="color: #11740a; font-style: italic;">//[cell setBackgroundColor:[UIColor clearColor]];</span>
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #002200;">&#91;</span>cell.textLabel setNumberOfLines<span style="color: #002200;">:</span><span style="color: #2400d9;">7</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>cell.textLabel setText<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#40;</span>Tweet<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#91;</span>tweets objectAtIndex<span style="color: #002200;">:</span>indexPath.row<span style="color: #002200;">&#93;</span> tweet<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #a61390;">return</span> cell;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>CGFloat<span style="color: #002200;">&#41;</span>tableView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UITableView <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>tableView heightForRowAtIndexPath<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSIndexPath</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>indexPath <span style="color: #002200;">&#123;</span>
&nbsp;
	<span style="color: #a61390;">return</span> <span style="color: #2400d9;">150</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>With this in place you will have an OAuth twitter client that is capable of doing any type of interaction with Twitter. You can find the source for the project <a href="/wp-content/uploads/2010/09/iCodeOauth1.zip">here</a>. Please post any questions you have and happy coding!</p>
<p>Follow me on Twitter at <a href="http://www.twitter.com/cruffenach" target="_blank">@cruffenach</a></p>
]]></content:encoded>
			<wfw:commentRss>http://icodeblog.com/2010/09/16/dealing-with-the-twitter-oauth-apocalypse/feed/</wfw:commentRss>
		<slash:comments>124</slash:comments>
		</item>
		<item>
		<title>Diving into the Twitter Stream</title>
		<link>http://icodeblog.com/2010/05/21/diving-into-the-twitter-stream/</link>
		<comments>http://icodeblog.com/2010/05/21/diving-into-the-twitter-stream/#comments</comments>
		<pubDate>Fri, 21 May 2010 21:37:47 +0000</pubDate>
		<dc:creator><![CDATA[Collin]]></dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Intermediate]]></category>
		<category><![CDATA[NSURLConnection]]></category>
		<category><![CDATA[twitter]]></category>
		<category><![CDATA[Twitter Stream]]></category>
		<category><![CDATA[Web Service]]></category>

		<guid isPermaLink="false">http://icodeblog.com/?p=1981</guid>
		<description><![CDATA[Hey iCoders. As I posted earlier I recently made an iPad app called TweetMapper. I just put out a new release of the app with a big new feature. The app now has a scrolling timeline of the tweets it is seeing as they come in. In order to make this app I took advantage of the Twitter Stream API that is provided by twitter. This API creates a persistent connection between the Twitter servers and your application. We will essentially start a stream of incoming NSData object to an NSURLConnection that you create querying the stream. We will look into the different search parameters which can be passed into the request, the way in which our code responds to authentication requests from Twitter, and the logic we must use to ensure that the data we have received is a complete XML element and not chopped off.]]></description>
				<content:encoded><![CDATA[<p><a href="/wp-content/uploads/2010/05/Twitter-Bird2.png"><img class="alignleft size-full wp-image-1984" title="Twitter-Bird" src="/wp-content/uploads/2010/05/Twitter-Bird2.png" alt="" width="256" height="256" /></a><span style="color: #ff6600;"><br />
</span>Hey iCoders. As I posted earlier I recently made an iPad app called <a title="Tweet Mapper in the App Store" href="http://itunes.apple.com/app/tweet-mapper/id367888965?mt=8" target="_blank">TweetMapper</a>. I just put out a new release of the app with a big new feature. The app now has a scrolling timeline of the tweets it is seeing as they come in. In order to make this app I took advantage of the <a title="Twitter Stream API" href="http://apiwiki.twitter.com/Streaming-API-Documentation" target="_blank">Twitter Stream API</a> that is provided by twitter. This API creates a persistent connection between the Twitter servers and your application. We will essentially start a stream of incoming NSData object to an NSURLConnection that you create querying the stream. We will look into the different search parameters which can be passed into the request, the way in which our code responds to authentication requests from Twitter, and the logic we must use to ensure that the data we have received is a complete XML element and not chopped off. There are three major steps to taking advantage of this in your app.</p>
<ol>
<li>Create an NSURLConnection to request an XML response from the Stream.</li>
<li>Create a parameter string for the HTTP Body.</li>
<li>Respond to the authentication challenge with user credentials.</li>
<li>Append the data as it comes in and when a complete element is received parse the Tweets.</li>
</ol>
<p>So lets dive in.</p>
<h2><span style="color: #ff6600;">Creating the Class</span></h2>
<p>The first thing that we need to do is establish a connection with the Twitter stream. I will not be creating a Twitter client class that can be plugged into any application you choose. You will be able to find the class as a download at the end of the post. We will call the class TwitterStreamClient. Lets first define the header file of the class.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import</span>
<span style="color: #6e371a;">#import &quot;TouchXML.h&quot;</span>
<span style="color: #6e371a;">#import &quot;Tweet.h&quot;</span>
&nbsp;
<span style="color: #a61390;">@interface</span> TwitterClient <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span> <span style="color: #002200;">&#123;</span>
&nbsp;
	<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>searchString;
	<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>locationString;
	<span style="color: #400080;">NSMutableString</span> <span style="color: #002200;">*</span>holderString;
	<span style="color: #a61390;">id</span> mapController;
	<span style="color: #400080;">NSURLConnection</span> <span style="color: #002200;">*</span>connection;
	<span style="color: #400080;">NSMutableURLRequest</span> <span style="color: #002200;">*</span>request;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, assign<span style="color: #002200;">&#41;</span> <span style="color: #a61390;">id</span> mapController;
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>startStreamingTweets;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>makeMyRequest;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>searchByLocation;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>searchByTerm;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>searchByUser;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>parseXMLString<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>xmlString;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>locationStringForLongitude<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">double</span><span style="color: #002200;">&#41;</span>_long Latitude<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">double</span><span style="color: #002200;">&#41;</span>_lat;
&nbsp;
<span style="color: #a61390;">@end</span></pre></td></tr></table></div>

<p>You will need to have TouchXML installed for this class to work. You can find installation instructions <a title="TouchXML Installation" href="http://code.google.com/p/touchcode/wiki/TouchXMLHowTo" target="_blank">here</a>. Let move onto the .m file of the TwitterStreamClient. First thing to do is create the initializer. Ours will look like this:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span>init <span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>self <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>super init<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
		holderString <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableString</span> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #a61390;">return</span> self;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>This holder string is what we are going to use to synchronize our incoming NSData from the service. We will see the reason for this in the next step.</p>
<h2><span style="color: #ff6600;">Connect to Service</span></h2>
<p><span style="color: #ff6600;"><span style="color: #000000;">The Twitter stream provides many different parameters for users to pass into their request. You can see the full list <a title="Twitter Stream Query Parameters" href="http://apiwiki.twitter.com/Streaming-API-Documentation#QueryParameters" target="_blank">here</a>, but we will be focusing on user specific stream, keyword specific stream and location specific stream. We are going to be hardcoding what these will be searching for but in use you can pass in whatever values you like. We will create an NSMutableURLRequest and fill in its HTTP body with the appropriate request. First we will make a method to create the stream request.</span></span></p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>makeMyRequest <span style="color: #002200;">&#123;</span>
&nbsp;
	request <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableURLRequest</span> alloc<span style="color: #002200;">&#93;</span> initWithURL<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSURL</span> URLWithString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;http://stream.twitter.com/1/statuses/filter.xml&quot;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>request setHTTPMethod<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;POST&quot;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>With this done we will create three different methods that can be called. One that bases its search on keywords, another based on users and a final one based on location.</p>
<p>Request by keyword</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>searchByTerm <span style="color: #002200;">&#123;</span>
&nbsp;
	searchString <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;track=Love,Hate,Want,Need&quot;</span>;
&nbsp;
	<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>httpBody <span style="color: #002200;">=</span> searchString;
	<span style="color: #002200;">&#91;</span>request setHTTPBody<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>httpBody dataUsingEncoding<span style="color: #002200;">:</span>NSUTF8StringEncoding<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
	connection <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSURLConnection</span> alloc<span style="color: #002200;">&#93;</span> initWithRequest<span style="color: #002200;">:</span>request delegate<span style="color: #002200;">:</span>self startImmediately<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>request release<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>Request by User ID</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>searchByUser <span style="color: #002200;">&#123;</span>
&nbsp;
	searchString <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;follow=14402149,29089557,807095,19058681&quot;</span>;
&nbsp;
	<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>httpBody <span style="color: #002200;">=</span> searchString;
	<span style="color: #002200;">&#91;</span>request setHTTPBody<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>httpBody dataUsingEncoding<span style="color: #002200;">:</span>NSUTF8StringEncoding<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
	connection <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSURLConnection</span> alloc<span style="color: #002200;">&#93;</span> initWithRequest<span style="color: #002200;">:</span>request delegate<span style="color: #002200;">:</span>self startImmediately<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>request release<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>The final method will create a location string. The twitter stream requires passing in 4 coordinates, 2 coordinate pairs. This will define the southwest most point and northeast most point. The square that the coordinates define has a maximum of 1 degree length for any side of the defined box. My helper method takes in a longitude and latitude as a center points and creates as large an area as possible around it and returns the 4 coordinates as a string. Paste in the following code to search with center points on Tempe, AZ and New York, NY.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>locationStringForLongitude<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">double</span><span style="color: #002200;">&#41;</span>_long Latitude<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">double</span><span style="color: #002200;">&#41;</span>_lat <span style="color: #002200;">&#123;</span>
&nbsp;
	<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>returnString <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;%f,%f,%f,%f&quot;</span>,<span style="color: #002200;">&#40;</span>_long<span style="color: #002200;">-</span>.5<span style="color: #002200;">&#41;</span>,<span style="color: #002200;">&#40;</span>_lat<span style="color: #002200;">-</span>.5<span style="color: #002200;">&#41;</span>,<span style="color: #002200;">&#40;</span>_long<span style="color: #002200;">+</span>.5<span style="color: #002200;">&#41;</span>,<span style="color: #002200;">&#40;</span>_lat<span style="color: #002200;">+</span>.5<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #a61390;">return</span> returnString;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>searchByLocation <span style="color: #002200;">&#123;</span>
&nbsp;
	searchString <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;locations=%@,%@&quot;</span>,
					  <span style="color: #002200;">&#91;</span>self locationStringForLongitude<span style="color: #002200;">:-</span><span style="color: #2400d9;">111.932898</span> Latitude<span style="color: #002200;">:</span><span style="color: #2400d9;">33.419265</span><span style="color: #002200;">&#93;</span>,
					  <span style="color: #002200;">&#91;</span>self locationStringForLongitude<span style="color: #002200;">:-</span><span style="color: #2400d9;">74.0</span> Latitude<span style="color: #002200;">:</span><span style="color: #2400d9;">40.7</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>httpBody <span style="color: #002200;">=</span> searchString;
	<span style="color: #002200;">&#91;</span>request setHTTPBody<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>httpBody dataUsingEncoding<span style="color: #002200;">:</span>NSUTF8StringEncoding<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
	connection <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSURLConnection</span> alloc<span style="color: #002200;">&#93;</span> initWithRequest<span style="color: #002200;">:</span>request delegate<span style="color: #002200;">:</span>self startImmediately<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>request release<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<h2><span style="color: #ff6600;">Answering Authentication</span></h2>
<p><span style="color: #000000;">The NSURLConnection will connect to the Twitter stream and then Twitter will ask for a username and password for the request. With the NSURLConnection&#8217;s delegate set to self we will implement the following method to answer the TwitterRequest.</span></p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>connection<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSURLConnection</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>connection didReceiveAuthenticationChallenge<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSURLAuthenticationChallenge</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>challenge <span style="color: #002200;">&#123;</span>
&nbsp;
	<span style="color: #400080;">NSURLCredential</span> <span style="color: #002200;">*</span>cred <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSURLCredential</span> alloc<span style="color: #002200;">&#93;</span> initWithUser<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;USERNAME&quot;</span> password<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;PASSWORD&quot;</span> persistence<span style="color: #002200;">:</span>NSURLCredentialPersistencePermanent<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>challenge sender<span style="color: #002200;">&#93;</span> useCredential<span style="color: #002200;">:</span>cred forAuthenticationChallenge<span style="color: #002200;">:</span>challenge<span style="color: #002200;">&#93;</span>;
	NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Received Challenge&quot;</span><span style="color: #002200;">&#41;</span>;
	<span style="color: #002200;">&#91;</span>cred release<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>This should satisfy the authentication challenge and the stream should start sending you data.</p>
<h2><span style="color: #ff6600;">Receiving Data</span></h2>
<p>Now the stream will start sending you data, it will coming into another NSURLConnection delegate method. The stream will send NSData into this method. The NSData can be parsed into a string and if you print it you will see output like this:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="xml" style="font-family:monospace;">2010-05-21 14:05:17.491 TweetMapper[72280:207] Recieved Data from Stream:
 2010-05-21 14:05:17.919 TweetMapper[72280:207] Recieved Data from Stream:
  Fri May 21 21:05:17 +0000 2010
  14452520592
  Need some followers yo I jus got this twitter its pretty str8
  <span style="color: #ddbb00;">&amp;lt;</span>a href=&quot;http://mobile.twitter.com&quot; rel=&quot;nofollow&quot;<span style="color: #ddbb00;">&amp;gt;</span>Twitter for Android<span style="color: #ddbb00;">&amp;lt;</span>/a<span style="color: #ddbb00;">&amp;gt;</span>
false
&nbsp;
  false
&nbsp;
    144901351
    David Powell
    Ballaholic3223
&nbsp;
    I'm a chill dude that love to play ball, hang out,  and uhhhh chillax...holla at me and we can be cool
http://a1.twimg.com/profile_images/907668616/VDG61D80_normal
&nbsp;
false
    1
9ae4e8
000000
0000ff
e0ff92
87bc44
    0
    Mon May 17 16:17:15 +0000 2010
    0</pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="code"><pre class="xml" style="font-family:monospace;">2010-05-21 14:05:23.073 TweetMapper[72280:207] Recieved Data from Stream: e_background_color<span style="color: #ddbb00;">&amp;gt;</span>
666666
2FC2EF
252429
181A1E
    5
    Fri Aug 01 08:10:36 +0000 2008
    0
    -28800
    Pacific Time (US <span style="color: #ddbb00;">&amp;amp;</span> Canada)
http://s.twimg.com/a/1274144130/images/themes/theme9/bg.gif
false
&nbsp;
    false
    false
&nbsp;
    44
    en
    false</pre></td></tr></table></div>

<p>A response from the Twitter stream comes in as XML. An entire XML element that represents a single is a  element. The status element seen here took two different calls to the didRecieveData method. Because of this we need to build in logic to recognize when a complete Tweet XML object is received and pass that onto our parsing method. This is what we use the NSMutableSting holderString for. You can see the completed method below.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>connection<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSURLConnection</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>connection didReceiveData<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSData</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>data <span style="color: #002200;">&#123;</span>
&nbsp;
	<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>dataString <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> alloc<span style="color: #002200;">&#93;</span> initWithData<span style="color: #002200;">:</span>data encoding<span style="color: #002200;">:</span>NSUTF8StringEncoding<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #002200;">&#91;</span>holderString appendString<span style="color: #002200;">:</span>dataString<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>dataString release<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>holderString rangeOfString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;&quot;</span><span style="color: #002200;">&#93;</span>.location <span style="color: #002200;">!=</span> NSNotFound <span style="color: #002200;">&amp;</span>amp;<span style="color: #002200;">&amp;</span>amp; <span style="color: #002200;">&#91;</span>holderString rangeOfString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;&quot;</span><span style="color: #002200;">&#93;</span>.location <span style="color: #002200;">!=</span> NSNotFound<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
&nbsp;
		<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>holderString rangeOfString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;&quot;</span><span style="color: #002200;">&#93;</span>.location <span style="color: #002200;">&amp;</span>lt; <span style="color: #002200;">&#91;</span>holderString rangeOfString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;&quot;</span><span style="color: #002200;">&#93;</span>.location<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
			<span style="color: #a61390;">NSRange</span> start <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>holderString rangeOfString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;&quot;</span><span style="color: #002200;">&#93;</span>;
			<span style="color: #a61390;">NSRange</span> end <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>holderString rangeOfString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
			<span style="color: #a61390;">NSRange</span> range;
			range.location <span style="color: #002200;">=</span> start.location;
			range.length <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>end.location<span style="color: #002200;">+</span>end.length<span style="color: #002200;">&#41;</span><span style="color: #002200;">-</span>start.location;
&nbsp;
			<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>xmlString <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>holderString substringWithRange<span style="color: #002200;">:</span>range<span style="color: #002200;">&#93;</span>;
&nbsp;
			<span style="color: #002200;">&#91;</span>self parseXMLString<span style="color: #002200;">:</span>xmlString<span style="color: #002200;">&#93;</span>;
&nbsp;
			<span style="color: #002200;">&#91;</span>holderString deleteCharactersInRange<span style="color: #002200;">:</span>range<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#125;</span>
	<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>Here we receive data and append it to our holder string. We then check that both an opening and closing  element tag is seen. If both tags are seen and the opening tag occurs before the closing tag, we create a substring in that range, pass the completed element to a method called parseXMLString and delete the characters from the holder string.</p>
<h2><span style="color: #ff6600;">Parsing Data</span></h2>
<p>Now that we have completed elements being grabbed out of the data we receive we need to parse out what we find as important. For the sake of extensibility I created a helper object called Tweet. A Tweet takes in tweet text, an author name, the authors Twitter URL, a timestamp, an image URL and a coordinate. This is the header for the class</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import</span>
<span style="color: #6e371a;">#import</span>
&nbsp;
<span style="color: #a61390;">@interface</span> Tweet <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span> <span style="color: #002200;">&#123;</span>
&nbsp;
	<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>tweet;
	<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>author;
	<span style="color: #400080;">NSURL</span> <span style="color: #002200;">*</span>authorURL;
	<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>timeStamp;
	<span style="color: #400080;">NSURL</span> <span style="color: #002200;">*</span>authorImageURL;
	CLLocationCoordinate2D coord;
<span style="color: #002200;">&#125;</span>
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, retain<span style="color: #002200;">&#41;</span> <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>tweet;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, retain<span style="color: #002200;">&#41;</span> <span style="color: #400080;">NSURL</span> <span style="color: #002200;">*</span>authorImageURL;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, retain<span style="color: #002200;">&#41;</span> <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>author;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, retain<span style="color: #002200;">&#41;</span> <span style="color: #400080;">NSURL</span> <span style="color: #002200;">*</span>authorURL;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, retain<span style="color: #002200;">&#41;</span> <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>timeStamp;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, assign<span style="color: #002200;">&#41;</span> CLLocationCoordinate2D coord;
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span>UIImage<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>authorPhoto;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSURL</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>tweetLink;
&nbsp;
<span style="color: #a61390;">@end</span></pre></td></tr></table></div>

<p>I implement the class like this. I include a method called TweetLink which will return an NSURL of any url within the tweet text.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &quot;Tweet.h&quot;</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> Tweet
&nbsp;
<span style="color: #a61390;">@synthesize</span> tweet;
<span style="color: #a61390;">@synthesize</span> authorImageURL;
<span style="color: #a61390;">@synthesize</span> author;
<span style="color: #a61390;">@synthesize</span> authorURL;
<span style="color: #a61390;">@synthesize</span> timeStamp;
<span style="color: #a61390;">@synthesize</span> coord;
&nbsp;
<span style="color: #002200;">-</span>init <span style="color: #002200;">&#123;</span>
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>super init<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
&nbsp;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #a61390;">return</span> self;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSURL</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>tweetLink <span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">NSRange</span> httpRange <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self tweet<span style="color: #002200;">&#93;</span> rangeOfString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;http://&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>httpRange.location <span style="color: #002200;">==</span> NSNotFound<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
		<span style="color: #a61390;">return</span> <span style="color: #a61390;">nil</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #a61390;">else</span> <span style="color: #002200;">&#123;</span>
		httpRange.length <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self tweet<span style="color: #002200;">&#93;</span> length<span style="color: #002200;">&#93;</span> <span style="color: #002200;">-</span> httpRange.location;
		<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>customString <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self tweet<span style="color: #002200;">&#93;</span> substringWithRange<span style="color: #002200;">:</span>httpRange<span style="color: #002200;">&#93;</span>;
		httpRange <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>customString rangeOfString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot; &quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>httpRange.location <span style="color: #002200;">==</span> NSNotFound<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
&nbsp;
			httpRange.location <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;
			httpRange.length <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>customString length<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#125;</span>
&nbsp;
		<span style="color: #a61390;">else</span> <span style="color: #002200;">&#123;</span>
			httpRange.length <span style="color: #002200;">=</span> httpRange.location;
			httpRange.location <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;
		<span style="color: #002200;">&#125;</span>
&nbsp;
		NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;URL I am returning: %@&quot;</span>, <span style="color: #002200;">&#91;</span>customString substringWithRange<span style="color: #002200;">:</span>httpRange<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
		<span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSURL</span> URLWithString<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>customString substringWithRange<span style="color: #002200;">:</span>httpRange<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span>UIImage<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>authorPhoto <span style="color: #002200;">&#123;</span>
&nbsp;
	<span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIImage alloc<span style="color: #002200;">&#93;</span> initWithData<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSData</span> dataWithContentsOfURL<span style="color: #002200;">:</span>authorURL<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>description <span style="color: #002200;">&#123;</span>
&nbsp;
	<span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Tweet:%@<span style="color: #2400d9;">\n</span>Author:%@<span style="color: #2400d9;">\n</span>Author URL:%@<span style="color: #2400d9;">\n</span>Published:%@<span style="color: #2400d9;">\n</span>Longitude:%f<span style="color: #2400d9;">\n</span>Latitude:%f<span style="color: #2400d9;">\n</span>Author Image URL:%@&quot;</span>, tweet, author, authorURL, timeStamp,coord.longitude,coord.latitude, authorImageURL<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
<span style="color: #a61390;">@end</span></pre></td></tr></table></div>

<p>I will use this class to create a single object holding all of the data I parse out of the XML elements I am looking at. All that is left to do is finish my parse method to get create these Tweet objects. This is my parse method that takes advantage of Touch XML and its awesome XML parsing methods.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>parseXMLString<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>xmlString <span style="color: #002200;">&#123;</span>
&nbsp;
	CXMLDocument <span style="color: #002200;">*</span>document <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>CXMLDocument alloc<span style="color: #002200;">&#93;</span> initWithXMLString<span style="color: #002200;">:</span>xmlString options<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span> error<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>document rootElement<span style="color: #002200;">&#93;</span> elementsForName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;geo&quot;</span><span style="color: #002200;">&#93;</span> objectAtIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span> childCount<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&amp;</span>gt; <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
&nbsp;
		Tweet <span style="color: #002200;">*</span>tweet <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>Tweet alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #002200;">&#91;</span>tweet setTweet<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>document rootElement<span style="color: #002200;">&#93;</span> elementsForName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;text&quot;</span><span style="color: #002200;">&#93;</span> objectAtIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span> stringValue<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span>tweet setAuthor<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;@%@&quot;</span>, <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>document rootElement<span style="color: #002200;">&#93;</span> elementsForName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;user&quot;</span><span style="color: #002200;">&#93;</span> objectAtIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span> elementsForName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;screen_name&quot;</span><span style="color: #002200;">&#93;</span> objectAtIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span> stringValue<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span>tweet setAuthorURL<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSURL</span> URLWithString<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>document rootElement<span style="color: #002200;">&#93;</span> elementsForName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;user&quot;</span><span style="color: #002200;">&#93;</span> objectAtIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span> elementsForName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;url&quot;</span><span style="color: #002200;">&#93;</span> objectAtIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span> stringValue<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span>tweet setAuthorImageURL<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSURL</span> URLWithString<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>document rootElement<span style="color: #002200;">&#93;</span> elementsForName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;user&quot;</span><span style="color: #002200;">&#93;</span> objectAtIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span> elementsForName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;profile_image_url&quot;</span><span style="color: #002200;">&#93;</span> objectAtIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span> stringValue<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>coordinateString <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>document rootElement<span style="color: #002200;">&#93;</span> elementsForName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;geo&quot;</span><span style="color: #002200;">&#93;</span> objectAtIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span> elementsForName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;point&quot;</span><span style="color: #002200;">&#93;</span> objectAtIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span> stringValue<span style="color: #002200;">&#93;</span>;
		<span style="color: #a61390;">NSRange</span> range <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>coordinateString rangeOfString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot; &quot;</span><span style="color: #002200;">&#93;</span>;
		<span style="color: #a61390;">NSRange</span> lon;
		lon.location <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;
		lon.length <span style="color: #002200;">=</span> range.location;
		<span style="color: #a61390;">NSRange</span> lat;
		lat.location <span style="color: #002200;">=</span> range.location<span style="color: #002200;">+</span>range.length;
		lat.length <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>coordinateString length<span style="color: #002200;">&#93;</span> <span style="color: #002200;">-</span> lat.location;
&nbsp;
		<span style="color: #a61390;">double</span> longit <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>coordinateString substringWithRange<span style="color: #002200;">:</span>lon<span style="color: #002200;">&#93;</span> doubleValue<span style="color: #002200;">&#93;</span>;
		<span style="color: #a61390;">double</span> latit <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>coordinateString substringWithRange<span style="color: #002200;">:</span>lat<span style="color: #002200;">&#93;</span> doubleValue<span style="color: #002200;">&#93;</span>;
&nbsp;
		CLLocationCoordinate2D coord;
		coord.longitude <span style="color: #002200;">=</span> latit;
		coord.latitude <span style="color: #002200;">=</span> longit;
&nbsp;
		<span style="color: #002200;">&#91;</span>tweet setCoord<span style="color: #002200;">:</span>coord<span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #11740a; font-style: italic;">//PASS TWEET ONTO WHATEVER CLASS WILL USE IT</span>
&nbsp;
		<span style="color: #002200;">&#91;</span>tweet release<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<h2><span style="color: #ff6600;">Usage</span></h2>
<p>Make sure that you install Touch XML and import the MapKit framework when using this class. Customize the terms, users and coordinates for the stream method to fit what you need it for. All that will be required to use the class is the following to create and begin parsing:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;">TwitterClient <span style="color: #002200;">*</span>client <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>TwitterClient alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>client makeMyRequest<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #002200;">&#91;</span>client searchByTerm<span style="color: #002200;">&#93;</span>;
&nbsp;
OR
&nbsp;
<span style="color: #002200;">&#91;</span>client searchByLocation<span style="color: #002200;">&#93;</span>;
&nbsp;
OR
&nbsp;
<span style="color: #002200;">&#91;</span>client searchByUser<span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

<p>Fillin the very end of the parsing method to send the tweets off to whatever part of the application you want to use them in. I hope this introduction will help you guys add live twitter streams into your apps. Happy coding!</p>
<h2><span style="color: #ff6600;">Source Downloads</span></h2>
<p><a href="/wp-content/uploads/2010/05/TwitterStream21.zip">TwitterStream</a></p>
]]></content:encoded>
			<wfw:commentRss>http://icodeblog.com/2010/05/21/diving-into-the-twitter-stream/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Follow Us on Twitter!</title>
		<link>http://icodeblog.com/2009/01/30/follow-me-on-twitter/</link>
		<comments>http://icodeblog.com/2009/01/30/follow-me-on-twitter/#comments</comments>
		<pubDate>Sat, 31 Jan 2009 00:58:08 +0000</pubDate>
		<dc:creator><![CDATA[brandontreb]]></dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[twitter]]></category>

		<guid isPermaLink="false">http://icodeblog.com/?p=612</guid>
		<description><![CDATA[<a href="http://twitter.com/rightsprite">http://twitter.com/rightsprite</a>
Stay tuned for the next game programming tutorial.  Should be up soon&#8230;
Happy iCoding.
]]></description>
				<content:encoded><![CDATA[<p><a href="http://twitter.com/rightsprite">http://twitter.com/rightsprite</a></p>
<p>Stay tuned for the next game programming tutorial.  Should be up soon&#8230;</p>
<p>Happy iCoding.</p>
]]></content:encoded>
			<wfw:commentRss>http://icodeblog.com/2009/01/30/follow-me-on-twitter/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>iCodeBlog Is Now On twitter</title>
		<link>http://icodeblog.com/2008/09/19/icodeblog-is-now-on-twitter/</link>
		<comments>http://icodeblog.com/2008/09/19/icodeblog-is-now-on-twitter/#comments</comments>
		<pubDate>Fri, 19 Sep 2008 16:32:40 +0000</pubDate>
		<dc:creator><![CDATA[brandontreb]]></dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[icodeblog]]></category>
		<category><![CDATA[subscribe]]></category>
		<category><![CDATA[twitter]]></category>

		<guid isPermaLink="false">http://icodeblog.com/?p=412</guid>
		<description><![CDATA[
<a href="http://twitter.com/icodeblog"></a>
For those of you <a href="http://twitter.pbwiki.com/">Twitter Fans</a> out there, you can now subscribe to iCodeBlog.com via <a href="http://twitter.com/icodeblog">twitter</a>. Every time a new post is added or updated, my blog will automatically post a tweet to <a href="http://twitter.com/icodeblog">twitter</a>.  I subscribe to a few blogs via twitter and feel this is a valuable tool for sharing content.
So if you prefer twitter to <a href="http://feeds.feedburner.com/icodeblog">RSS</a>, <a href="http://twitter.com/icodeblog">subscribe now</a>!
]]></description>
				<content:encoded><![CDATA[<p><img class="alignnone size-full wp-image-419" style="display:none;" title="tbird" src="/wp-content/uploads/2008/09/tbird1.png" alt="" width="128" height="128" /></p>
<p><a href="http://twitter.com/icodeblog"><img class="size-full wp-image-413 aligncenter" title="follow us on twitter" src="/wp-content/uploads/2008/09/twitter1.jpg" alt="" width="500" height="184" /></a></p>
<p>For those of you <a href="http://twitter.pbwiki.com/">Twitter Fans</a> out there, you can now subscribe to iCodeBlog.com via <a href="http://twitter.com/icodeblog">twitter</a>. Every time a new post is added or updated, my blog will automatically post a tweet to <a href="http://twitter.com/icodeblog">twitter</a>.  I subscribe to a few blogs via twitter and feel this is a valuable tool for sharing content.</p>
<p>So if you prefer twitter to <a href="http://feeds.feedburner.com/icodeblog">RSS</a>, <a href="http://twitter.com/icodeblog">subscribe now</a>!</p>
]]></content:encoded>
			<wfw:commentRss>http://icodeblog.com/2008/09/19/icodeblog-is-now-on-twitter/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
