<?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; Advanced</title>
	<atom:link href="/tag/advanced/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>Adding an OpenGL ES view to your project using NinevehGL</title>
		<link>http://icodeblog.com/2012/09/07/3856/</link>
		<comments>http://icodeblog.com/2012/09/07/3856/#comments</comments>
		<pubDate>Fri, 07 Sep 2012 09:00:34 +0000</pubDate>
		<dc:creator><![CDATA[ebennett]]></dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[3d]]></category>
		<category><![CDATA[Advanced]]></category>
		<category><![CDATA[gl]]></category>
		<category><![CDATA[iOS]]></category>
		<category><![CDATA[opengl es]]></category>

		<guid isPermaLink="false">http://icodeblog.com/?p=3856</guid>
		<description><![CDATA[Recently I came across an openGL ES 2.0 engine that made setup, displaying and animating of 3D objects a breeze, called <a title="NinevehGL" href="http://nineveh.gl" target="_blank">NinevehGL</a>.  This 3D engine has many great features including a full multithreading environment, to keep the main run loop free during object rendering,  motion tweening, object groupings, custom lighting, materials and textures, custom shader support,  and native support for augmented reality, just to name a few. Another major benefit is the ability to import both wavefront  ...]]></description>
				<content:encoded><![CDATA[<p>Recently I came across an openGL ES 2.0 engine that made setup, displaying and animating of 3D objects a breeze, called <a title="NinevehGL" href="http://nineveh.gl" target="_blank">NinevehGL</a>.  This 3D engine has many great features including a full multithreading environment, to keep the main run loop free during object rendering,  motion tweening, object groupings, custom lighting, materials and textures, custom shader support,  and native support for augmented reality, just to name a few. Another major benefit is the ability to import both wavefront (.obj) and Collada (.dae) files. This framework is very easy to use, powerful,  and can be utilized in many different ways.</p>
<p><iframe src="http://player.vimeo.com/video/48964690" height="758" width="500" allowfullscreen="" frameborder="0"></iframe></p>
<p><a href="http://vimeo.com/48964690">IOS openGL rotating Earth using NinevehGL</a> from <a href="http://vimeo.com/user13362310">Erick Bennett</a> on <a href="http://vimeo.com">Vimeo</a>.</p>
<p>In this tutorial I am going to show you how to easily, and effectively add an openGL ES 2.0 3D view into your project, and display a fully rendered, rotating 3D earth. The model assets are available <a title="openGLView assets" href="/wp-content/uploads/2012/09/openGLViewAssets.zip">here</a>, and include the object and texture. These assets are also available in the <a title="openGLview Project file." href="/wp-content/uploads/2012/09/openGLview.zip">project ZIP file</a>, available thru the link at the end of this tutorial.</p>
<p>Download and install the <a title="NinevehGL" href="http://nineveh.gl" target="_blank">NinevehGL framework</a>.</p>
<p>Since we want to add this view into an existing project and understand how the engine functions, lets create a new single-view project to start. Although <a title="NinevehGL" href="http://nineveh.gl" target="_blank">NinevehGL</a> can just as easily be setup thru interface builder, we will be adding this view programmatically. For this exercise, it does not matter if you select storyboard or XIB for the new project.</p>
<p>To begin with there are a few frameworks we will need to add. Do this by first selecting your projects main file in the project navigator pane. Make sure your projects target is selected, and under Build Phases click &#8216;Link Binaries with Libraries&#8217;. Use the + at the bottom of this display and add the <strong>QuartsCore </strong> and <strong>OpenGLES</strong> frameworks.</p>
<p><a href="/2012/09/07/3856/addframework/" rel="attachment wp-att-4107"><img class="aligncenter size-medium wp-image-4107" alt="" src="/wp-content/uploads/2012/09/addFramework-300x135.png" width="300" height="135" /></a></p>
<p>Next, using the &#8216;Add Other&#8217; button, navigate to your downloaded NinevehGL folder and import the <strong>NinevehGL.framework</strong>.</p>
<p><a href="/2012/09/07/3856/addother/" rel="attachment wp-att-4106"><img class="aligncenter size-medium wp-image-4106" alt="" src="/wp-content/uploads/2012/09/addOther-261x300.png" width="261" height="300" /></a></p>
<p>Now onto the code&#8230;&#8230;.import NinevehGL into your header (.h) file,</p>
<div class="wp_syntax">
<pre><span style="color: #993300;">#import</span> &lt;NinevehGL/NinevehGL.h&gt;</pre>
</div>
<p>add the delegate to your @interface, if this is missing your drawview method will never get called.</p>
<div class="wp_syntax">
<pre>@interface ViewController : UIViewController &lt;<span style="color: #333399;">NGLViewDelegate</span>&gt;;</pre>
</div>
<p>In order to see what is being rendered we will need an ivar for an NGLCamera, and for our single object, an NGLMesh. The complete code for all of this should look as follows.</p>
<div class="wp_syntax">
<pre><span style="color: #993300;">#import</span> &lt;UIKit/UIKit.h&gt;
<span style="color: #993300;">#import</span> &lt;NinevehGL/NinevehGL.h&gt; 

@interface ViewController : UIViewController &lt;<span style="color: #333399;">NGLViewDelegate</span>&gt; 
{ 
    <span style="color: #333399;">NGLCamera</span> *_camera; 
    <span style="color: #333399;">NGLMesh</span> *_mesh; 
}</pre>
</div>
<p>In your source (.m) file you must initialize your NGLView, set its delgate to self, define a dictionary with a few basic settings, and initialize the mesh and camera. I am doing this in my viewDidLoad method but depending on your needs, this could be done elsewhere.</p>
<div class="wp_syntax">
<pre>    
- (void)viewDidLoad
{
    [super viewDidLoad];
    <span style="color: #008000;">// Do any additional setup after loading the view, typically from a nib.</span>

    <span style="color: #008000;">// Setup our NGLView.</span>
    <span style="color: #333399;">NGLView</span> *theView = [[<span style="color: #333399;">NGLView</span> alloc] initWithFrame:CGRectMake(40, 40, 240, 360)];

    <span style="color: #008000;">// Set its delegate to self so the drawview method is used.</span>
    theView.delegate = self;

    <span style="color: #008000;">// Add the new view on top of our existing view.</span> 
    [self.view addSubview:theView];

    <span style="color: #008000;">// The content scale factor is used to increase the resolution to take advantage </span></pre>
<pre><span style="color: #008000;"> of retina displays and values range from 1.0 to 2.0. This sets the contentscalefactor </span></pre>
<pre><span style="color: #008000;"> to the max screen scale of the device.</span>
    theView.contentScaleFactor = [[UIScreen mainScreen] scale];

    <span style="color: #008000;">// Setup some initial environment states to be applied to our mesh.</span>
    NSDictionary *settings = [NSDictionary dictionaryWithObjectsAndKeys:
	<span style="color: #993300;">kNGLMeshCentralizeYes</span>, <span style="color: #993300;">kNGLMeshKeyCentralize</span>,
	<span style="color: #cc0000;">@"0.3"</span>, <span style="color: #993300;">kNGLMeshKeyNormalize</span>, nil];

    <span style="color: #008000;">// Initialize the mesh which includes the model file to import (.obj or .dae).</span>
    _mesh = [[<span style="color: #333399;">NGLMesh</span> alloc] initWithFile:<span style="color: #cc0000;">@"earth.obj"</span> settings:settings delegate:nil];

    <span style="color: #008000;">// Initialize the camera used to render the scene.</span>
    _camera = [[<span style="color: #333399;">NGLCamera</span> alloc] initWithMeshes:_mesh, nil];

    <span style="color: #008000;">// If a trasparent background of the NGLView is needed</span>
    <span style="color: #008000;">//theView.backgroundColor = [UIColor clearColor];</span>
    <span style="color: #008000;">//nglGlobalColorFormat(NGLColorFormatRGBA);</span>
    <span style="color: #008000;">//nglGlobalFlush();</span>

    <span style="color: #008000;">// The following command displays a 3d view status monitor that will show your current frame rate and number of polygons being rendered in the view.</span>
    [[<span style="color: #333399;">NGLDebug</span> debugMonitor] startWithView:(<span style="color: #333399;">NGLView</span> *)self.view];
}</pre>
</div>
<p>You will need to add at least one method to your source (.m) file, this is the drawView loop. This drawView loop is used to generate each frame being displayed (up to 60 per second). This drawloop is where you would affect the mesh to animate movement or create the effects your a looking to display. The  [_camera drawCamera] is needed to commit the changes and render the frame. The method usually ends with this command.</p>
<div class="wp_syntax">
<pre>-(void)drawView
{
    <span style="color: #008000;">// Rotate on Y axis</span>
    _mesh.rotateY += .5;

    <span style="color: #008000;">// Draw to our camera to display on our view</span>
    [_camera drawCamera];
}</pre>
</div>
<p>In the above example, the _mesh.rotateY += .5 is used to rotate the mesh .5 degrees each time the frame is rendered.</p>
<p>Now build and run the project and you should see a slowly spinning planet Earth. The NGLView was purposely drawn smaller than the background view in order to show how you could overlay this NGLView over any other normal view to add 3D content. If you want the background of the NGLView to be transparent, set the background color of the view to clear and add the following additional commands. There is also a status monitor that can be run to display frame rates and the number of polygons being rendered for optimization.</p>
<p><a href="/2012/09/07/3856/openglviewcapture/" rel="attachment wp-att-4180"><img class="alignleft size-medium wp-image-4180" alt="" src="/wp-content/uploads/2012/09/openGLviewCapture-162x300.png" width="162" height="300" /></a></p>
<div class="wp_syntax">
<pre><span style="color: #008000;">// If a trasparent background of the NGLView is needed</span>
 theView.backgroundColor = [UIColor clearColor];
 nglGlobalColorFormat(<span style="color: #333399;">NGLColorFormatRGBA</span>);
 nglGlobalFlush();

<span style="color: #008000;">// The following command displays a 3d view status monitor that will show your current frame rate and number of polygons being rendered in the view.</span>
 [[<span style="color: #333399;">NGLDebug</span> debugMonitor] startWithView:(<span style="color: #333399;">NGLView</span> *)self.view];</pre>
</div>
<p>There are many commands you can use to affect your object, it&#8217;s movement and how it interacts in it&#8217;s 3D environment, here are just a few basic ones. Advanced animation, movement techniques, object hit testing, custom shaders, etc. would be worthy of a separate tutorial. Enjoy.</p>
<div class="wp_syntax">
<pre><span style="color: #008000;">// rotation</span>
 _mesh.rotateX, rotateY, rotateZ

<span style="color: #008000;">// scaling</span>
 _mesh.scaleX, scaleY, scaleZ 

<span style="color: #008000;">// placement</span>
 _mesh.x,y,z 

<span style="color: #008000;">// offset center of objects rotation</span>
 _mesh.pivot = nglVec3Make(x, y, z)</pre>
</div>
<p><a title="openGLView assets" href="/wp-content/uploads/2012/09/openGLViewAssets.zip">Assets</a></p>
<p><a title="openGLview project file." href="/wp-content/uploads/2012/09/openGLview.zip">Project file</a></p>
]]></content:encoded>
			<wfw:commentRss>http://icodeblog.com/2012/09/07/3856/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Using Method-Swizzling to help with Test Driven Development</title>
		<link>http://icodeblog.com/2012/08/08/using-method-swizzling-to-help-with-test-driven-development/</link>
		<comments>http://icodeblog.com/2012/08/08/using-method-swizzling-to-help-with-test-driven-development/#comments</comments>
		<pubDate>Wed, 08 Aug 2012 08:00:22 +0000</pubDate>
		<dc:creator><![CDATA[dpettigrew]]></dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Advanced]]></category>
		<category><![CDATA[hamcrest]]></category>
		<category><![CDATA[iOS]]></category>
		<category><![CDATA[mock]]></category>
		<category><![CDATA[mocking]]></category>
		<category><![CDATA[objective-c]]></category>
		<category><![CDATA[ocunit]]></category>
		<category><![CDATA[runtime]]></category>
		<category><![CDATA[swizzling]]></category>
		<category><![CDATA[tdd]]></category>
		<category><![CDATA[test driven development]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://icodeblog.com/?p=3741</guid>
		<description><![CDATA[Introduction
In this blog post I will demonstrate how to mock HTTP requests using an Objective-C runtime dynamic method replacement technique known as method swizzling. I will show how this can be used in tandem with some unit test technologies to help with development of iOS web-service client side code.
In this example, the actual work of the HTTP request is embodied in my AsyncURLConnection class. It uses NSURLConnection to perform an NSURLRequest in an asynchronous manner, using completion handler blocks to return  ...]]></description>
				<content:encoded><![CDATA[<h2>Introduction</h2>
<p>In this blog post I will demonstrate how to mock HTTP requests using an Objective-C runtime dynamic method replacement technique known as <em>method swizzling</em>. I will show how this can be used in tandem with some unit test technologies to help with development of iOS web-service client side code.</p>
<p>In this example, the actual work of the HTTP request is embodied in my AsyncURLConnection class. It uses NSURLConnection to perform an NSURLRequest in an asynchronous manner, using completion handler blocks to return the response to the caller. I could have chosen to mock the native APIs but mocking at the AsyncURLConnection level provides the desired effect more easily.</p>
<h2>Method Swizzling Helper Class</h2>
<p>This is a generic helper class for method swizzling (swapping). This gets used in tests as seen below. The</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;">swizzleClassMethod<span style="color: #002200;">:</span>selector<span style="color: #002200;">:</span>swizzleClass<span style="color: #002200;">:</span></pre></td></tr></table></div>

<p>method takes a class and a method selector along with a class with an alternative implementation of the method given by the selector. The class swaps the implementation of the existing class method with the corresponding method in the swizzleClass.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &amp;lt;Foundation/Foundation.h&amp;gt;</span>
<span style="color: #6e371a;">#import &amp;lt;objc/runtime.h&amp;gt;</span>
&nbsp;
<span style="color: #a61390;">@interface</span> Swizzler <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</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>swizzleClassMethod<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">Class</span><span style="color: #002200;">&#41;</span>target_class selector<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">SEL</span><span style="color: #002200;">&#41;</span>selector swizzleClass<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">Class</span><span style="color: #002200;">&#41;</span>swizzleClass;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>deswizzle;
&nbsp;
<span style="color: #a61390;">@end</span>
&nbsp;
<span style="color: #6e371a;">#import &quot;Swizzler.h&quot;</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> Swizzler
&nbsp;
Method originalMethod <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
Method swizzleMethod <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">/**
Swaps the implementation of an existing class method with the corresponding method in the swizzleClass.
 */</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>swizzleClassMethod<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">Class</span><span style="color: #002200;">&#41;</span>targetClass selector<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">SEL</span><span style="color: #002200;">&#41;</span>selector swizzleClass<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">Class</span><span style="color: #002200;">&#41;</span>swizzleClass
<span style="color: #002200;">&#123;</span>
	originalMethod <span style="color: #002200;">=</span> class_getClassMethod<span style="color: #002200;">&#40;</span>targetClass, selector<span style="color: #002200;">&#41;</span>; <span style="color: #11740a; font-style: italic;">// save the oringinal implementation so it can be restored</span>
	swizzleMethod <span style="color: #002200;">=</span> class_getClassMethod<span style="color: #002200;">&#40;</span>swizzleClass, selector<span style="color: #002200;">&#41;</span>; <span style="color: #11740a; font-style: italic;">// save the replacement method</span>
	method_exchangeImplementations<span style="color: #002200;">&#40;</span>originalMethod, swizzleMethod<span style="color: #002200;">&#41;</span>; <span style="color: #11740a; font-style: italic;">// perform the swap, replacing the original with the swizzle method implementation.</span>
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">/**
 Restores the implementation of an existing class method with its original implementation.
 */</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>deswizzle
<span style="color: #002200;">&#123;</span>
	method_exchangeImplementations<span style="color: #002200;">&#40;</span>swizzleMethod, originalMethod<span style="color: #002200;">&#41;</span>; <span style="color: #11740a; font-style: italic;">// perform the swap, replacing the previously swizzled method with the original implementation.</span>
	swizzleMethod <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
	originalMethod <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></td></tr></table></div>

<h2>Class to be mocked</h2>
<p>This is the interface of a class that connects and loads a URL. I will be replacing the implementation so its details are left out of here for brevity.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &amp;lt;Foundation/Foundation.h&amp;gt;</span>
&nbsp;
<span style="color: #a61390;">typedef</span> <span style="color: #a61390;">void</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">^</span>completeBlock_t<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSData</span> <span style="color: #002200;">*</span>data<span style="color: #002200;">&#41;</span>;
<span style="color: #a61390;">typedef</span> <span style="color: #a61390;">void</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">^</span>errorBlock_t<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSError</span> <span style="color: #002200;">*</span>error<span style="color: #002200;">&#41;</span>;
&nbsp;
<span style="color: #a61390;">@interface</span> AsyncURLConnection <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span>
<span style="color: #002200;">&#123;</span>
    <span style="color: #400080;">NSMutableData</span> <span style="color: #002200;">*</span>data;
    completeBlock_t completeBlock;
    errorBlock_t errorBlock;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">/**
 Asynchronously performs an HTTP GET - invokes one of the blocks depending on the response to the request 
*/</span>
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>request<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>requestUrl completeBlock<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>completeBlock_t<span style="color: #002200;">&#41;</span>aCompleteBlock errorBlock<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>errorBlock_t<span style="color: #002200;">&#41;</span>anErrorBlock;
&nbsp;
<span style="color: #11740a; font-style: italic;">/**
 Initializes and asynchronously performs an HTTP GET - invokes one of the blocks depending on the response to the request
 */</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>initWithRequest<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>requestUrl completeBlock<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>completeBlock_t<span style="color: #002200;">&#41;</span>aCompleteBlock errorBlock<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>errorBlock_t<span style="color: #002200;">&#41;</span>anErrorBlock;
&nbsp;
<span style="color: #11740a; font-style: italic;">// If both HTTP Basic authentication optional parameters are non-empty strings, request will encode the URL with them.</span>
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, copy<span style="color: #002200;">&#41;</span> <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>authUserName; <span style="color: #11740a; font-style: italic;">// HTTP Basic authentication optional parameter</span>
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, copy<span style="color: #002200;">&#41;</span> <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>authPassword; <span style="color: #11740a; font-style: italic;">// HTTP Basic authentication optional parameter</span>
&nbsp;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, copy<span style="color: #002200;">&#41;</span> <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>accept; <span style="color: #11740a; font-style: italic;">// Optional parameter to set the response type accepted (default is @&quot;application/json&quot;)</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></td></tr></table></div>

<h2>Example of Usage in a Test</h2>
<p>In this test I am testing that I am correctly processing the JSON data returned from the web service. However, I want to isolate the actual web service from the test. I want a test to validate the creation of the NSDictionary response object from the JSON.<br />
As an aside, by removing the Swizzler code, this test could be run against the actual web service; but that is not the intent here.<br />
Here is what the JSON response looks like &#8211;</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#123;</span>
    <span style="color: #3366CC;">&quot;coaches&quot;</span> <span style="color: #339933;">:</span>
	<span style="color: #009900;">&#91;</span>
     <span style="color: #009900;">&#123;</span>
     <span style="color: #3366CC;">&quot;id&quot;</span> <span style="color: #339933;">:</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">,</span>
     <span style="color: #3366CC;">&quot;name&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Jeff Tucker&quot;</span><span style="color: #339933;">,</span>
     <span style="color: #3366CC;">&quot;thumbnail_url&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;http://img.breakingmuscle.com/sites/default/files/imagecache/full_portrait/images/author/64337_10150578149548471_1189630488_n.jpg&quot;</span>
     <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
     <span style="color: #009900;">&#123;</span>
     <span style="color: #3366CC;">&quot;id&quot;</span> <span style="color: #339933;">:</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">,</span>
     <span style="color: #3366CC;">&quot;name&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Traver H. Boehm&quot;</span><span style="color: #339933;">,</span>
     <span style="color: #3366CC;">&quot;thumbnail_url&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;http://img.breakingmuscle.com/sites/default/files/imagecache/full_portrait/images/author/traver.jpg&quot;</span>
     <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
     <span style="color: #009900;">&#123;</span>
     <span style="color: #3366CC;">&quot;id&quot;</span> <span style="color: #339933;">:</span><span style="color: #CC0000;">3</span><span style="color: #339933;">,</span>
     <span style="color: #3366CC;">&quot;name&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Jeff Tucker&quot;</span><span style="color: #339933;">,</span>
     <span style="color: #3366CC;">&quot;thumbnail_url&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;http://img.breakingmuscle.com/sites/default/files/imagecache/full_portrait/images/author/64337_10150578149548471_1189630488_n.jpg&quot;</span>
     <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
     <span style="color: #009900;">&#123;</span>
     <span style="color: #3366CC;">&quot;id&quot;</span> <span style="color: #339933;">:</span> <span style="color: #CC0000;">4</span><span style="color: #339933;">,</span>
     <span style="color: #3366CC;">&quot;name&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Traver H. Boehm&quot;</span><span style="color: #339933;">,</span>
     <span style="color: #3366CC;">&quot;thumbnail_url&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;http://img.breakingmuscle.com/sites/default/files/imagecache/full_portrait/images/author/traver.jpg&quot;</span>
     <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
     <span style="color: #009900;">&#123;</span>
     <span style="color: #3366CC;">&quot;id&quot;</span> <span style="color: #339933;">:</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">,</span>
     <span style="color: #3366CC;">&quot;name&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Jeff Tucker&quot;</span><span style="color: #339933;">,</span>
     <span style="color: #3366CC;">&quot;thumbnail_url&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;http://img.breakingmuscle.com/sites/default/files/imagecache/full_portrait/images/author/64337_10150578149548471_1189630488_n.jpg&quot;</span>
     <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
     <span style="color: #009900;">&#123;</span>
     <span style="color: #3366CC;">&quot;id&quot;</span> <span style="color: #339933;">:</span> <span style="color: #CC0000;">6</span><span style="color: #339933;">,</span>
     <span style="color: #3366CC;">&quot;name&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Traver H. Boehm&quot;</span><span style="color: #339933;">,</span>
     <span style="color: #3366CC;">&quot;thumbnail_url&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;http://img.breakingmuscle.com/sites/default/files/imagecache/full_portrait/images/author/traver.jpg&quot;</span>
     <span style="color: #009900;">&#125;</span>
     <span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">&quot;per_page&quot;</span><span style="color: #339933;">:</span> <span style="color: #CC0000;">6</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">&quot;page&quot;</span><span style="color: #339933;">:</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">&quot;total&quot;</span> <span style="color: #339933;">:</span> <span style="color: #CC0000;">11</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">&quot;total_pages&quot;</span><span style="color: #339933;">:</span> <span style="color: #CC0000;">2</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>The method under test here is:-</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>loadCollectionFromEndpoint<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>endpoint successBlock<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>JSONLoaderLoadSuccessBlock<span style="color: #002200;">&#41;</span>successBlock failBlock<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>JSONLoaderLoadFailBlock<span style="color: #002200;">&#41;</span>failBlock</pre></td></tr></table></div>

<p>This method calls the AsyncURLConnection class method</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;">id</span><span style="color: #002200;">&#41;</span>request<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>requestUrl completeBlock<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>completeBlock_t<span style="color: #002200;">&#41;</span>completeBlock errorBlock<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>errorBlock_t<span style="color: #002200;">&#41;</span>errorBlock;</pre></td></tr></table></div>

<p>I will replace the existing implementation of that method with one that does not actually make an HTTP request, but instead gets JSON from a local file.</p>
<p>The unit test uses <a href="https://github.com/hamcrest/OCHamcrest">OCHamcrest</a> matchers and XCode&#8217;s built in <a href="http://www.sente.ch/software/ocunit/">OCUnit</a>.</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>test_loadPopularCoaches_success
<span style="color: #002200;">&#123;</span>
    <span style="color: #11740a; font-style: italic;">// Test set-up</span>
    Swizzler <span style="color: #002200;">*</span>swizzler <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>Swizzler new<span style="color: #002200;">&#93;</span>;
    <span style="color: #11740a; font-style: italic;">// Replace the actual AsyncURLConnection method with our own test one.</span>
    <span style="color: #002200;">&#91;</span>swizzler swizzleClassMethod<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>AsyncURLConnection class<span style="color: #002200;">&#93;</span> selector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>request<span style="color: #002200;">:</span>completeBlock<span style="color: #002200;">:</span>errorBlock<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span> swizzleClass<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>RequestPopularCoachesPassMock class<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
    __block <span style="color: #a61390;">BOOL</span> hasCalledBack <span style="color: #002200;">=</span> <span style="color: #a61390;">NO</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Test</span>
    <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>endpoint <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;%@?per_page=6&amp;amp;page=1&amp;amp;popular=true&quot;</span>, kCoachesEndpoint<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>JSONLoader sharedJSONLoader<span style="color: #002200;">&#93;</span> loadCollectionFromEndpoint<span style="color: #002200;">:</span>endpoint successBlock<span style="color: #002200;">:^</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span>collection<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>coaches <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>collection valueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;coaches&quot;</span><span style="color: #002200;">&#93;</span>;
        assertThat<span style="color: #002200;">&#40;</span>coaches, hasCountOf<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">6</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>; 
        <span style="color: #a61390;">int</span> i <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;
        <span style="color: #a61390;">for</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span>coach <span style="color: #a61390;">in</span> coaches<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
            STAssertTrue<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>coach isKindOfClass<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDictionary</span> class<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Incorrect class&quot;</span><span style="color: #002200;">&#41;</span>;
            assertThat<span style="color: #002200;">&#40;</span>coach, equalTo<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>popularCoaches objectAtIndex<span style="color: #002200;">:</span>i<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>; <span style="color: #11740a; font-style: italic;">// popularCoaches defined elsewhere with expected data</span>
            <span style="color: #002200;">++</span>i;
        <span style="color: #002200;">&#125;</span>
        assertThat<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>collection valueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;per_page&quot;</span><span style="color: #002200;">&#93;</span>, equalToInt<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">6</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
        assertThat<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>collection valueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;page&quot;</span><span style="color: #002200;">&#93;</span>, equalToInt<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">1</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
        assertThat<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>collection valueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;total&quot;</span><span style="color: #002200;">&#93;</span>, equalToInt<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">11</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
        assertThat<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>collection valueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;total_pages&quot;</span><span style="color: #002200;">&#93;</span>, equalToInt<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">2</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
        hasCalledBack <span style="color: #002200;">=</span> <span style="color: #a61390;">YES</span>;
    <span style="color: #002200;">&#125;</span> failBlock<span style="color: #002200;">:^</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSError</span> <span style="color: #002200;">*</span>error<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;%@&quot;</span>, <span style="color: #002200;">&#91;</span>error localizedDescription<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
        STFail<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Unexpected error returned&quot;</span><span style="color: #002200;">&#41;</span>;
        hasCalledBack <span style="color: #002200;">=</span> <span style="color: #a61390;">YES</span>;
    <span style="color: #002200;">&#125;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// see http://drewsmitscode.posterous.com/testing-asynchronous-code-in-objective-c</span>
    <span style="color: #400080;">NSDate</span> <span style="color: #002200;">*</span>loopUntil <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDate</span> dateWithTimeIntervalSinceNow<span style="color: #002200;">:</span><span style="color: #2400d9;">1</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">while</span> <span style="color: #002200;">&#40;</span>hasCalledBack <span style="color: #002200;">==</span> <span style="color: #a61390;">NO</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSRunLoop</span> currentRunLoop<span style="color: #002200;">&#93;</span> runMode<span style="color: #002200;">:</span>NSDefaultRunLoopMode beforeDate<span style="color: #002200;">:</span>loopUntil<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
    swizzler <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>You can use any class to provide an implementation of the replacement method. This approach allows you can return various responses from your HTTP request. Here is one that returns the expected payload that is read from a file.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> RequestPopularCoachesPassMock <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span>
&nbsp;
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>request<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>requestUrl completeBlock<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>completeBlock_t<span style="color: #002200;">&#41;</span>completeBlock errorBlock<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>errorBlock_t<span style="color: #002200;">&#41;</span>errorBlock;
&nbsp;
<span style="color: #a61390;">@end</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> RequestPopularCoachesPassMock
&nbsp;
<span style="color: #11740a; font-style: italic;">// Swizzled method</span>
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>request<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>requestUrl completeBlock<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>completeBlock_t<span style="color: #002200;">&#41;</span>completeBlock errorBlock<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>errorBlock_t<span style="color: #002200;">&#41;</span>errorBlock
<span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>completeBlock<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #400080;">NSData</span> <span style="color: #002200;">*</span>data <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>FileReader dataWithContentsOfBundleFile<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;popularCoaches.json&quot;</span><span style="color: #002200;">&#93;</span>;
        completeBlock<span style="color: #002200;">&#40;</span>data<span style="color: #002200;">&#41;</span>;        
    <span style="color: #002200;">&#125;</span>
    <span style="color: #a61390;">return</span> self;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></td></tr></table></div>

<h2>Conclusion</h2>
<p>In this blog post I have demonstrated how to use the Objective-C runtime dynamic method replacement technique known as *method swizzling* to assist with unit testing in a situation where there is a 3rd party web service that I want to mock. This technique can be used during development to assist with integrating of an iOS app with a web service. It helps speed up development and prototyping. It can also be applied during development in the normal running of your app if you want to mock out portions of or all of a web service.</p>
]]></content:encoded>
			<wfw:commentRss>http://icodeblog.com/2012/08/08/using-method-swizzling-to-help-with-test-driven-development/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Update #2: ELCImagePickerController</title>
		<link>http://icodeblog.com/2011/10/18/update-elcimagepickercontroller-2/</link>
		<comments>http://icodeblog.com/2011/10/18/update-elcimagepickercontroller-2/#comments</comments>
		<pubDate>Tue, 18 Oct 2011 19:10:10 +0000</pubDate>
		<dc:creator><![CDATA[Chris Schepman]]></dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Featured]]></category>
		<category><![CDATA[Advanced]]></category>

		<guid isPermaLink="false">http://icodeblog.com/?p=3485</guid>
		<description><![CDATA[Welcome back to another update to the <a href="https://github.com/elc/ELCImagePickerController" title="ELCImagePickerController">ELCImagePickerController</a>. If you&#8217;re not familiar with this class I&#8217;d suggest you check out the following posts:
<a href="/2010/10/07/cloning-uiimagepickercontroller-using-the-assets-library-framework/" title="Cloning UIImagePickerController using the Assets Library Framework">Cloning UIImagePickerController using the Assets Library Framework</a>
<a href="/2011/03/03/update-elcimagepickercontroller/">Update: ELCImagePickerController</a>
A lot has happened in the iOS world since the creation of this code. With the advent of every new version of iOS there are always exciting new features and bug fixes. Sometimes subtle code changes sneak into classes we&#8217;ve  ...]]></description>
				<content:encoded><![CDATA[<p>Welcome back to another update to the <a href="https://github.com/elc/ELCImagePickerController" title="ELCImagePickerController">ELCImagePickerController</a>. If you&#8217;re not familiar with this class I&#8217;d suggest you check out the following posts:</p>
<p><a href="/2010/10/07/cloning-uiimagepickercontroller-using-the-assets-library-framework/" title="Cloning UIImagePickerController using the Assets Library Framework">Cloning UIImagePickerController using the Assets Library Framework</a><br />
<a href="/2011/03/03/update-elcimagepickercontroller/">Update: ELCImagePickerController</a></p>
<p>A lot has happened in the iOS world since the creation of this code. With the advent of every new version of iOS there are always exciting new features and bug fixes. Sometimes subtle code changes sneak into classes we&#8217;ve been using for a while though. These changes might create bugs that can be hard to find. Whenever I am debugging code on a new release, especially if it&#8217;s a beta release, I try to remember to consult the docs sooner than later. When we started to get reports of ELCImagePickerController not working in iOS 5, it was time to do some homework.</p>
<p>The problem seemed to be that the albums were coming up as empty, even when people had plenty of pictures to display. Someone in the community found a quick work around that if you don&#8217;t release the library object, then the code starts working again! Leaking memory like this is never the right answer, but it was a great starting spot to figure out the real problem.</p>
<p>The problem seemed to be arising here:</p>
<script src="http://gist.github.com/1294115.js"></script>
<p>Commenting out the release fixed the problem? Very odd. I floundered around the code a bit before remembering to go to the documentation for this new (beta, at the time) version of iOS. Lo and behold!</p>
<p><a href="/2011/10/18/update-elcimagepickercontroller-2/screen-shot-2011-10-17-at-10-54-47-am-2/" rel="attachment wp-att-3488"><img class="alignnone size-large wp-image-3488" src="/wp-content/uploads/2011/10/Screen-Shot-2011-10-17-at-10.54.47-AM1-600x94.png" alt="" width="600" height="94" /></a></p>
<p>The last sentence is key, &#8220;The lifetimes of objects you get back from a library instance are tied to the life of the library instance.&#8221; If we release our library instance right there we aren&#8217;t going to have access to any of the objects we get back from it anymore! This is new behavior in iOS 5. To adapt to it I just moved the library variable out to be an instance variable of the class and didn&#8217;t release it until dealloc is called.</p>
<p>Not such a hard fix once I knew what to look for, just have to remember that the documentation is there for a reason! Thanks to the community for the feedback that made tracking down this bug much easier.</p>
<p><a href="/2011/10/18/update-elcimagepickercontroller-2/elc-image-picker-controller-ios5/" rel="attachment wp-att-3580"><img src="/wp-content/uploads/2011/10/elc-image-picker-controller-ios5.png" alt="" width="320" height="480" class="alignnone size-full wp-image-3580" style="float: none" /></a></p>
<p>We&#8217;re back in action!</p>
<p>As I was troubleshooting this control I kept running into the problem that my simulator didn&#8217;t have any pictures in it. If only there was a utility to populate the photo album for me. Maybe it could grab images of cute little kittens from the internet or something? Hmm. Stay tuned to iCodeBlog for more on this!</p>
]]></content:encoded>
			<wfw:commentRss>http://icodeblog.com/2011/10/18/update-elcimagepickercontroller-2/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Grand Central Dispatch And Writing Methods That Accept Blocks As Arguments</title>
		<link>http://icodeblog.com/2011/08/31/grand-central-dispatch-and-writing-methods-that-accept-blocks-as-arguments/</link>
		<comments>http://icodeblog.com/2011/08/31/grand-central-dispatch-and-writing-methods-that-accept-blocks-as-arguments/#comments</comments>
		<pubDate>Wed, 31 Aug 2011 22:10:42 +0000</pubDate>
		<dc:creator><![CDATA[brandontreb]]></dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Advanced]]></category>

		<guid isPermaLink="false">http://icodeblog.com/?p=3385</guid>
		<description><![CDATA[Have you ever looked at the enumerateObjectsUsingBlock method of NSArray and wondered how you might use that design pattern in your own application.  In this tutorial, I will be showing you how to do just this.  We will be writing methods that accept blocks as arguments as well as learning how to declare block ivars and properties.
This tutorial assumes that you have at least a basic understanding of using blocks and Grand Central Dispatch (GCD).  If not,  ...]]></description>
				<content:encoded><![CDATA[<p>Have you ever looked at the enumerateObjectsUsingBlock method of NSArray and wondered how you might use that design pattern in your own application.  In this tutorial, I will be showing you how to do just this.  We will be writing methods that accept blocks as arguments as well as learning how to declare block ivars and properties.</p>
<p>This tutorial assumes that you have at least a <i>basic</i> understanding of using blocks and Grand Central Dispatch (GCD).  If not, @cruffenach has created a more basic intro to blocks tutorial <a href="/2010/07/08/asset-libraries-and-blocks-in-ios-4/">here</a> if you need a refresher on them.  </p>
<h4>When You Might Want To Pass A Block To A Method</h4>
<p>There are quite a few times when you might want to pass a block to a method.  The most common reason is to replace the &#8220;delegate&#8221; design pattern.  Basically, when you want to process some data (usually in another thread) and it needs to call back to the caller when it completes.  This pattern is also useful when you want to have a method &#8220;return&#8221; multiple values.  (Note: return is in quotes, because we are not actually returning multiple values but calling back using the block and passing multiple values).</p>
<p>Another great reason to use blocks in this way is it keeps your code flowing in a much more logical order.  Rather than having this feeling of spaghetti code with callbacks jumping all over the place, all of your callback code can be written inline with everything else.</p>
<p>In the following example, we are going to (asynchronously) fetch a very large array of strings (in plist form) from the web and load it up into a UITableView.  We will pass a simple completion block to our array fetch method which will tell our class to reload the table.</p>
<h4>Creating Our Dictionary Downloader Class</h4>
<p>We&#8217;ll start by creating a new class called PListDownloader.  This will contain a single method called downloadPlistForURL.  Declare it like this in PListDownloader.h.</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> downloadPlistForURL<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> url completionBlock<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">^</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>data, <span style="color: #400080;">NSError</span> <span style="color: #002200;">*</span>error<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span> block;</pre></td></tr></table></div>

<p>Let&#8217;s break this down. First, the method is void (no return).  That&#8217;s because our block will handle all of the data that this method &#8220;returns&#8221;.  The first parameter is simply the URL of the plist file.  In our case, we will be passing in http://icodeblog.com/samples/block_test/block_test.plist .  This is a ~3MB plist file that should take a couple seconds to download and process.</p>
<p>Finally, we pass in our block.  Let&#8217;s break the block down even further.</p>
<p>1. &#8220;(void &#8221; The void denotes that the block doesn&#8217;t return any value.</p>
<p>2. &#8220;(^))&#8221; The ^ without anything next to it denotes that it&#8217;s an anonymous block.  We will go over naming our blocks in a bit.</p>
<p>3. (NSArray *data, NSError *error)) is what will be passed back into the block when it gets called.  This would be equivalent to the &#8220;return values&#8221; in this case.</p>
<p>4. &#8220;block&#8221; This is simply the name of our block parameter passed in.  You could have named it anything really.</p>
<p>It&#8217;s time for the implementation of this method. In the PListDownloader.m file add the following code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><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> downloadPlistForURL<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> url completionBlock<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">^</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>data, <span style="color: #400080;">NSError</span> <span style="color: #002200;">*</span>error<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span> block <span style="color: #002200;">&#123;</span>
    dispatch_async<span style="color: #002200;">&#40;</span>dispatch_get_global_queue<span style="color: #002200;">&#40;</span>DISPATCH_QUEUE_PRIORITY_HIGH, 0ul<span style="color: #002200;">&#41;</span>, <span style="color: #002200;">^</span><span style="color: #002200;">&#123;</span>
        <span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>returnArray <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSArray</span> arrayWithContentsOfURL<span style="color: #002200;">:</span>url<span style="color: #002200;">&#93;</span>;
        <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>returnArray<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
            block<span style="color: #002200;">&#40;</span>returnArray, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span>;
        <span style="color: #002200;">&#125;</span> <span style="color: #a61390;">else</span> <span style="color: #002200;">&#123;</span>
            <span style="color: #400080;">NSError</span> <span style="color: #002200;">*</span>error <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSError</span> errorWithDomain<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;plist_download_error&quot;</span> code<span style="color: #002200;">:</span><span style="color: #2400d9;">1</span> 
                                             userInfo<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDictionary</span> dictionaryWithObject<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Can't fetch data&quot;</span> forKey<span style="color: #002200;">:</span>NSLocalizedDescriptionKey<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
            block<span style="color: #002200;">&#40;</span><span style="color: #a61390;">nil</span>, error<span style="color: #002200;">&#41;</span>;
        <span style="color: #002200;">&#125;</span>
&nbsp;
    <span style="color: #002200;">&#125;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>At first glance, one might argue that this looks &#8220;sloppier&#8221; than the delegate style.  Simply because it uses nested blocks.  I can assure you this is <b>much</b> cleaner AND simpler.  For one, by using GCD you no longer have to worry about creating auto release pools.</p>
<p>So, there&#8217;s quite a bit going on here, let&#8217;s break it down line by line.</p>
<p>2. This is the Grand Central Dispatch way of firing off a code segment inside of a new thread.  Basically we are executing this code on a new thread with a priority of HIGH.  You can use this first parameter to prioritize the execution of multiple threads. For example say you are running 2 concurrent threads (one to download user data, the other to download the avatar). You can assign the user data thread higher priority than the avatar thread.  The system will handle the rest.</p>
<p>**Note: Using Blocks here replaces the delgate method style of performSelectorInBackground</p>
<p>3. This is a very simple line to download plist data from a URL into an NSArray.  There is no error checking or anything fancy.  In production, you would want to use NSURLConnection or a lib built on top of it like <a href="http://allseeing-i.com/ASIHTTPRequest/">ASIHTTPRequest</a>.</p>
<p>4. Very simple error check, basically seeing if there was any data downloaded.</p>
<p>5. This is where the magic happens.  We call this block as a simple function call passing in our returned array and nil for the error (nil since there was no error).</p>
<p>6-11. This segment handles our error case.  If for some reason, no data was downloaded, we create our own NSError object and pass it into the exact same block as in line 5.  The only difference is we pass in nil for the results and our error for the NSError object.</p>
<p>Before you gripe in the comments &#8220;You could have just done that async call in your main class&#8230;blah&#8230;blah&#8230;&#8221;, know that I know this.  However, this is just a trivial example.  Often times you might have a much larger codebase built up that does more than downloads a stupid plist.  Perhaps you have a ton of preprocessing of the url or post-processing of the data.  Then it starts to make less and less sense to put this code in your main implementation file.</p>
<p>Also, you might need to download plist files in various parts of the application and do different things with the data that was returned.</p>
<h4>Calling A Method With A Block</h4>
<p>So, now we have our simple class to download our plist and return the data in a block.  But, how do we use it? </p>
<p>Let me give you a bit of context about my sample application first.  I have a basic &#8220;Navigation Based&#8221; project with a NSArray property called pListData.  I am displaying the contents of pListData in the UITableView.  So, I want to download the plist data from the web, set it to my pListData property and reload my table.</p>
<p>First import the PListDownloader class and then uncomment the viewDidLoad method adding the following code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><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>viewDidLoad <span style="color: #002200;">&#123;</span>
    <span style="color: #002200;">&#91;</span>super viewDidLoad<span style="color: #002200;">&#93;</span>;
    PListDownloader <span style="color: #002200;">*</span>downloader <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>PListDownloader alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
    <span style="color: #400080;">NSURL</span> <span style="color: #002200;">*</span>url <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://icodeblog.com/samples/block_test/block_test.plist&quot;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>downloader downloadPlistForURL<span style="color: #002200;">:</span>url completionBlock<span style="color: #002200;">:^</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>data, <span style="color: #400080;">NSError</span> <span style="color: #002200;">*</span>error<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        self.plistData <span style="color: #002200;">=</span> data;
        <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span>error<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
            dispatch_sync<span style="color: #002200;">&#40;</span>dispatch_get_main_queue<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>, <span style="color: #002200;">^</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
                <span style="color: #002200;">&#91;</span>self.tableView reloadData<span style="color: #002200;">&#93;</span>;
            <span style="color: #002200;">&#125;</span><span style="color: #002200;">&#41;</span>;
        <span style="color: #002200;">&#125;</span> <span style="color: #a61390;">else</span> <span style="color: #002200;">&#123;</span>
            NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;error %@&quot;</span>, error<span style="color: #002200;">&#41;</span>;
        <span style="color: #002200;">&#125;</span>
    <span style="color: #002200;">&#125;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>Again, let&#8217;s take a look at it line by line.</p>
<p>3. This should be familiar, we create a new instance of PListDownloader.  One thing to note here is we are able to set it to autorelease.  It won&#8217;t be released until AFTER the execution finishes and the thread dies.  This is a HUGE improvement over the async delegate model where you would have to keep a property laying around.</p>
<p>4. Simply creating an NSURL object using the string location of our test plist (it&#8217;s still there, you can test with it)</p>
<p>5. This is where we actually call our downloadPListForURL method. The first parameter is the easy one, just the URL you created on line 4.  The second is the the block that the method will call back to when execution finishes. Luckily XCode will auto complete a nice template for us.</p>
<p>6. Once the method calls back and contains an array of strings, we set our property (pListData) to the returned array.  This will allow use to actually do something with the data.</p>
<p>7. Our simple error check</p>
<p>8. It&#8217;s going to get crazy again.  Since we used GCD to run our code in a separate thread, we need a way to come back to the main thread to update the UI. If you are reading this and don&#8217;t know that you do all UI manipulations in the main thread, then I suggest you start following my &#8220;<a href="/2011/07/12/iphone-development-back-to-basics/">Back To Basics</a>&#8221; tutorial series.</p>
<p>9. Now that we are in the context of the main thread, we can simply reload the tableview.</p>
<p>Now, when you run this application, it should take quite some time to fetch that data (3MB).  But, you will have complete control over the UI while it&#8217;s fetch in the background.  You could even throw up a little loading message if you so choose.</p>
<p>For completeness, here is a look at what this application produces.</p>
<p><img src="http://f.cl.ly/items/06181C2F0v442x0o371h/Screen%20Shot%202011-08-31%20at%203.54.53%20PM.png" width="550"></p>
<div style="clear:both">&nbsp;</div>
<p>As this post has gotten a little longer than I anticipated, I&#8217;ll save the part about declaring blocks as properties until next time. If you have any questions or comments, feel free to leave them in the comments of this post (or write them to me on <a href="http://twitter.com/brandontreb">Twitter</a>).</p>
<p>You can download the source for this tutorial <a href="http://cl.ly/3F00290E2j3l0g2E2p0x">here</a>.</p>
<p>Happy iCoding!</p>
]]></content:encoded>
			<wfw:commentRss>http://icodeblog.com/2011/08/31/grand-central-dispatch-and-writing-methods-that-accept-blocks-as-arguments/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Adding block callbacks to the Facebook iOS SDK</title>
		<link>http://icodeblog.com/2011/07/19/adding-block-callbacks-to-the-facebook-ios-sdk/</link>
		<comments>http://icodeblog.com/2011/07/19/adding-block-callbacks-to-the-facebook-ios-sdk/#comments</comments>
		<pubDate>Tue, 19 Jul 2011 16:28:31 +0000</pubDate>
		<dc:creator><![CDATA[ayanok]]></dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Advanced]]></category>

		<guid isPermaLink="false">http://icodeblog.com/?p=3139</guid>
		<description><![CDATA[If you are like me and love using blocks over delegates, then these code snippets will come in handy. After learning about blocks I have come to love to use them, especially using them for callbacks rather than using the old protocol/delegate methodology. These code snippets will allow you to use blocks as callbacks over delegates.
In a previous post <a href="/2011/03/28/facebook-sdk-posting-to-user-news-feed/"> Facebook SDK &#8211; Posting to User News Feed</a> I showed how to post various types of status&#8217;s to the  ...]]></description>
				<content:encoded><![CDATA[<p>If you are like me and love using blocks over delegates, then these code snippets will come in handy. After learning about blocks I have come to love to use them, especially using them for callbacks rather than using the old protocol/delegate methodology. These code snippets will allow you to use blocks as callbacks over delegates.</p>
<p>In a previous post <a href="/2011/03/28/facebook-sdk-posting-to-user-news-feed/"> Facebook SDK &#8211; Posting to User News Feed</a> I showed how to post various types of status&#8217;s to the logged in users news feed. These code snippets have been added to a <a href="https://github.com/ayanok/facebook-ios-sdk">forked version</a> of the official Facebook for iOS SDK. A sample of how to use the new block callbacks can be found within the <a href="https://github.com/ayanok/Andy-Yanok---Public-Sample-Code/tree/master/Facebook%20Demo">sample project</a> from the last post.</p>
<p>For more information on blocks, you can refer to the <a href="http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/Blocks/Articles/00_Introduction.html">Apple document</a> discussing blocks in more detail.</p>
<p>Step 1: Modifying FBRequest Class for new callbacks<br />
First we will modify the FBRequest class. There are a few simple modifications that we need to create. Within the FBRequest.h file add the following:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>copy<span style="color: #002200;">&#41;</span> <span style="color: #a61390;">void</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">^</span>FBRequestCallback<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#40;</span>FBRequest<span style="color: #002200;">*</span>, <span style="color: #a61390;">id</span>, <span style="color: #400080;">NSError</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, assign<span style="color: #002200;">&#41;</span>  <span style="color: #a61390;">BOOL</span> usesBlockCallback;
&nbsp;
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span>FBRequest<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> getRequestWithParams<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSMutableDictionary</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>params
                         httpMethod<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>httpMethod
                           callback<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">^</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#40;</span>FBRequest <span style="color: #002200;">*</span>request, <span style="color: #a61390;">id</span> result, <span style="color: #400080;">NSError</span> <span style="color: #002200;">*</span>error<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span> _block
                         requestURL<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>url;</pre></td></tr></table></div>

<p>In the FBRequest.m file add the following code:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@synthesize</span> FBRequestCallback <span style="color: #002200;">=</span> _FBRequestCallback;
<span style="color: #a61390;">@synthesize</span> usesBlockCallback;
&nbsp;
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span>FBRequest<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> getRequestWithParams<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSMutableDictionary</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>params
                         httpMethod<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>httpMethod
                           callback<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">^</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#40;</span>FBRequest <span style="color: #002200;">*</span>request, <span style="color: #a61390;">id</span> result, <span style="color: #400080;">NSError</span> <span style="color: #002200;">*</span>error<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span> _block
                         requestURL<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>url
<span style="color: #002200;">&#123;</span>
    FBRequest<span style="color: #002200;">*</span> request <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>FBRequest alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
    request.delegate <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
    request.url <span style="color: #002200;">=</span> url;
    request.httpMethod <span style="color: #002200;">=</span> httpMethod;
    request.params <span style="color: #002200;">=</span> params;
    request.connection <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
    request.responseText <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
    request.FBRequestCallback <span style="color: #002200;">=</span> _block;
&nbsp;
    <span style="color: #a61390;">return</span> request;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>In the FBRequest class we are duplicating what they already have in the provided method but swapping out the delegate with a block. Out of personal preference I went with a single callback versus having an error and result callback. If there is no error we will be returning nil for the error parameter.</p>
<p>I also added a boolean property that will be used to determine which callback to use when processing the data when it has been returned from the Facebook servers.</p>
<p>Moving onto the &#8211; (void)handleResponseData:(NSData *)data; Method we need to add logic to pass the result request data onto the block callback.</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>handleResponseData<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>
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>_delegate respondsToSelector<span style="color: #002200;">:</span>
      <span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>request<span style="color: #002200;">:</span>didLoadRawResponse<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
    <span style="color: #002200;">&#91;</span>_delegate request<span style="color: #002200;">:</span>self didLoadRawResponse<span style="color: #002200;">:</span>data<span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#125;</span>
&nbsp;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>usesBlockCallback<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #400080;">NSError</span><span style="color: #002200;">*</span> error <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
        <span style="color: #a61390;">id</span> result <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self parseJsonResponse<span style="color: #002200;">:</span>data error<span style="color: #002200;">:&amp;</span>amp;error<span style="color: #002200;">&#93;</span>;
        _FBRequestCallback<span style="color: #002200;">&#40;</span>self, result, error<span style="color: #002200;">&#41;</span>;
    <span style="color: #002200;">&#125;</span>
    <span style="color: #a61390;">else</span> <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>_delegate respondsToSelector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>request<span style="color: #002200;">:</span>didLoad<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span> ||
             <span style="color: #002200;">&#91;</span>_delegate respondsToSelector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>request<span style="color: #002200;">:</span>didFailWithError<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#123;</span>
        <span style="color: #400080;">NSError</span><span style="color: #002200;">*</span> error <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
        <span style="color: #a61390;">id</span> result <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self parseJsonResponse<span style="color: #002200;">:</span>data error<span style="color: #002200;">:&amp;</span>amp;error<span style="color: #002200;">&#93;</span>;
&nbsp;
        <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>error<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
            <span style="color: #002200;">&#91;</span>self failWithError<span style="color: #002200;">:</span>error<span style="color: #002200;">&#93;</span>;
        <span style="color: #002200;">&#125;</span>
        <span style="color: #a61390;">else</span> <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>_delegate respondsToSelector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>request<span style="color: #002200;">:</span>didLoad<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
            <span style="color: #002200;">&#91;</span>_delegate request<span style="color: #002200;">:</span>self didLoad<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>result <span style="color: #002200;">==</span> <span style="color: #a61390;">nil</span> ? data <span style="color: #002200;">:</span> result<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
        <span style="color: #002200;">&#125;</span>
&nbsp;
    <span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>We are using the same logic as the standard delegate methodology, except we do not need to make a separate delegate call if there is an error. Within the block we have included result and an NSError parameter. That way you can handle the error logic within the same callback.</p>
<p>Finally within the dealloc method we need to release the block. Since we are using the block in objective-c we can simply use [_FBRequestCallback release]; You can also use Block_release(_FBRequestCallback); but since we are using it within Objective-C we can use the standard way of releasing an object.</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>dealloc <span style="color: #002200;">&#123;</span>
    <span style="color: #002200;">&#91;</span>_FBRequestCallback release<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>_connection cancel<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>_connection release<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>_responseText release<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>_url release<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>_httpMethod release<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>_params release<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>super dealloc<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>Step 2: Adding request methods with block callbacks</p>
<p>We want to include two new methods to the Facebook.h class. One method is for graph API requests, and one is for the the old REST API.</p>
<p>In the Facebook.h add the following two 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> requestWithGraphPath<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> _graphPath
                       params<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSMutableDictionary</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> _params
                       method<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> _method
                     callback<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">^</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#40;</span>FBRequest <span style="color: #002200;">*</span>request, <span style="color: #a61390;">id</span> result, <span style="color: #400080;">NSError</span> <span style="color: #002200;">*</span>error<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span> _block;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> requestWithMethodName<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> _methodName
                     andParams<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSMutableDictionary</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> _params
                 andHttpMethod<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> _method
                      callback<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">^</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#40;</span>FBRequest <span style="color: #002200;">*</span>request, <span style="color: #a61390;">id</span> result, <span style="color: #400080;">NSError</span> <span style="color: #002200;">*</span>error<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span> _block;</pre></td></tr></table></div>

<p>In the Facebook.m file add the following two method implementations:</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> requestWithGraphPath<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> _graphPath
                       params<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSMutableDictionary</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> _params
                       method<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> _method
                     callback<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">^</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#40;</span>FBRequest <span style="color: #002200;">*</span>request, <span style="color: #a61390;">id</span> result, <span style="color: #400080;">NSError</span> <span style="color: #002200;">*</span>error<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span> _block
<span style="color: #002200;">&#123;</span>
    <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span> fullURL <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>kGraphBaseURL stringByAppendingString<span style="color: #002200;">:</span>_graphPath<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>_params setValue<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;json&quot;</span> forKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;format&quot;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>_params setValue<span style="color: #002200;">:</span>kSDK forKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;sdk&quot;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>_params setValue<span style="color: #002200;">:</span>kSDKVersion forKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;sdk_version&quot;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>self isSessionValid<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #002200;">&#91;</span>_params setValue<span style="color: #002200;">:</span>self.accessToken forKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;access_token&quot;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
&nbsp;
    <span style="color: #002200;">&#91;</span>_request release<span style="color: #002200;">&#93;</span>;
    <span style="color: #11740a; font-style: italic;">//modify request to have block</span>
    _request <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>FBRequest getRequestWithParams<span style="color: #002200;">:</span>_params
                                     httpMethod<span style="color: #002200;">:</span>_method
                                       callback<span style="color: #002200;">:</span>_block
                                     requestURL<span style="color: #002200;">:</span>fullURL<span style="color: #002200;">&#93;</span> retain<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>_request connect<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> requestWithMethodName<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> _methodName
                     andParams<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSMutableDictionary</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> _params
                 andHttpMethod<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> _method
                      callback<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">^</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#40;</span>FBRequest <span style="color: #002200;">*</span>request, <span style="color: #a61390;">id</span> result, <span style="color: #400080;">NSError</span> <span style="color: #002200;">*</span>error<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span> _block
<span style="color: #002200;">&#123;</span>
    <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span> fullURL <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>kRestserverBaseURL stringByAppendingString<span style="color: #002200;">:</span>_methodName<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>_params setValue<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;json&quot;</span> forKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;format&quot;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>_params setValue<span style="color: #002200;">:</span>kSDK forKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;sdk&quot;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>_params setValue<span style="color: #002200;">:</span>kSDKVersion forKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;sdk_version&quot;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>self isSessionValid<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #002200;">&#91;</span>_params setValue<span style="color: #002200;">:</span>self.accessToken forKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;access_token&quot;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
&nbsp;
    <span style="color: #002200;">&#91;</span>_request release<span style="color: #002200;">&#93;</span>;
    <span style="color: #11740a; font-style: italic;">//modify request to have block</span>
    _request <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>FBRequest getRequestWithParams<span style="color: #002200;">:</span>_params
                                     httpMethod<span style="color: #002200;">:</span>_method
                                       callback<span style="color: #002200;">:</span>_block
                                     requestURL<span style="color: #002200;">:</span>fullURL<span style="color: #002200;">&#93;</span> retain<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>_request connect<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>Once again we are duplicating the previously implemented methods by Facebook. The only difference is changing the request method. As you notice we are now using the methods we implemented in the FBRequest class.</p>
<p>Now that we have everything implemented that we need in the Facebook SDK we can use our awesome new block callbacks! The following snippet will return the logged in users friends!</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;">    <span style="color: #400080;">NSMutableDictionary</span> <span style="color: #002200;">*</span>params <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: #400080;">NSMutableDictionary</span> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>facebook requestWithGraphPath<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;me/friends&quot;</span>
                                    params<span style="color: #002200;">:</span>params
                                    method<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;GET&quot;</span>
                                  callback<span style="color: #002200;">:^</span><span style="color: #002200;">&#40;</span>FBRequest <span style="color: #002200;">*</span>request, <span style="color: #a61390;">id</span> result, <span style="color: #400080;">NSError</span> <span style="color: #002200;">*</span>error<span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#123;</span>
        NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;friends %@&quot;</span>, result<span style="color: #002200;">&#41;</span>;
        <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>result isKindOfClass<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDictionary</span> class<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
            friendsList <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>result objectForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;data&quot;</span><span style="color: #002200;">&#93;</span> retain<span style="color: #002200;">&#93;</span>;
            <span style="color: #002200;">&#91;</span>table reloadData<span style="color: #002200;">&#93;</span>;
        <span style="color: #002200;">&#125;</span>
&nbsp;
    <span style="color: #002200;">&#125;</span><span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://icodeblog.com/2011/07/19/adding-block-callbacks-to-the-facebook-ios-sdk/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Creating Static Libraries For iOS</title>
		<link>http://icodeblog.com/2011/04/07/creating-static-libraries-for-ios/</link>
		<comments>http://icodeblog.com/2011/04/07/creating-static-libraries-for-ios/#comments</comments>
		<pubDate>Thu, 07 Apr 2011 21:54:38 +0000</pubDate>
		<dc:creator><![CDATA[brandontreb]]></dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Advanced]]></category>
		<category><![CDATA[static libraries]]></category>

		<guid isPermaLink="false">http://icodeblog.com/?p=3041</guid>
		<description><![CDATA[Today I&#8217;m going to show you how to make a static library for iOS.  We will make a simple library and use it in a separate project.
What&#8217;s a static library
Here is what <a href="http://en.wikipedia.org/wiki/Static_library">wiki</a> has to say:
In computer science, a static library or statically-linked library is a set of routines, external functions and variables which are resolved in a caller at compile-time and copied into a target application by a compiler, linker, or binder, producing an object file and  ...]]></description>
				<content:encoded><![CDATA[<p>Today I&#8217;m going to show you how to make a static library for iOS.  We will make a simple library and use it in a separate project.</p>
<h4>What&#8217;s a static library</h4>
<p>Here is what <a href="http://en.wikipedia.org/wiki/Static_library">wiki</a> has to say:</p>
<blockquote><p><em>In computer science, a static library or statically-linked library is a set of routines, external functions and variables which are resolved in a caller at compile-time and copied into a target application by a compiler, linker, or binder, producing an object file and a stand-alone executable. This executable and the process of compiling it are both known as a static build of the program. Historically, libraries could only be static.</em></p></blockquote>
<p>On the iPhone, static libraries have a .a extention and can only contain code.  This means that any resources (xibs, images, etc&#8230;) must be packed either in a bundle or shipped separately than the library.  We won&#8217;t go into this part today.</p>
<h4>Why Use Static Libraries</h4>
<p>There are a number of reasons to use static libraries, but the main two are distribution to others without revealing source code, and code reuse across projects.</p>
<p>1. <strong>Distribution</strong>:</p>
<p>Say you have a great idea for a view that given a few images, creates a beautiful photo mosaic.  Now, others might want to use your awesome mosaic class as part of their fart-light (fart machine flashlight) app to make mosaic fart-lights from your photo library.  You have done a ton of work to make your mosaic app and don&#8217;t want to just give away the code.  $$$ you decide to sell your code.  However, would-be thieves might not honor your awesome license and begin to redistribute the code as if it were their own (oh noooz). Boom static library.</p>
<p>Bundle up your code into a static library and package it with the header files.  Now others can purchase your library, use it in their own apps to make fart mosaics without revealing the source and you can continue to sell copy after copy.</p>
<p>2. <strong>Code reuse</strong>:</p>
<p>So, you have like 30 apps in the store that do just about nothing.  However they all have some common code, say a farting engine.  Rather than dragging all of the source files in and having redundant code all over the place, you can just compile your fart engine once and use the library in all of your projects.</p>
<h4>Where Can I Get Me One Of These?</h4>
<p>Well, let&#8217;s dig in and create a sample static library.  The project we are going to create is code-only (remember we are not going over resources in this tutorial) and will be a simple math library with two methods exposed.  It will be able to compute fibonacci numbers given number n and compute a factorial value given number n (I know, pretty dang exciting).</p>
<h4>Step 1: Starting a New Static Library Project</h4>
<p>This is pretty straight forward as Apple has made it pretty simple to create a static library.</p>
<p>Open XCode and start a new project. Under iOS, select Library and &#8220;Cocoa Touch Static Library&#8221;.  We are going to name ours ICodeMathUtils.  This will create a nice new project for us that builds a .a file.</p>
<p><img src="/wp-content/uploads/2011/04/Screen-shot-2011-04-07-at-11.31.31-AM.png" alt="" width="300" /></p>
<p>One thing to note is, NO class files get created by default.  Don&#8217;t freak out, we will create some manually.</p>
<h4>Step 2: Code your static library</h4>
<p>First we need to add some files.  Add a new NSObject subclass to your project and name it MathFunctions.m.</p>
<p>Add this code to the header file:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import </span>
&nbsp;
<span style="color: #a61390;">@interface</span> MathFunctions <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span> <span style="color: #002200;">&#123;</span>
<span style="color: #002200;">&#125;</span>
&nbsp;
<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> fibonacci<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span> n;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span> factorial<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span> n;
&nbsp;
<span style="color: #a61390;">@end</span></pre></td></tr></table></div>

<p>Nothing fancy, just our method declarations.  When we distribute the library file, this header file MUST be included as well.</p>
<p>One thing I should point out here is <strong>don&#8217;t make every iVar public!</strong> As a newer developer (or a stubborn lazy one: Im looking at you <a href="http://twitter.com/cruffenach">@cruffenach</a>), you might think it&#8217;s cool to make a public property out of EVERYTHING.  This is bad form and here is where it will bite you.  You don&#8217;t want the external world being able to muck with things that you don&#8217;t want them to.</p>
<p>And Now the .m file for the implementation:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &quot;MathFunctions.h&quot;</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> MathFunctions
&nbsp;
<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> fibonacci<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span> n <span style="color: #002200;">&#123;</span>
&nbsp;
	<span style="color: #400080;">NSMutableArray</span> <span style="color: #002200;">*</span>fib <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableArray</span> array<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #a61390;">int</span> a <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;
	<span style="color: #a61390;">int</span> b <span style="color: #002200;">=</span> <span style="color: #2400d9;">1</span>;
	<span style="color: #a61390;">int</span> sum;
	<span style="color: #a61390;">int</span> i;
&nbsp;
	<span style="color: #a61390;">for</span> <span style="color: #002200;">&#40;</span>i<span style="color: #002200;">=</span><span style="color: #2400d9;">0</span>;i <span style="color: #002200;">&amp;</span>lt; n;i<span style="color: #002200;">++</span><span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #002200;">&#91;</span>fib addObject<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNumber</span> numberWithInt<span style="color: #002200;">:</span>a<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
		sum <span style="color: #002200;">=</span> a <span style="color: #002200;">+</span> b;
		a <span style="color: #002200;">=</span> b;
		b <span style="color: #002200;">=</span> sum;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #a61390;">return</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> fib;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span> factorial<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span> n <span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> n <span style="color: #002200;">&amp;</span>lt;<span style="color: #002200;">=</span> <span style="color: #2400d9;">1</span> <span style="color: #002200;">&#41;</span>
		<span style="color: #a61390;">return</span> <span style="color: #2400d9;">1</span>;
	<span style="color: #a61390;">else</span>
		<span style="color: #a61390;">return</span> n <span style="color: #002200;">*</span> <span style="color: #002200;">&#91;</span>self factorial<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span> n<span style="color: #002200;">-</span><span style="color: #2400d9;">1</span> <span style="color: #002200;">&#41;</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>This code should be very familiar to any CS 101 student : ).  And that&#8217;s it for our static library.  The best way to test this puppy out before distributing is to create a new unit test target, set up some unit tests, and check it out that way. That tutorial is for another day.</p>
<h4>Building And Distributing Your Library</h4>
<p>Once you are happy with your library, simply build it in XCode.  Obviously, don&#8217;t do build and run as you can&#8217;t run it (again unless you have a test suite).  Now, look under the <strong>Products</strong> group in XCode and you should see a file called lib(libraryName).a. In our case, it&#8217;s libICodeMathUtils.a.</p>
<p>Right click on that file and select &#8220;Reveal In Finder&#8221;. Drag this file into a new folder that you create where you will be bundling all of your library files.  I just created a folder on my desktop named iCodeBlogsMathLibrary.  Now, do the same with all of the .h files.  In our case, just copy MathFunctions.h into this new directory.  Your directory structure should now look like:</p>
<p>iCodeBlogsMathLibarby<br />
|- libICodeMathUtils.a<br />
|- MathFunctions.h</p>
<p>Now you can zip this folder and sell it to would-be iOS developers for millions!</p>
<h4>Linking Your Library In A New Project</h4>
<p><img class="alignright" style="float:right;padding:5px" src="/wp-content/uploads/2011/04/Screen-shot-2011-04-07-at-11.43.14-AM.png" alt="" width="250" /></p>
<p>So now that you have built your shiny new static library, it&#8217;s time to test it out in another application. XCode has a number of ways to actually achieve this, but I will show the most simple and Mac-like&#8230; drag and drop : ).</p>
<p>Create a new View-Based project (or whatever it doesn&#8217;t really matter).  I named mine MathTest.</p>
<p>Now, just drag this folder into the project and XCode will set up all of the linking automagically.  When prompted to copy, I usually say yes, but you can do whatever you want depending on how you intend on using the library.  Sometimes just linking and not copying is far more beneficial if you have multiple projects sharing a single library.  It ensures that they all use the most up to date version.</p>
<p>You should now see the .a file along with the header files in the new project.</p>
<p><img src="/wp-content/uploads/2011/04/Screen-shot-2011-04-07-at-11.43.33-AM.png" alt="" /></p>
<h4>Using The Static Library Code</h4>
<p>Now that all of the linking is set up, you just use your library like any other class.  In the App Delegate class of my test project, I simple did this:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Import at the top</span>
<span style="color: #6e371a;">#import &quot;MathFunctions.h&quot;</span></pre></td></tr></table></div>

<p>And in the applicationDidFinishLaunchingMethod&#8230;</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;">MathFunctions <span style="color: #002200;">*</span>mFunctions <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>MathFunctions alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;fibonacci for 10 = %@&quot;</span>, <span style="color: #002200;">&#91;</span>mFunctions fibonacci<span style="color: #002200;">:</span><span style="color: #2400d9;">10</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;10! = %d&quot;</span>,<span style="color: #002200;">&#91;</span>mFunctions factorial<span style="color: #002200;">:</span><span style="color: #2400d9;">10</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;</pre></td></tr></table></div>

<p>This of course prints out:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000;">2011</span>-04-07 <span style="color: #000000;">11</span>:<span style="color: #000000;">48</span>:<span style="color: #000000;">49.528</span> MathTest<span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000;">854</span>:<span style="color: #000000;">607</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> fibonacci <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #000000;">10</span> = <span style="color: #7a0874; font-weight: bold;">&#40;</span>
    <span style="color: #000000;">0</span>,
    <span style="color: #000000;">1</span>,
    <span style="color: #000000;">1</span>,
    <span style="color: #000000;">2</span>,
    <span style="color: #000000;">3</span>,
    <span style="color: #000000;">5</span>,
    <span style="color: #000000;">8</span>,
    <span style="color: #000000;">13</span>,
    <span style="color: #000000;">21</span>,
    <span style="color: #000000;">34</span>
<span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #000000;">2011</span>-04-07 <span style="color: #000000;">11</span>:<span style="color: #000000;">48</span>:<span style="color: #000000;">49.533</span> MathTest<span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000;">854</span>:<span style="color: #000000;">607</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #000000;">10</span><span style="color: #000000; font-weight: bold;">!</span> = <span style="color: #000000;">3628800</span></pre></td></tr></table></div>

<p>And there it is! Static library code being executed in a totally separate project.</p>
<h4>Conclusion</h4>
<p>Like I said earlier, static libraries can be quite helpful in your iPhone development endeavors.  I feel that they are quite underused, but could really save time and money in the long run.</p>
<p>You can <a href="/wp-content/uploads/2011/04/ICodeBlogStaticLibrarySample.zip">download the source code here</a>.</p>
<p>As always, if you have any questions please feel free to leave them in the comments OR <a href="http://twitter.com/brandontreb">write me on Twitter</a>.</p>
<p>Happy iCoding!</p>
]]></content:encoded>
			<wfw:commentRss>http://icodeblog.com/2011/04/07/creating-static-libraries-for-ios/feed/</wfw:commentRss>
		<slash:comments>49</slash:comments>
		</item>
		<item>
		<title>Update: ELCImagePickerController</title>
		<link>http://icodeblog.com/2011/03/03/update-elcimagepickercontroller/</link>
		<comments>http://icodeblog.com/2011/03/03/update-elcimagepickercontroller/#comments</comments>
		<pubDate>Thu, 03 Mar 2011 00:23:14 +0000</pubDate>
		<dc:creator><![CDATA[Matt Tuzzolo]]></dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Featured]]></category>
		<category><![CDATA[Advanced]]></category>
		<category><![CDATA[ELCImagePickerController]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[UIImagePickerController]]></category>

		<guid isPermaLink="false">http://icodeblog.com/?p=2897</guid>
		<description><![CDATA[I recently spent some  time with <a href="https://github.com/elc/ELCImagePickerController">ELCImagePickerController</a>.  For those of you who&#8217;ve worked with UIImagePickerController, you might have noticed one of its major drawbacks: you can only select one photo at a time.  ELCImagePickerController solves this issue by cloning the UI of UIImagePickerController, but with the added bonus of allowing you to select multiple assets.  Collin Ruffenach (<a href="http://twitter.com/cruffenach">@cruffenach</a>), who authored the the first version of the picker, has done an awesome job of making  ...]]></description>
				<content:encoded><![CDATA[<p>I recently spent some  time with <a href="https://github.com/elc/ELCImagePickerController">ELCImagePickerController</a>.  For those of you who&#8217;ve worked with UIImagePickerController, you might have noticed one of its major drawbacks: you can only select one photo at a time.  ELCImagePickerController solves this issue by cloning the UI of UIImagePickerController, but with the added bonus of allowing you to select multiple assets.  Collin Ruffenach (<a href="http://twitter.com/cruffenach">@cruffenach</a>), who authored the the first version of the picker, has done an awesome job of making ELCImagePickerController look, feel, and behave like a native image picker.  For this post I&#8217;m going to go through some recent improvements I made to ELCImagePickerController and pass along some lessons learned from working with the AssetsLibrary Framework.</p>
<p>This is my first foray into the AssetsLibrary Framework which was introduced with iOS 4.0.  If you&#8217;re not already familiar with the framework, I highly suggest checking out both of these posts:</p>
<h3><a title="Asset Libraries and Blocks in iOS 4" rel="bookmark" href="/2010/07/08/asset-libraries-and-blocks-in-ios-4/">Asset Libraries and Blocks in iOS 4</a></h3>
<h3><a title="Cloning UIImagePickerController using the Assets Library Framework" rel="bookmark" href="/2010/10/07/cloning-uiimagepickercontroller-using-the-assets-library-framework/">Cloning UIImagePickerController using the Assets Library Framework</a></h3>
<p>These should give you a solid picture of how to work with ALAssetLibrary, ALAssetGroup, and ALAsset.</p>
<p>One of the obvious changes I&#8217;ve made to the project is a reorganization of the code.  Classes are broken out for clarity as you can see here:</p>
<p>Old Tree:<br />
<img class="alignnone" src="/wp-content/uploads/2011/03/old_tree.png" alt="" width="289" height="191" /><br />
New Tree:<br />
<img class="alignnone" src="/wp-content/uploads/2011/03/Screen-shot-2011-03-01-at-5.14.49-PM.png" alt="" width="246" height="309" /></p>
<div>I&#8217;ve also renamed several classes to avoid confusion w/ Apple&#8217;s and ELC&#8217;s respective classes.</div>
<p>On major change to the project is how you present the new ELCImagePickerController.  This is mainly due to an issue we uncovered with a redundant call to [super init] which was causing a substantial memory leak.  Here&#8217;s the new way of showing the image picker:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="obj-c" style="font-family:monospace;">ELCAlbumPickerController *albumController = [[ELCAlbumPickerController alloc] initWithNibName:@&quot;ELCAlbumPickerController&quot; bundle:[NSBundle mainBundle]];ELCImagePickerController *elcPicker = [[ELCImagePickerController alloc] initWithRootViewController:albumController];
&nbsp;
[albumController setParent:elcPicker];
&nbsp;
[elcPicker setDelegate:self];
&nbsp;
ELCImagePickerDemoAppDelegate *app = (ELCImagePickerDemoAppDelegate *)[[UIApplication sharedApplication] delegate];
&nbsp;
[app.viewController presentModalViewController:elcPicker animated:YES];
&nbsp;
[elcPicker release];
&nbsp;
[albumController release];</pre></td></tr></table></div>

<p>One interesting optimization I was able to make was on the loading of large albums which was previously taking several seconds.  Since ALAssetGroup uses a block to enumerate assets, I just fire off a reloadTable call after a short delay:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="obj-c" style="font-family:monospace;">// Show partial list while full list loads
&nbsp;
[self.tableView performSelector:@selector(reloadData) withObject:nil afterDelay:.5];</pre></td></tr></table></div>

<p>Then when the block is finished enumerating, it&#8217;ll call reloadTable as well.  On an album with roughly 1500 photos, it was pretty tough to reach the bottom of the tableview before the block finished enumerating.  So this is a pretty decent solution to the issue.  Originally, I experimented with lazy loading the assets as the user scrolled through the table, but ultimately wasn&#8217;t able to get the performance I wanted out of it.</p>
<p>This release of ELCImagePickerController should perform faster, and with a smaller memory footprint than before.  I hope you enjoy it.</p>
<p>You can follow me on twitter <a href="http://twitter.com/matt_tuzzolo">@matt_tuzzolo</a> or get in touch with us at <a href="http://www.elctech.com">http://www.elctech.com</a></p>
<p>ps. for an extra bonus, check out [ELCAsset toggleSelection];</p>
]]></content:encoded>
			<wfw:commentRss>http://icodeblog.com/2011/03/03/update-elcimagepickercontroller/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Working with UIGestureRecognizers</title>
		<link>http://icodeblog.com/2010/10/14/working-with-uigesturerecognizers/</link>
		<comments>http://icodeblog.com/2010/10/14/working-with-uigesturerecognizers/#comments</comments>
		<pubDate>Fri, 15 Oct 2010 00:37:52 +0000</pubDate>
		<dc:creator><![CDATA[Collin]]></dc:creator>
				<category><![CDATA[Featured]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Advanced]]></category>
		<category><![CDATA[Drag]]></category>
		<category><![CDATA[Multi Touch]]></category>
		<category><![CDATA[Rotate]]></category>
		<category><![CDATA[Scale]]></category>
		<category><![CDATA[UIGestureRecognizers]]></category>
		<category><![CDATA[UIImage]]></category>

		<guid isPermaLink="false">http://icodeblog.com/?p=2489</guid>
		<description><![CDATA[Hey iCoders! Today we are going to make a fun project that takes advantage of UIGestureRecognizers which were introduced in iOS 3.0, way back when it was called iPhone OS. UIGestureRecognizer is an abstract class that several concrete classes extend Eg. UITapGestureRecognizer, UIPinchGestureRecognizer. Today we are going to be building a simple photo board application. You will be able to add photos from your board, move, rotate and zoom them in and out around the board. We will also build in some simple physics to give a sense of the photos being thrown around the board. Here is a short video of what our final product will look like.]]></description>
				<content:encoded><![CDATA[<p>Hey iCoders! Today we are going to make a fun project that takes advantage of UIGestureRecognizers which were introduced in iOS 3.0, way back when it was called iPhone OS. UIGestureRecognizer is an abstract class that several concrete classes extend Eg. UITapGestureRecognizer, UIPinchGestureRecognizer. Today we are going to be building a simple photo board application. You will be able to add photos from your board, move, rotate and zoom them in and out around the board. We will also build in some simple physics to give a sense of the photos being thrown around the board. Here is a short video of what our final product will look like.</p>
<p><iframe src="http://player.vimeo.com/video/15779202?portrait=0&amp;color=ff9933" width="600" height="375" frameborder="0"></iframe>
<p><a href="http://vimeo.com/15779202">Demo of Photo Board Application for iCodeBlog.com</a> from <a href="http://vimeo.com/user2008025">Collin Ruffenach</a> on <a href="http://vimeo.com">Vimeo</a>.</p>
<h2>GIT Hub</h2>
<p>You can find this project <a title="Demo Paste Board Application on GITHub" href="http://github.com/elc/iCodeBlogDemoPhotoBoard" target="_blank">now on GitHub</a>. Please let me know any issues you may have. Happy coding!</p>
<h2>Creating the project</h2>
<p>Lets get a project ready that can handle all of this functionality. Open up xCode and start a View based iPad application called DemoPhotoBoard. Once the project window has come up go to the Framework group in the left bar and right click on it. Select Add -&gt; Existing Framework&#8230; A large model view will come down listing all of the Frameworks that can be added to the project. Add in the Framework &#8220;Mobile Core Services&#8221;. Now go into DemoPhotoBoardViewController.h and add in the line</p>
<p><img class="aligncenter size-full wp-image-2493" title="Screen shot 2010-10-12 at 11.22.25 AM" src="/wp-content/uploads/2010/10/Screen-shot-2010-10-12-at-11.22.25-AM1.png" alt="" width="400" height="28" /></p>
<p>I apologies for having to use the image there, WordPress hates any line of code with &lt; or &gt; in it. We might as well finish filling in the rest of the header now too. Don&#8217;t worry about what these properties will be used for yet just include them in the header for the moment, they will be what we use to keep track of our scaling, movement and rotation. The add photo method will be called from a button we put in our interface in the next step.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> DemoPhotoBoardViewController <span style="color: #002200;">:</span> UIViewController  <span style="color: #002200;">&#123;</span>
&nbsp;
	CGFloat lastScale;
	CGFloat lastRotation;
&nbsp;
	CGFloat firstX;
	CGFloat firstY;
<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>addPhoto<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>

<h2>Filling in the XIB</h2>
<p>The next thing we are going to do is add a tool bar and tool bar button to our XIB. Double click on DemoPhotoBoardViewController.xib. Once it has opened drag in a UIToolBar Item and then Put a UIToolBarButtonItem with a Flexible Space element to the left of it. Make the UIBarButtonItem the system button &#8220;Add&#8221;. Now if you go to the file owner below and right click on it, you should  see an outlet for the method &#8220;addPhoto&#8221;. Connect this to the add button we have. As a final step, select the UIToolBar and look at its size panel in the inspector. Make sure the Autosizing settings match the settings seen below so that things don&#8217;t get screwy when the app is in other orientations.</p>
<p><a href="/wp-content/uploads/2010/10/Screen-shot-2010-10-12-at-11.31.47-AM1.png"><img class="aligncenter size-full wp-image-2497" title="Screen shot 2010-10-12 at 11.31.47 AM" src="/wp-content/uploads/2010/10/Screen-shot-2010-10-12-at-11.31.47-AM1.png" alt="" width="301" height="450" /></a></p>
<h2>Implementing the Photo Picker</h2>
<p>Go ahead and open up DemoPhotoBoardViewController.m. The first thing we are going to do is implement the addPhoto method. 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: #002200;">-</span><span style="color: #002200;">&#40;</span>IBAction<span style="color: #002200;">&#41;</span>addPhoto<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;
	UIImagePickerController <span style="color: #002200;">*</span>controller <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIImagePickerController alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>controller setMediaTypes<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSArray</span> arrayWithObject<span style="color: #002200;">:</span>kUTTypeImage<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>controller setDelegate<span style="color: #002200;">:</span>self<span style="color: #002200;">&#93;</span>;
&nbsp;
	UIPopoverController <span style="color: #002200;">*</span>popover <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIPopoverController alloc<span style="color: #002200;">&#93;</span> initWithContentViewController<span style="color: #002200;">:</span>controller<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>popover setDelegate<span style="color: #002200;">:</span>self<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>popover presentPopoverFromBarButtonItem<span style="color: #002200;">:</span>sender permittedArrowDirections<span style="color: #002200;">:</span>UIPopoverArrowDirectionUp animated<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>This method creates a UIImagePickerController and tells it to only display images. Next we create an UIPopoverController that we instantiate with our UIImagePickerController as the content view controller. We set the delegate to ourself, and present it from the bar button item sender, which refers to the add button in our interface. We know the pop over will always be below our button so we force the button direction to always be point up. With this done, we can now run the app and see a UIImagePickercController appearing in a UIPopOverController below our add button.</p>
<h2>Setting up the Gesture Recognizers</h2>
<p>Now we need to implement the delegate method for our UIImagePickerController and add the image to our view when it is selected. We do this with the imagePickerControler:DidFinishPickingMediaWithInfo: delegate method. This method will provide us a dictionary where the key @&#8221;UIImagePickerControllerOriginalImage&#8221; will return a UIImage object of the image the user selected. We are going to need to create an ImageView and then put this ImageView in a UIView holder. The reason we do this is because standard UIImageViews, despite being UIView subclasses, do not react to gesture recognizer added to them. I&#8217;m not exactly sure why that is but this is the solution I have found in my testing. We are going to create 4 different kinds UIGestureRecognizers and connect them to our holder view.</p>
<p>We will first create a UIPinchGestureRecognizer. This object doesn&#8217;t require and customization, we will simply set its target to us with the scale: selector and assign this class as its delegate. With this done we add it to the holder view we created.</p>
<p>Next we create a UIRotationGestureRecognizer. This object doesn&#8217;t require much customization either. We simply set it to call the rotate: method in our class and set its delegate.</p>
<p>Net we create the UIPanGestureRecognizer. We create the PanGestureRecognizer to make a call to the method move: upon being activated. We tell the PanGestureRecognizer that we only care when a single touch is panning by setting the maximum and minimum touches to 1. We once again add this to the holder view we created.</p>
<p>The final UIGestureRecognizer we create is the UITapGestureRecognizer. The UITapGestureRecognizer will be used to stop an object that has been &#8220;thrown&#8221; from going all the way to its stopping point. It essentially will be used to catch an object while it is still moving. We set the required number of taps to 1 and set the delegate. We add this final UIGestureRecognizer to our holder view and add the view to subview. You can see the code below. Please ignore the random number WordPress is weak sauce.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;">&nbsp;</pre></td></tr></table></div>


<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>imagePickerController<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIImagePickerController <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>picker didFinishPickingMediaWithInfo<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>info <span style="color: #002200;">&#123;</span>
&nbsp;
	UIImage <span style="color: #002200;">*</span>image <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>info objectForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;UIImagePickerControllerOriginalImage&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
	UIView <span style="color: #002200;">*</span>holderView <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIView alloc<span style="color: #002200;">&#93;</span> initWithFrame<span style="color: #002200;">:</span>CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0</span>, <span style="color: #2400d9;">0</span>, image.size.width, image.size.height<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
	UIImageView <span style="color: #002200;">*</span>imageview <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIImageView alloc<span style="color: #002200;">&#93;</span> initWithFrame<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>holderView frame<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>imageview setImage<span style="color: #002200;">:</span>image<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>holderView addSubview<span style="color: #002200;">:</span>imageview<span style="color: #002200;">&#93;</span>;
&nbsp;
	UIPinchGestureRecognizer <span style="color: #002200;">*</span>pinchRecognizer <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIPinchGestureRecognizer alloc<span style="color: #002200;">&#93;</span> initWithTarget<span style="color: #002200;">:</span>self action<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>scale<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>pinchRecognizer setDelegate<span style="color: #002200;">:</span>self<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>holderView addGestureRecognizer<span style="color: #002200;">:</span>pinchRecognizer<span style="color: #002200;">&#93;</span>;
&nbsp;
	UIRotationGestureRecognizer <span style="color: #002200;">*</span>rotationRecognizer <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIRotationGestureRecognizer alloc<span style="color: #002200;">&#93;</span> initWithTarget<span style="color: #002200;">:</span>self action<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>rotate<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>rotationRecognizer setDelegate<span style="color: #002200;">:</span>self<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>holderView addGestureRecognizer<span style="color: #002200;">:</span>rotationRecognizer<span style="color: #002200;">&#93;</span>;
&nbsp;
	UIPanGestureRecognizer <span style="color: #002200;">*</span>panRecognizer <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIPanGestureRecognizer alloc<span style="color: #002200;">&#93;</span> initWithTarget<span style="color: #002200;">:</span>self action<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>move<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>panRecognizer setMinimumNumberOfTouches<span style="color: #002200;">:</span><span style="color: #2400d9;">1</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>panRecognizer setMaximumNumberOfTouches<span style="color: #002200;">:</span><span style="color: #2400d9;">1</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>panRecognizer setDelegate<span style="color: #002200;">:</span>self<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>holderView addGestureRecognizer<span style="color: #002200;">:</span>panRecognizer<span style="color: #002200;">&#93;</span>;
&nbsp;
	UITapGestureRecognizer <span style="color: #002200;">*</span>tapRecognizer <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UITapGestureRecognizer alloc<span style="color: #002200;">&#93;</span> initWithTarget<span style="color: #002200;">:</span>self action<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>tapped<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>tapRecognizer setNumberOfTapsRequired<span style="color: #002200;">:</span><span style="color: #2400d9;">1</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>tapRecognizer setDelegate<span style="color: #002200;">:</span>self<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>holderView addGestureRecognizer<span style="color: #002200;">:</span>tapRecognizer<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #002200;">&#91;</span>self.view addSubview<span style="color: #002200;">:</span>holderView<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>Quickly lets also define each of these methods as such so we can see them all occurring as we touch an object that we add to the view. Add this code in and run the application, you can click around on the objects you add to the board and see the Log messages displaying in the terminal. In the terminal you may or may not be able to activate all of these, because of multi touch being simulated in a pretty limited way. But you can run the code and try this out.</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>scale<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>
	NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;See a pinch gesture&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>rotate<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>
	NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;See a rotate gesture&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>move<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>
	NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;See a move gesture&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>tapped<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>
	NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;See a tap gesture&quot;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<h2>UIGestureRecognizer Action Methods</h2>
<p>All UIGestureRecognizers contain a state property of type UIGestureRecognizerState. This is because of UIGestureRecognizers calling their action methods throughout the entire time a gesture is being performed. When the gesture first begins the state of the calling UIGestureRecognizer is UIGestureRecognizerStateBegan, throughout its all subsequent calls have the state UIGestureRecognizerStateChanged, and the final call is of state UIGestureRecognizerStateEnded. We can use this to our advantage to do house keeping in each of our gesture action methods. Another important thing to note about the action calls from UIGestureRecognizers is that the properties it will report about a gesture, such as scale for UIPinchGestureRecognizer and rotation for UIRotationGestureRecognizer, will always be in reference to the original state of the object. So as a scale is happening the scale may be reported as; 1.1, 1.2, 1.3, 1.4 and 1.5 on subsequent calls. These scales are not cumulative but all in reference to the original state of the object the UIGestureRecognizers are attached to.</p>
<h2>Implementing Scaling</h2>
<p>First thing we will do is implement the scale method. The scale method will be called by an id sender, this sender will actually be a UIPinchGestureRecognizer object. If we look at the documentation for a UIPinchGestureRecognizer object we will see that it includes a scale property that is a CGFloat. This scale property will be provided by the UIPinchGestureRecognizer every time the scale: method is called. Because the scale will be cumulative and always in reference to the original state of the object. Because of this, as we make our photo grow, we must make sure that we only scale by the difference of the last scale to the current scale. For example of the first scale: call has the UIPinchGestureRecognizer scale as being 1.1 and the next call has it by 1.2, we should scale by 1.1 and then by another 1.1. To handle this we have the the class property CGFloat lastScale. This will keep track of the last scale we applied to our view so  that on the next call we can only apply the difference between them.</p>
<p>So now that we can tell how much to scale an item upon being pinched we need to look into the mechanism that will actually scale the view. Every UIView has a CGAffineTransform property called transform. This described much of the geometry of the view that will be drawn. Using method provided by the Quartz frameworks we will figure out how to change this variable to scale as we need. Lets first take a look at out whole scaling method.</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>scale<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>self.view bringSubviewToFront<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#40;</span>UIPinchGestureRecognizer<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>sender view<span style="color: #002200;">&#93;</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;">&#40;</span>UIPinchGestureRecognizer<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>sender state<span style="color: #002200;">&#93;</span> <span style="color: #002200;">==</span> UIGestureRecognizerStateEnded<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
&nbsp;
		lastScale <span style="color: #002200;">=</span> <span style="color: #2400d9;">1.0</span>;
		<span style="color: #a61390;">return</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	CGFloat scale <span style="color: #002200;">=</span> <span style="color: #2400d9;">1.0</span> <span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>lastScale <span style="color: #002200;">-</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#40;</span>UIPinchGestureRecognizer<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>sender scale<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
	CGAffineTransform currentTransform <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#40;</span>UIPinchGestureRecognizer<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>sender view<span style="color: #002200;">&#93;</span>.transform;
	CGAffineTransform newTransform <span style="color: #002200;">=</span> CGAffineTransformScale<span style="color: #002200;">&#40;</span>currentTransform, scale, scale<span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#40;</span>UIPinchGestureRecognizer<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>sender view<span style="color: #002200;">&#93;</span> setTransform<span style="color: #002200;">:</span>newTransform<span style="color: #002200;">&#93;</span>;
&nbsp;
	lastScale <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#40;</span>UIPinchGestureRecognizer<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>sender scale<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>The first thing we do in this method is bring the touched view to the front. We do this by accessing the view property of our sender which in this case is the UIPinchGesturereRecognizer. Next thing we do is check if this is the final touch in this pinch motion. If it is we reset out lastTouch value to 1. When the scale of one is applied a view does not change. When a pinch has ended we set the final pinch as being the new starting point for the next pinch sequence, specifically 1. Any other touch besides the last will subtract the difference from the last scale and the current scale from 1. This will be the scale change between the current touch and the last. We want to apply this to the current CGAffineTransfom matrix of the view this gesture recognizer is attached to. We now get the current transform of the view and pass it into CGAffineTransformScale() method. The first parameter is for current transform and the following two are the x and y scale to be applied to the passed in transform. The output here will be the new transform for the view. We apply this and reset the scale.</p>
<h2>Implementing Rotation</h2>
<p>Next thing we handle is rotation. This method has a very similar structure to the scaling method. We use another class property called lastRotation this time instead and a slightly different Quartz method, but the code overall should make sense. Check it out 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>rotate<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>self.view bringSubviewToFront<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#40;</span>UIRotationGestureRecognizer<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>sender view<span style="color: #002200;">&#93;</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;">&#40;</span>UIRotationGestureRecognizer<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>sender state<span style="color: #002200;">&#93;</span> <span style="color: #002200;">==</span> UIGestureRecognizerStateEnded<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
&nbsp;
		lastRotation <span style="color: #002200;">=</span> <span style="color: #2400d9;">0.0</span>;
		<span style="color: #a61390;">return</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	CGFloat rotation <span style="color: #002200;">=</span> <span style="color: #2400d9;">0.0</span> <span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>lastRotation <span style="color: #002200;">-</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#40;</span>UIRotationGestureRecognizer<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>sender rotation<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
	CGAffineTransform currentTransform <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#40;</span>UIPinchGestureRecognizer<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>sender view<span style="color: #002200;">&#93;</span>.transform;
	CGAffineTransform newTransform <span style="color: #002200;">=</span> CGAffineTransformRotate<span style="color: #002200;">&#40;</span>currentTransform,rotation<span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#40;</span>UIRotationGestureRecognizer<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>sender view<span style="color: #002200;">&#93;</span> setTransform<span style="color: #002200;">:</span>newTransform<span style="color: #002200;">&#93;</span>;
&nbsp;
	lastRotation <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#40;</span>UIRotationGestureRecognizer<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>sender rotation<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<h2>Implementing Movement</h2>
<p>Now we handle movement which is a bit different that the rotation and scaling transformations. Although you could use the transform to move an object around using the transform property, we are going to continuously reset the center of each view. We utilize PanGestureRecognizers method translationInView to get the point which the view has been moved to in reference to its starting point. If this is the first touch from the gesture recognizer we set our class properties firstX and firstY. We the calculate our translated point by adding the original center points to the translated point in the view. We set the view&#8217;s center with this newly calculated view. You can see the code 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>move<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><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#40;</span>UITapGestureRecognizer<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>sender view<span style="color: #002200;">&#93;</span> layer<span style="color: #002200;">&#93;</span> removeAllAnimations<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #002200;">&#91;</span>self.view bringSubviewToFront<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#40;</span>UIPanGestureRecognizer<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>sender view<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
	CGPoint translatedPoint <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#40;</span>UIPanGestureRecognizer<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>sender translationInView<span style="color: #002200;">:</span>self.view<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;">&#40;</span>UIPanGestureRecognizer<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>sender state<span style="color: #002200;">&#93;</span> <span style="color: #002200;">==</span> UIGestureRecognizerStateBegan<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
&nbsp;
		firstX <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>sender view<span style="color: #002200;">&#93;</span> center<span style="color: #002200;">&#93;</span>.x;
		firstY <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>sender view<span style="color: #002200;">&#93;</span> center<span style="color: #002200;">&#93;</span>.y;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	translatedPoint <span style="color: #002200;">=</span> CGPointMake<span style="color: #002200;">&#40;</span>firstX<span style="color: #002200;">+</span>translatedPoint.x, firstY<span style="color: #002200;">+</span>translatedPoint.y<span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>sender view<span style="color: #002200;">&#93;</span> setCenter<span style="color: #002200;">:</span>translatedPoint<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;">&#40;</span>UIPanGestureRecognizer<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>sender state<span style="color: #002200;">&#93;</span> <span style="color: #002200;">==</span> UIGestureRecognizerStateEnded<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
&nbsp;
		CGFloat finalX <span style="color: #002200;">=</span> translatedPoint.x <span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span>.35<span style="color: #002200;">*</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#40;</span>UIPanGestureRecognizer<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>sender velocityInView<span style="color: #002200;">:</span>self.view<span style="color: #002200;">&#93;</span>.x<span style="color: #002200;">&#41;</span>;
		CGFloat finalY <span style="color: #002200;">=</span> translatedPoint.y <span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span>.35<span style="color: #002200;">*</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#40;</span>UIPanGestureRecognizer<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>sender velocityInView<span style="color: #002200;">:</span>self.view<span style="color: #002200;">&#93;</span>.y<span style="color: #002200;">&#41;</span>;
&nbsp;
		<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>UIDeviceOrientationIsPortrait<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIDevice currentDevice<span style="color: #002200;">&#93;</span> orientation<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span><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>finalX <span style="color: #002200;">&amp;</span>lt; <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span> 				 				finalX <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>; 			<span style="color: #002200;">&#125;</span> 			 			<span style="color: #a61390;">else</span> <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>finalX <span style="color: #002200;">&amp;</span>gt; <span style="color: #2400d9;">768</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
&nbsp;
				finalX <span style="color: #002200;">=</span> <span style="color: #2400d9;">768</span>;
			<span style="color: #002200;">&#125;</span>
&nbsp;
			<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>finalY <span style="color: #002200;">&amp;</span>lt; <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span> 				 				finalY <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>; 			<span style="color: #002200;">&#125;</span> 			 			<span style="color: #a61390;">else</span> <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>finalY <span style="color: #002200;">&amp;</span>gt; <span style="color: #2400d9;">1024</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
&nbsp;
				finalY <span style="color: #002200;">=</span> <span style="color: #2400d9;">1024</span>;
			<span style="color: #002200;">&#125;</span>
		<span style="color: #002200;">&#125;</span>
&nbsp;
		<span style="color: #a61390;">else</span> <span style="color: #002200;">&#123;</span>
&nbsp;
			<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>finalX <span style="color: #002200;">&amp;</span>lt; <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span> 				 				finalX <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>; 			<span style="color: #002200;">&#125;</span> 			 			<span style="color: #a61390;">else</span> <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>finalX <span style="color: #002200;">&amp;</span>gt; <span style="color: #2400d9;">1024</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
&nbsp;
				finalX <span style="color: #002200;">=</span> <span style="color: #2400d9;">768</span>;
			<span style="color: #002200;">&#125;</span>
&nbsp;
			<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>finalY <span style="color: #002200;">&amp;</span>lt; <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span> 				 				finalY <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>; 			<span style="color: #002200;">&#125;</span> 			 			<span style="color: #a61390;">else</span> <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>finalY <span style="color: #002200;">&amp;</span>gt; <span style="color: #2400d9;">768</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
&nbsp;
				finalY <span style="color: #002200;">=</span> <span style="color: #2400d9;">1024</span>;
			<span style="color: #002200;">&#125;</span>
		<span style="color: #002200;">&#125;</span>
&nbsp;
		<span style="color: #002200;">&#91;</span>UIView beginAnimations<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span> context<span style="color: #002200;">:</span><span style="color: #a61390;">NULL</span><span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span>UIView setAnimationDuration<span style="color: #002200;">:</span>.35<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span>UIView setAnimationCurve<span style="color: #002200;">:</span>UIViewAnimationCurveEaseOut<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>sender view<span style="color: #002200;">&#93;</span> setCenter<span style="color: #002200;">:</span>CGPointMake<span style="color: #002200;">&#40;</span>finalX, finalY<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span>UIView commitAnimations<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<h2>Implementing Momentum</h2>
<p>The final half of the above method is for us to calculate the momentum that the object will have after being moved. This will make the object appear as if it is being thrown across a table and slowly coming to stop. In order to do this we utilize UIPanGestureRecognizers velocityInView method which will tell us the velocity of the pan touch within a provided view. With this we can do an easy position calculation for both the x and y coordinates of our object. To do this we must provide an input for time, in this case .4 seconds. While this is not truly momentum and friction based physics it provides a nice affect for our interaction. With out final resting place calculated we ensure that where the object ends up will still be within the visible surface of the iPad by checking against the bounds of the screen depending on the current orientation. The final step is to animate the view moving to this final location over the same .4 second time period.</p>
<h2>Implementing Taps</h2>
<p>We have one final gesture recognizer implementation to do and that is the tap: method. This method will be used when a user taps an object that is in the midsts of sliding after being moved. We essentially want to stop the movement mid slide. In order to do that we are going to tell the CALayer layer property of our view to cancel all current animations. The short piece of code can be seen 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>tapped<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><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#40;</span>UITapGestureRecognizer<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>sender view<span style="color: #002200;">&#93;</span> layer<span style="color: #002200;">&#93;</span> removeAllAnimations<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<h2>Implementing UIGestureDelegate</h2>
<p>If you run the code now you will be able to perform all of the gestures described within this document but you will notice that you are not able to do several at the same time. For instance you can not pinch zoom and rotate a view at the same time. This is because we still need to implement the UIGestureRecognizerDelegate method gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer. We want to make sure that any gesture recognizers can happen together except for the pan gesture recognizer. To do this we simply check that it is not a UIPanGestureRecognizerClass and return true in that case. See the short code 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;">BOOL</span><span style="color: #002200;">&#41;</span>gestureRecognizer<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIGestureRecognizer <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIGestureRecognizer <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>otherGestureRecognizer <span style="color: #002200;">&#123;</span>
&nbsp;
	<span style="color: #a61390;">return</span> <span style="color: #002200;">!</span><span style="color: #002200;">&#91;</span>gestureRecognizer isKindOfClass<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>UIPanGestureRecognizer class<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<h2>GIT Hub</h2>
<p>You can find this project <a title="Demo Paste Board Application on GITHub" href="http://github.com/elc/iCodeBlogDemoPhotoBoard" target="_blank">now on GitHub</a>. Please let me know any issues you may have. Happy coding!</p>
<p>Follow me on Twitter <a href="http://twitter.com/cruffenach" target="_blank">@cruffenach</a></p>
]]></content:encoded>
			<wfw:commentRss>http://icodeblog.com/2010/10/14/working-with-uigesturerecognizers/feed/</wfw:commentRss>
		<slash:comments>48</slash:comments>
		</item>
		<item>
		<title>Cloning UIImagePickerController using the Assets Library Framework</title>
		<link>http://icodeblog.com/2010/10/07/cloning-uiimagepickercontroller-using-the-assets-library-framework/</link>
		<comments>http://icodeblog.com/2010/10/07/cloning-uiimagepickercontroller-using-the-assets-library-framework/#comments</comments>
		<pubDate>Thu, 07 Oct 2010 22:51:05 +0000</pubDate>
		<dc:creator><![CDATA[Collin]]></dc:creator>
				<category><![CDATA[Featured]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Advanced]]></category>
		<category><![CDATA[Custom UITableViewCells]]></category>
		<category><![CDATA[ELCImagePickerController]]></category>
		<category><![CDATA[nsoperation]]></category>
		<category><![CDATA[UIGestureRecognizers]]></category>
		<category><![CDATA[UIImagePickerController]]></category>
		<category><![CDATA[uitableview]]></category>

		<guid isPermaLink="false">http://icodeblog.com/?p=2338</guid>
		<description><![CDATA[Hello iCoders. This is a follow up post to my initial post on the <a title="The Assets Library and Blocks in iOS 4.0" href="/2010/07/08/asset-libraries-and-blocks-in-ios-4/" target="_blank">Assets Library Framework and Blocks</a>. We came across an interesting problem when working on the application for <a href="http://animoto.com">Animoto.com</a>. They have had an <a title="Animoto in the App Store" href="http://itunes.apple.com/us/app/animoto-videos/id300033126?mt=8" target="_blank">app in the store</a> since the very early days of the app store, and one of our biggest struggles has been creating an interface to allow  ...]]></description>
				<content:encoded><![CDATA[<p>Hello iCoders. This is a follow up post to my initial post on the <a title="The Assets Library and Blocks in iOS 4.0" href="/2010/07/08/asset-libraries-and-blocks-in-ios-4/" target="_blank">Assets Library Framework and Blocks</a>. We came across an interesting problem when working on the application for <a href="http://animoto.com">Animoto.com</a>. They have had an <a title="Animoto in the App Store" href="http://itunes.apple.com/us/app/animoto-videos/id300033126?mt=8" target="_blank">app in the store</a> since the very early days of the app store, and one of our biggest struggles has been creating an interface to allow users to select multiple photos from their photo album for a slideshow. While the iPhone photos application allows for this, the UIImagePicker does not. With the release of the Assets Library framework we can now recreate the experience of the UIImagePicker, with some additional custom functionality to fit our needs. As a result we created a new cloned version of the UIImagePicker that allows for multiple photo selection just like the photos app. We have decided to open source the controller for other developers to use. This post will explain the process of creating the new image picker as well as the method of incorporating it into your code.</p>
<h2>The ELCImagePickerController</h2>
<p>The ELCImagePickerController is only possible because of the newly introduced Assets Library framework. This framework gives you raw (more or less) access to the photo and video elements of a user. We looked at UIImagePickerController and saw a lot of weaknesses with it. You can only select 1 photo at a time, and even in Apple&#8217;s photo app, where you can choose several pictures at a time, you can&#8217;t use multi touch to do your selection. To solve these problems we rolled our own solution that works very closely to UIImagePickerController.</p>
<h3>How to use it</h3>
<p>First I am going to explain using the picker since to many people the process of creating it won&#8217;t be very interesting. The image picker is created and displayed in a very similar manner to the UIImagePickerController. The sample application that is part of the GitHub project, where I distribute the controller, shows its use, but I will go into detail here. To display the controller you instantiate it and display it modally like so.</p>
<pre>ELCImagePickerController *controller = [[ELCImagePickerController alloc] initImagePicker];
[controller setDelegate:self];
[self presentModalViewController:controller animated:YES];
[controller release];</pre>
<p>The ELCImagePickerController will return the select images back to the ELCImagePickerControllerDelegate. The delegate contains to methods very similar to the UIImagePickerControllerDelegate. Instead of returning one dictionary representing a single image the controller sends back an array of similarly structured dictionaries. The two delegate methods are:]</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>elcImagePickerController<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>ELCImagePickerController <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>picker didFinishPickingMediaWithInfo<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>info;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>elcImagePickerControllerDidCancel<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>ELCImagePickerController <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>picker;</pre></td></tr></table></div>

<h2>GIT Hub</h2>
<p>You can find this project <a title="ELCImagePickerController on GITHub" href="http://github.com/elc/ELCImagePickerController" target="_blank">now on GitHub</a>. Please let me know any issues you may have and look for future releases with feature enhancements</p>
<h2>General Structure</h2>
<p style="text-align: center;"><a href="/wp-content/uploads/2010/10/ELCImagePickerControllerArchitecture1.png"><img class="aligncenter size-full wp-image-2444" title="ELCImagePickerControllerArchitecture" src="/wp-content/uploads/2010/10/ELCImagePickerControllerArchitecture1.png" alt="" width="526" height="306" /></a></p>
<p style="text-align: left;">The ELCImagePicker is a collection of UITableViewControllers that are placed inside a UINavigationController. While the ELCImagePickerController is actually 5 separate custom classes I have written, I have put them all within a single header and main. I chose this to make the classes that were required to be imported into a project when using this as few as possible.  While usually when presenting a UINavigationController you would create one yourself in code and use the initWithRootViewController method, in this case we have created a UINavigationController subclass called ELCImagePickerController which does all this behind the scenes. In the end all a developer has to do is use the initImagePicker method and present the controller modally. This lets the class match the functionality of UIImagePickerController closer. You can see the Header and Main for the ELCImagePickerController class on the next page.</p>
<p><a href="http://vimeo.com/15666311">ELCImagePickerControllerDemo</a> from <a href="http://vimeo.com/user2008025">Collin Ruffenach</a> on <a href="http://vimeo.com">Vimeo</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://icodeblog.com/2010/10/07/cloning-uiimagepickercontroller-using-the-assets-library-framework/feed/</wfw:commentRss>
		<slash:comments>49</slash:comments>
		</item>
		<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>
	</channel>
</rss>
