<?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>[iPhoneiOS dev:tips]; &#187; Networking</title>
	<atom:link href="http://iPhoneDeveloperTips.com/category/networking/feed" rel="self" type="application/rss+xml" />
	<link>http://iPhoneDeveloperTips.com</link>
	<description>Tips and Tricks for iPhone developers</description>
	<lastBuildDate>Thu, 29 Jul 2010 17:59:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Post to a Twitter Account from the iPhone</title>
		<link>http://iPhoneDeveloperTips.com/networking/post-to-a-twitter-account-from-the-iphone.html</link>
		<comments>http://iPhoneDeveloperTips.com/networking/post-to-a-twitter-account-from-the-iphone.html#comments</comments>
		<pubDate>Mon, 16 Nov 2009 02:04:27 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Networking]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=4522</guid>
		<description><![CDATA[This is the third tip in a series of four that is based on content from the O&#8217;Reilly book Head First iPhone Development. This tip is from a code sample in Chapter 3 &#8211; Objective-C for the iPhone, and shows how to post to a Twitter account. Twitter URL Request The first step is to [...]]]></description>
			<content:encoded><![CDATA[<p>This is the third tip in a series of four that is based on content from the O&#8217;Reilly book <a  target="_blank"  href="http://iphonedevelopertips.com/?wp_ct=12">Head First iPhone Development</a>. This tip is from a code sample in Chapter 3 &#8211; Objective-C for the iPhone, and shows how to post to a Twitter account.<br />
<span id="more-4522"></span></p>
<h5>Twitter URL Request</h5>
<p>The first step is to setup the appropriate URL to post to a twitter account, replace your twitter account and password for the placeholder values.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSMutableURLRequest</span> <span style="color: #002200;">*</span>request <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableURLRequest</span> requestWithURL<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://TWITTER_ACCOUNT:PASSWORD@twitter.com/statuses/update.xml&quot;</span><span style="color: #002200;">&#93;</span> 
  cachePolicy<span style="color: #002200;">:</span>NSURLRequestUseProtocolCachePolicy timeoutInterval<span style="color: #002200;">:</span><span style="color: #2400d9;">30.0</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

<h5>Data Related Setup</h5>
<p>With the URL ready, we need to setup the HTTP request types as well as the text to twit:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// The text to post</span>
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>msg <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;testing&quot;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Set the HTTP request method</span>
<span style="color: #002200;">&#91;</span>request setHTTPMethod<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;POST&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #002200;">&#91;</span>request setHTTPBody<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</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;status=%@&quot;</span>, msg<span style="color: #002200;">&#93;</span> 
    dataUsingEncoding<span style="color: #002200;">:</span>NSASCIIStringEncoding<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #400080;">NSURLResponse</span> <span style="color: #002200;">*</span>response;
<span style="color: #400080;">NSError</span> <span style="color: #002200;">*</span>error;
&nbsp;
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSURLConnection</span> sendSynchronousRequest<span style="color: #002200;">:</span>request 
    returningResponse<span style="color: #002200;">:&amp;</span>response error<span style="color: #002200;">:&amp;</span>error<span style="color: #002200;">&#93;</span> <span style="color: #002200;">!=</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span>
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Posted to Twitter successfully.&quot;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #a61390;">else</span> 
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Error posting to Twitter.&quot;</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<h5>Asynchronous Requests</h5>
<p>Notice in the code above that the URL connection request is sent synchronously, so the UI will block waiting for this to return. One relatively straightforward option is to wrap the above code inside a method, and call that method in a background thread.</p>
<p>For example, in the example below I&#8217;ve added the code into a method named postToTwitter, and requested this method to run in a new thread. The example is a little contrived as you wouldn&#8217;t hard-code the text to post to Twitter, however, you get the idea.</p>

<div class="wp_syntax"><div 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> postToTwitter
<span style="color: #002200;">&#123;</span>
  <span style="color: #11740a; font-style: italic;">// Since this will be launched in a separate thread, we need</span>
  <span style="color: #11740a; font-style: italic;">// an autorelease pool</span>
  <span style="color: #400080;">NSAutoreleasePool</span> <span style="color: #002200;">*</span>pool <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSAutoreleasePool</span> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #400080;">NSMutableURLRequest</span> <span style="color: #002200;">*</span>request <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableURLRequest</span> requestWithURL<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://TWITTER_ACCOUNT:PASSWORD@twitter.com/statuses/update.xml&quot;</span><span style="color: #002200;">&#93;</span> 
    cachePolicy<span style="color: #002200;">:</span>NSURLRequestUseProtocolCachePolicy timeoutInterval<span style="color: #002200;">:</span><span style="color: #2400d9;">30.0</span><span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// The text to post</span>
  <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>msg <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;testing&quot;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Set the HTTP request method</span>
  <span style="color: #002200;">&#91;</span>request setHTTPMethod<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;POST&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #002200;">&#91;</span>request setHTTPBody<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</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;status=%@&quot;</span>, msg<span style="color: #002200;">&#93;</span> 
    dataUsingEncoding<span style="color: #002200;">:</span>NSASCIIStringEncoding<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #400080;">NSURLResponse</span> <span style="color: #002200;">*</span>response;
  <span style="color: #400080;">NSError</span> <span style="color: #002200;">*</span>error;
&nbsp;
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSURLConnection</span> sendSynchronousRequest<span style="color: #002200;">:</span>request 
      returningResponse<span style="color: #002200;">:&amp;</span>response error<span style="color: #002200;">:&amp;</span>error<span style="color: #002200;">&#93;</span> <span style="color: #002200;">!=</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span>
    NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Posted to Twitter successfully.&quot;</span><span style="color: #002200;">&#41;</span>;
  <span style="color: #a61390;">else</span> 
    NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Error posting to Twitter.&quot;</span><span style="color: #002200;">&#41;</span>; 
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Release pool</span>
  <span style="color: #002200;">&#91;</span>pool release<span style="color: #002200;">&#93;</span>; 
<span style="color: #002200;">&#125;</span>    
&nbsp;
...
&nbsp;
<span style="color: #002200;">&#91;</span><span style="color: #400080;">NSThread</span> detachNewThreadSelector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>postToTwitter<span style="color: #002200;">&#41;</span>
   toTarget<span style="color: #002200;">:</span>self withObject<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/networking/post-to-a-twitter-account-from-the-iphone.html/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>iPhone JSON Flickr Tutorial – Part 3</title>
		<link>http://iPhoneDeveloperTips.com/networking/iphone-json-flickr-tutorial-part-3.html</link>
		<comments>http://iPhoneDeveloperTips.com/networking/iphone-json-flickr-tutorial-part-3.html#comments</comments>
		<pubDate>Thu, 17 Sep 2009 12:03:11 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Networking]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=3904</guid>
		<description><![CDATA[Welcome to the third and final post on working with the iPhone SDK, JSON and the Flickr API. In I Part 1 we setup a JSON library, created a Flickr API key and wrote code to retrieve images and the titles from Flickr. In Part 2 of the series we dove into the how to [...]]]></description>
			<content:encoded><![CDATA[<p>Welcome to the third and final post on working with the iPhone SDK, JSON and the Flickr API. In I <a  target="_blank"  href="http://iphonedevelopertips.com/networking/uber-iphone-json-flickr-tutorial-part-1.html">Part 1</a> we setup a JSON library, created a Flickr API key and wrote code to retrieve images and the titles from Flickr.</p>
<p>In <a  target="_blank"  href="http://iphonedevelopertips.com/networking/iphone-json-flickr-tutorial-part-2.html">Part 2 of the series</a> we dove into the how to quickly parse information returned from a JSON service using a <a  target="_blank"  href="http://code.google.com/p/json-framework/downloads/list">JSON framework</a>. We also created a table to display images returned from Flickr.</p>
<p>In this tutorial we wrap up by adding a simple image viewer that will display a larger variation of the image when the user taps on a row in the table. We&#8217;ll add some code to slide the larger images on/off screen, which is a nice visual effect for this application. You can review the finished application by watching the video below:<br />
<span id="more-3904"></span></p>
<p><center><br />
<embed src="http://iphonedevelopertips.com/wp-content/uploads/2009/08/iPhoneJSONFlickr.mov" width="256" height="405" autoplay="false"><br />
</center><br />
</p>
<h5>Download iPhone JSON Xcode Project</h5>
<p>You can download the Xcode project and walk through the source code:</p>
<blockquote>
<ol>
<li>Download <a href="http://iphonedevelopertips.com/wp-content/uploads/2009/09/iPhone-JSON-Flickr-Part3.zip">iPhone, JSON and Flickr &#8211; Part 3</a> Xcode project</li>
</ol>
</blockquote>
<h5>Zoomed Image Viewer</h5>
<p>Let&#8217;s begin by creating a simple class for viewing the larger variation of the image in the table. The class definition follows:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> ZoomedImageView <span style="color: #002200;">:</span> UIView
<span style="color: #002200;">&#123;</span>
  UIImageView <span style="color: #002200;">*</span>fullsizeImage;
<span style="color: #002200;">&#125;</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>initWithURL<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;
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<p>Not much here &#8211; just a <strong>UIView</strong> with an <strong>UIImageView</strong> to hold the large image downloaded from Flickr. Let&#8217;s look at the implementation of this class.</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
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &quot;ZoomedImageView.h&quot;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Private interface definitions
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/</span>
<span style="color: #a61390;">@interface</span> ZoomedImageView<span style="color: #002200;">&#40;</span>private<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>slideViewOffScreen;
<span style="color: #a61390;">@end</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> ZoomedImageView
&nbsp;
<span style="color: #11740a; font-style: italic;">/**************************************************************************
*
* Private implementation section
*
**************************************************************************/</span>
&nbsp;
<span style="color: #6e371a;">#pragma mark -</span>
<span style="color: #6e371a;">#pragma mark Private 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>slideViewOffScreen
<span style="color: #002200;">&#123;</span>
  <span style="color: #11740a; font-style: italic;">// Get the frame of this view</span>
  CGRect frame <span style="color: #002200;">=</span> self.frame;
&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>.45<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Set view to this offscreen location</span>
  frame.origin.x <span style="color: #002200;">=</span> <span style="color: #002200;">-</span><span style="color: #2400d9;">320</span>;
  self.frame <span style="color: #002200;">=</span> frame;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Slide view</span>
  <span style="color: #002200;">&#91;</span>UIView commitAnimations<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">/**************************************************************************
*
* Class implementation section
*
**************************************************************************/</span>
&nbsp;
<span style="color: #6e371a;">#pragma mark -</span>
<span style="color: #6e371a;">#pragma mark Initialization</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>initWithURL<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
<span style="color: #002200;">&#123;</span>
  <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>
    <span style="color: #11740a; font-style: italic;">// Create the view offscreen (to the right)</span>
    self.frame <span style="color: #002200;">=</span> CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">320</span>, <span style="color: #2400d9;">0</span>, <span style="color: #2400d9;">320</span>, <span style="color: #2400d9;">240</span><span style="color: #002200;">&#41;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Setup image</span>
    <span style="color: #400080;">NSData</span> <span style="color: #002200;">*</span>imageData <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSData</span> dataWithContentsOfURL<span style="color: #002200;">:</span>url<span style="color: #002200;">&#93;</span>;
    fullsizeImage <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> initWithImage<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>UIImage imageWithData<span style="color: #002200;">:</span>imageData<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Center the image...</span>
    <span style="color: #a61390;">int</span> width <span style="color: #002200;">=</span> fullsizeImage.frame.size.width;
    <span style="color: #a61390;">int</span> height <span style="color: #002200;">=</span> fullsizeImage.frame.size.height; 
&nbsp;
    <span style="color: #a61390;">int</span> x <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span><span style="color: #2400d9;">320</span> <span style="color: #002200;">-</span> width<span style="color: #002200;">&#41;</span> <span style="color: #002200;">/</span> <span style="color: #2400d9;">2</span>;
    <span style="color: #a61390;">int</span> y <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span><span style="color: #2400d9;">240</span> <span style="color: #002200;">-</span> height<span style="color: #002200;">&#41;</span> <span style="color: #002200;">/</span> <span style="color: #2400d9;">2</span>;
&nbsp;
    <span style="color: #002200;">&#91;</span>fullsizeImage setFrame<span style="color: #002200;">:</span>CGRectMake<span style="color: #002200;">&#40;</span>x, y, width, height<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;     
    fullsizeImage.userInteractionEnabled <span style="color: #002200;">=</span> <span style="color: #a61390;">YES</span>;                                      
    <span style="color: #002200;">&#91;</span>self addSubview<span style="color: #002200;">:</span>fullsizeImage<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #002200;">&#125;</span>
&nbsp;
  <span style="color: #a61390;">return</span> self;  
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #6e371a;">#pragma mark -</span>
<span style="color: #6e371a;">#pragma mark Event Mgmt</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>touchesBegan<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSSet</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>touches withEvent<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIEvent <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>event 
<span style="color: #002200;">&#123;</span>
  <span style="color: #002200;">&#91;</span>self slideViewOffScreen<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// We now send the same event up to the next responder</span>
  <span style="color: #11740a; font-style: italic;">// (the JSONFlickrViewController) so we can show enable</span>
  <span style="color: #11740a; font-style: italic;">// the search textfield again</span>
  <span style="color: #002200;">&#91;</span>self.nextResponder touchesBegan<span style="color: #002200;">:</span>touches withEvent<span style="color: #002200;">:</span>event<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #6e371a;">#pragma mark -</span>
<span style="color: #6e371a;">#pragma mark Cleanup</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>dealloc 
<span style="color: #002200;">&#123;</span>
  <span style="color: #002200;">&#91;</span>fullsizeImage 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>
&nbsp;
<span style="color: #a61390;">@end</span></pre></td></tr></table></div>

<p>It starts the the initialization code on line 46 &#8211; we pass in the URL of the larger image that we saved previously when making the original request for images matching a search string (more on that below). </p>
<p>Notice the x coordinate on line 51 has a value of 320, which sets this view off-screen to the right. As we&#8217;ll see momentarily, to accomplish the slide-in effect we&#8217;ll move the x coordinate to an on-screen location, which will slide the image into place. We wrap up the initialization code by creating the image, centering the image in the view, and adding the image as a subview.</p>
<p>Once an image is shown on screen, when a new image is requested by the user (tapping on a table row) we need to slide the current view off the screen to the left. The code beginning on line 21 shows how we accomplish this &#8211; setup the animation, move the frame for the current image off-screen to the left (-320) and commit the animation.</p>
<h5>Search TextField and Activity Indicator</h5>
<p>Our next task is to create the search textfield. Head over to the <strong>JSONFlickrViewController</strong> implementation file, where we need to add a <strong>UITextField</strong>, also add an activity indicator as shown here:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> JSONFlickrViewController <span style="color: #002200;">:</span> UIViewController &lt;UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate&gt;
<span style="color: #002200;">&#123;</span>
  UITextField     <span style="color: #002200;">*</span>searchTextField;
  UITableView     <span style="color: #002200;">*</span>theTableView;
  <span style="color: #400080;">NSMutableArray</span>  <span style="color: #002200;">*</span>photoTitles;         <span style="color: #11740a; font-style: italic;">// Titles of images</span>
  <span style="color: #400080;">NSMutableArray</span>  <span style="color: #002200;">*</span>photoSmallImageData; <span style="color: #11740a; font-style: italic;">// Image data (thumbnail)</span>
  <span style="color: #400080;">NSMutableArray</span>  <span style="color: #002200;">*</span>photoURLsLargeImage; <span style="color: #11740a; font-style: italic;">// URL to larger image</span>
&nbsp;
  ZoomedImageView  <span style="color: #002200;">*</span>fullImageViewController;
  UIActivityIndicatorView <span style="color: #002200;">*</span>activityIndicator;      
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>The code for initializing the textfield and activity indicator are inside the init method of the <strong>JSONFlickrViewController</strong> class:</p>

<div class="wp_syntax"><div 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>init
&nbsp;
   ...
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Create textfield for the search text</span>
    searchTextField <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UITextField alloc<span style="color: #002200;">&#93;</span> initWithFrame<span style="color: #002200;">:</span>CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">110</span>, <span style="color: #2400d9;">100</span>, <span style="color: #2400d9;">100</span>, <span style="color: #2400d9;">40</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span> retain<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>searchTextField setBorderStyle<span style="color: #002200;">:</span>UITextBorderStyleRoundedRect<span style="color: #002200;">&#93;</span>;
    searchTextField.placeholder <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;search&quot;</span>;
    searchTextField.returnKeyType <span style="color: #002200;">=</span> UIReturnKeyDone;
    searchTextField.clearButtonMode <span style="color: #002200;">=</span> UITextFieldViewModeWhileEditing;
    searchTextField.delegate <span style="color: #002200;">=</span> self;
    <span style="color: #002200;">&#91;</span>self.view addSubview<span style="color: #002200;">:</span>searchTextField<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>searchTextField release<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Create activity indicator</span>
    activityIndicator <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIActivityIndicatorView alloc<span style="color: #002200;">&#93;</span> initWithFrame<span style="color: #002200;">:</span>CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">220</span>, <span style="color: #2400d9;">110</span>, <span style="color: #2400d9;">15</span>, <span style="color: #2400d9;">15</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
    activityIndicator.activityIndicatorViewStyle <span style="color: #002200;">=</span> UIActivityIndicatorViewStyleWhite;
    activityIndicator.autoresizingMask <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>UIViewAutoresizingFlexibleLeftMargin |
                      UIViewAutoresizingFlexibleRightMargin |
                      UIViewAutoresizingFlexibleTopMargin |
                      UIViewAutoresizingFlexibleBottomMargin<span style="color: #002200;">&#41;</span>;
    <span style="color: #002200;">&#91;</span>	activityIndicator sizeToFit<span style="color: #002200;">&#93;</span>;
    activityIndicator.hidesWhenStopped <span style="color: #002200;">=</span> <span style="color: #a61390;">YES</span>; 
    <span style="color: #002200;">&#91;</span>self.view addSubview<span style="color: #002200;">:</span>activityIndicator<span style="color: #002200;">&#93;</span>;
&nbsp;
  ...
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>When new search text is entered, the code in the method <strong>textFieldShouldReturn</strong> will be run. Inside this method we remove all the objects from each of the arrays we created previously when calling Flickr (if we&#8217;ve called Flickr previously) and start the activity indicator. </p>
<p>Next, call the method <strong>searchFlickrPhotos</strong> which will kick-off the process for calling the Flickr API and rebuilding the arrays of content based on the JSON returned from the service &#8211; See <a  target="_blank"  href="http://iphonedevelopertips.com/networking/uber-iphone-json-flickr-tutorial-part-1.htm">Part 1 for more information</a>. Once the query is complete, the activity indicator will be stopped (you can see this code in the downloaded Xcode project).</p>

<div class="wp_syntax"><div 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>textFieldShouldReturn<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UITextField <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>textField
<span style="color: #002200;">&#123;</span>
  <span style="color: #002200;">&#91;</span>textField resignFirstResponder<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Remove any content from a previous search</span>
  <span style="color: #002200;">&#91;</span>photoTitles removeAllObjects<span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>photoSmallImageData removeAllObjects<span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>photoURLsLargeImage removeAllObjects<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Begin the call to Flickr</span>
  <span style="color: #002200;">&#91;</span>self searchFlickrPhotos<span style="color: #002200;">:</span>searchTextField.text<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Start the busy indicator</span>
  <span style="color: #002200;">&#91;</span>activityIndicator startAnimating<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #a61390;">return</span> <span style="color: #a61390;">YES</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<h5>Wrapping all the Pieces Together</h5>
<p>At this point all that&#8217;s left is to recognize when a new table row has been selected and begin the process for downloading the larger image view and sliding the image into place on the display. It all begins in the code for managing the table with the method shown below:</p>

<div class="wp_syntax"><div 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>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 didSelectRowAtIndexPath<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>
  searchTextField.hidden <span style="color: #002200;">=</span> <span style="color: #a61390;">YES</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// If we've created this VC before...</span>
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>fullImageViewController <span style="color: #002200;">!=</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span>
  <span style="color: #002200;">&#123;</span>
    <span style="color: #11740a; font-style: italic;">// Slide this view off screen</span>
    CGRect frame <span style="color: #002200;">=</span> fullImageViewController.frame;
&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>.45<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Off screen location</span>
    frame.origin.x <span style="color: #002200;">=</span> <span style="color: #002200;">-</span><span style="color: #2400d9;">320</span>;
    fullImageViewController.frame <span style="color: #002200;">=</span> frame;
&nbsp;
    <span style="color: #002200;">&#91;</span>UIView commitAnimations<span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#125;</span>
&nbsp;
  <span style="color: #002200;">&#91;</span>self performSelector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>showZoomedImage<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span> withObject<span style="color: #002200;">:</span>indexPath afterDelay<span style="color: #002200;">:</span><span style="color: #2400d9;">0.1</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>We begin by hiding the search textfield, then, if a zoomed image is visible, we need to slide it off-screen &#8211; we do this by setting up the animation, move the image location (frame) to an off-screen location (-320) and committing the animation. Once that is complete, we call <strong>showZoomedImage</strong> to display the image the user requested.</p>
<p>The last chunk of code is for displaying the requested image:</p>

<div class="wp_syntax"><div 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>showZoomedImage<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>
  <span style="color: #11740a; font-style: italic;">// Remove from view (and release)</span>
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>fullImageViewController superview<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>
  	<span style="color: #002200;">&#91;</span>fullImageViewController removeFromSuperview<span style="color: #002200;">&#93;</span>;
&nbsp;
  fullImageViewController <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>ZoomedImageView alloc<span style="color: #002200;">&#93;</span> initWithURL<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>photoURLsLargeImage objectAtIndex<span style="color: #002200;">:</span>indexPath.row<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #002200;">&#91;</span>self.view addSubview<span style="color: #002200;">:</span>fullImageViewController<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Slide this view off screen</span>
  CGRect frame <span style="color: #002200;">=</span> fullImageViewController.frame;
&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>.45<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Slide the image to its new location (onscreen)</span>
  frame.origin.x <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;
  fullImageViewController.frame <span style="color: #002200;">=</span> frame;
&nbsp;
  <span style="color: #002200;">&#91;</span>UIView commitAnimations<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>We start by releasing the current zoomed view (if there is one). Next, we create a new <strong>ZoomedImageView</strong>, passing in the URL of the larger image that was saved previously when we parsed the JSON data (see the previous posts). We setup the animation, move x coordinate of the image into place and commit the animation.</p>
<h5>Final Notes</h5>
<p>With that, we have a complete working application that will call Flickr based on a user requested search string, display a table of images and titles and allow zooming in on each image by selecting a row in the table.</p>
<p>It&#8217;s hard to get the big picture when looking at code snippets as shown in this tutorial. Once you download and tinker with the Xcode project, it&#8217;ll all make sense.</p>
<h5>Summary of Links</h5>
<blockquote>
<ol>
<li><a   target="_blank"  href="http://iphonedevelopertips.com/networking/uber-iphone-json-flickr-tutorial-part-1.html">iPhone, JSON and Flickr Tutorial &#8211; Part 1</a></li>
<li><a   target="_blank"  href="http://iphonedevelopertips.com/networking/iphone-json-flickr-tutorial-part-2.html">iPhone, JSON and Flickr Tutorial &#8211; Part 2</a></li>
<li><a   target="_blank"  href="http://iphonedevelopertips.com/wp-content/uploads/2009/09/iPhone-JSON-Flickr-Part3.zip">Download iPhone, JSON and Flickr &#8211; Part 3 Xcode Project</a></li>
<li><a  target="_blank"  href="http://www.flickr.com/services/api/keys/apply/">Get Flickr API key</a></li>
<li><a  target="_blank"  href="http://www.flickr.com/services/api/">Flickr API documentation</a></li>
<li><a  target="_blank"  href="http://code.google.com/p/json-framework/downloads/list">Download JSON framework</a> </li>
<li><a  target="_blank"  href="http://groups.google.com/group/json-framework">JSON Framework Google Group</a> </li>
<li><a  target="_blank"  href="http://devblog.brautaset.org/2009/05/31/json-framework-v2-2-released/">Stig Brautaset&#8217;s Blog</a> </li>
</ol>
</blockquote>
]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/networking/iphone-json-flickr-tutorial-part-3.html/feed</wfw:commentRss>
		<slash:comments>15</slash:comments>
<enclosure url="http://iphonedevelopertips.com/wp-content/uploads/2009/08/iPhoneJSONFlickr.mov" length="9127313" type="video/quicktime" />
		</item>
		<item>
		<title>iPhone JSON Flickr Tutorial – Part 2</title>
		<link>http://iPhoneDeveloperTips.com/networking/iphone-json-flickr-tutorial-part-2.html</link>
		<comments>http://iPhoneDeveloperTips.com/networking/iphone-json-flickr-tutorial-part-2.html#comments</comments>
		<pubDate>Tue, 08 Sep 2009 10:05:41 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Networking]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=3746</guid>
		<description><![CDATA[Welcome to the second tutorial in this three part series on using the iPhone SDK, JSON and Flickr. In the first tutorial we covered the basics for setting up the JSON framework within your projects, registering to get Flickr API key, and writing some code to asynchronously fetch images and their accompanying titles from Flickr. [...]]]></description>
			<content:encoded><![CDATA[<p>Welcome to the second tutorial in this three part series on using the iPhone SDK, JSON and Flickr. In the <a  target="_blank"  href="http://iphonedevelopertips.com/networking/uber-iphone-json-flickr-tutorial-part-1.html">first tutorial</a> we covered the basics for setting up the JSON framework within your projects, registering to get Flickr API key, and writing some code to asynchronously fetch images and their accompanying titles from Flickr.</p>
<p>The application created in Part 1 had no UI to speak of, the only output was debug information written directly to the console as a test case to verify we could successfully request and download images using JSON and Flickr.<br />
<span id="more-3746"></span></p>
<p>The video shown in Part 1 that highlights the finished application, developed over the course of these three tutorials, is displayed below if you need a refresher:</p>
<p><center><br />
<embed src="http://iphonedevelopertips.com/wp-content/uploads/2009/08/iPhoneJSONFlickr.mov" width="256" height="405" autoplay="false"><br />
</center><br />
</p>
<h5>Download iPhone JSON  Xcode Project File</h5>
<p>You may find it helpful to download the Xcode project and walk through the source code as you read this tutorial.</p>
<blockquote>
<ol>
<li>Download <a href="http://iphonedevelopertips.com/wp-content/uploads/2009/09/iPhone-JSON-Flickr-Part2.zip">iPhone, JSON and Flickr &#8211; Part 2</a> Xcode project</li>
</ol>
</blockquote>
<h5>Flickr JSON Data</h5>
<p>Let&#8217;s begin by taking a closer look at the data returned from Flickr and the resulting dictionary that the <a  target="_blank"  href="http://code.google.com/p/json-framework/downloads/list">JSON framework</a> creates for us.</p>
<p>The method that we wrote in Part 1 for receiving date from Flickr, <strong>didReceiveData</strong>, begins with the following two lines:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Store incoming data into a string</span>
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>jsonString <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> alloc<span style="color: #002200;">&#93;</span> initWithData<span style="color: #002200;">:</span>data encoding<span style="color: #002200;">:</span>NSUTF8StringEncoding<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Create a dictionary from the JSON string</span>
<span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span>results <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>jsonString JSONValue<span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

<p>Below is a screenshot of the <strong>jsonString</strong> (line 2 from above) as shown in the Xcode console. Everything you need is here, however, it would be a mess to parse this ourselves.</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2009/09/json1.png" /></p>
<p>With one line of code (on line 5 above) the JSON framework will convert the string format into a dictionary. The image below shows a formatted output of the JSON, which better describes the dictionary &#8211; <strong>photos</strong> is a dictionary, and looking down to the fifth entry in the list,  the key-value pair, with the key <strong>photo</strong> is an array of each photo returned from Flickr based on the search query we request. Each entry in the <strong>photo</strong> array is a dictionary as well, with keys &#8220;id&#8221;, &#8220;owner&#8221;, &#8220;secret&#8221;, &#8220;server&#8221;, etc. </p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2009/09/json2.png" /></p>
<p>With the help of the JSON framework and conversion to a dictionary, accessing the contents from Flickr becomes as easy as requesting key-value pairs. For example, to loop through the titles for each image we begin by creating a reference to the array of photos as listed here:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>photos <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>results objectForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;photos&quot;</span><span style="color: #002200;">&#93;</span> objectForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;photo&quot;</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>With the array in hand, we now access each entry in the array and create an <strong>NSString</strong> object from the key-value pair with the key &#8220;title&#8221;:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">for</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span>photo <span style="color: #a61390;">in</span> photos<span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
  <span style="color: #11740a; font-style: italic;">// Get title of the image</span>
  <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>title <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>photo objectForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;title&quot;</span><span style="color: #002200;">&#93;</span>;
  ...
<span style="color: #002200;">&#125;</span></pre></div></div>

<h5>Building Paths to the Images</h5>
<p>Let&#8217;s review how to build the path to images hosted on Flickr, based on values in the JSON return data. To access an image, the path looks like this: http://<strong>farmX</strong>.static.flickr.com/<strong>server</strong>/<strong>id</strong>_<strong>secret</strong>.jpg, where each of the bold entries in the path is mapped to one of the entries in the <strong>photos</strong> array (see the image above).</p>
<p>For example, in the return results shown below, note the values for <strong>farm</strong>, <strong>server</strong>, <strong>id</strong> and <strong>secret</strong>.</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2009/09/flickr1.png" /></p>
<p>Using the values above, we can now build a path to each image. For example, the path for the image data shown above looks as follows (screenshot of the Xcode console):</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2009/09/flickr2.png" /></p>
<h5>Define Table for Images and Titles</h5>
<p>Let&#8217;s add code to the previous project to store the images and title returned from Flickr into a table. The result will look as shown below:</p>
<p><img width="236" height="348" src="http://iPhoneDeveloperTips.com/wp-content/uploads/2009/09/json3.png" /></p>
<p>Begin by adding a tableview to <strong>JSONFlickrViewController</strong> to the implementation file <strong>JSONFlickrViewController.m</strong> as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">&nbsp;
<span style="color: #a61390;">@interface</span> JSONFlickrViewController <span style="color: #002200;">:</span> UIViewController &lt;UITableViewDelegate, UITableViewDataSource&gt;
<span style="color: #002200;">&#123;</span>
  UITableView     <span style="color: #002200;">*</span>theTableView;
  <span style="color: #400080;">NSMutableArray</span>  <span style="color: #002200;">*</span>photoTitles;         <span style="color: #11740a; font-style: italic;">// Titles of images</span>
  <span style="color: #400080;">NSMutableArray</span>  <span style="color: #002200;">*</span>photoSmallImageData; <span style="color: #11740a; font-style: italic;">// Image data (thumbnail)</span>
  <span style="color: #400080;">NSMutableArray</span>  <span style="color: #002200;">*</span>photoURLsLargeImage; <span style="color: #11740a; font-style: italic;">// URL to larger image</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>Now, inside the same file, we need to add code to initialize the table. In the <strong>init</strong> method, we&#8217;ll create the table as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">...
<span style="color: #11740a; font-style: italic;">// Create table view</span>
theTableView <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UITableView 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;">240</span>, <span style="color: #2400d9;">320</span>, <span style="color: #2400d9;">220</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>theTableView setDelegate<span style="color: #002200;">:</span>self<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>theTableView setDataSource<span style="color: #002200;">:</span>self<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>theTableView setRowHeight<span style="color: #002200;">:</span><span style="color: #2400d9;">80</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>theTableView setBackgroundColor<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>UIColor grayColor<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>theTableView setSeparatorStyle<span style="color: #002200;">:</span>UITableViewCellSeparatorStyleNone<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>self.view addSubview<span style="color: #002200;">:</span>theTableView<span style="color: #002200;">&#93;</span>;
...</pre></div></div>

<p>Notice the row height set to 80, as the Flickr thumbnail images are 75 x 75 pixels. The extra space allows some room around the image so it fits snuggly within the table row, yet has some visual padding. </p>
<h5>Populate Table with Flickr Content</h5>
<p>Next up is adding content to the table &#8211; we want an image on the left and its title text on the right. The code follows:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span>numberOfSectionsInTableView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UITableView <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>tableView 
<span style="color: #002200;">&#123;</span>
  <span style="color: #a61390;">return</span> <span style="color: #2400d9;">1</span>;
<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>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>
  <span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span>photoTitles 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>
  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><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;cachedCell&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>cell <span style="color: #002200;">==</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span>
    cell <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UITableViewCell alloc<span style="color: #002200;">&#93;</span> 
      initWithFrame<span style="color: #002200;">:</span>CGRectZero reuseIdentifier<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;cachedCell&quot;</span><span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #6e371a;">#if __IPHONE_3_0</span>
  cell.textLabel.text <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>photoTitles objectAtIndex<span style="color: #002200;">:</span>indexPath.row<span style="color: #002200;">&#93;</span>;
  cell.textLabel.font <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIFont systemFontOfSize<span style="color: #002200;">:</span><span style="color: #2400d9;">13.0</span><span style="color: #002200;">&#93;</span>;
<span style="color: #6e371a;">#else</span>
  cell.text <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>photoTitles objectAtIndex<span style="color: #002200;">:</span>indexPath.row<span style="color: #002200;">&#93;</span>;
  cell.font <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIFont systemFontOfSize<span style="color: #002200;">:</span><span style="color: #2400d9;">13.0</span><span style="color: #002200;">&#93;</span>;
<span style="color: #6e371a;">#endif</span>
&nbsp;
  <span style="color: #400080;">NSData</span> <span style="color: #002200;">*</span>imageData <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>photoSmallImageData objectAtIndex<span style="color: #002200;">:</span>indexPath.row<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #6e371a;">#if __IPHONE_3_0</span>
  cell.imageView.image <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIImage imageWithData<span style="color: #002200;">:</span>imageData<span style="color: #002200;">&#93;</span>;
<span style="color: #6e371a;">#else</span>
  cell.image <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIImage imageWithData<span style="color: #002200;">:</span>imageData<span style="color: #002200;">&#93;</span>;
<span style="color: #6e371a;">#endif</span>
&nbsp;
  <span style="color: #a61390;">return</span> cell;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>Most of this code is <code>straightforward</code> &#8211; there is one section in the table, the number of rows in that sole section is equal to the number of entries in the <strong>photoTitles</strong> array (which we built from the Flickr return data). </p>
<p>For each row in the table, we set the cell text by accessing the desired row from the <strong>photoTitles</strong> array. For the cell image we create a <strong>UIImage</strong> object from the <strong>photoSmallImageData</strong> array (again, from data we retreived from Flickr).</p>
<p>The last step is to make a call to populate the tableview once the data has been downloaded. Inside the method <strong>didReceiveData</strong>, at the bottom, once we&#8217;ve built all the internal arrays to hold the Flikr results, we make a request to reload the table data (see the project source code for the big picture of where this line of code lives):</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Update the table with data</span>
<span style="color: #002200;">&#91;</span>theTableView reloadData<span style="color: #002200;">&#93;</span>;</pre></div></div>

<h5>Looking Ahead to Part 3</h5>
<p>At this point we can request data from Flickr, store the results into internal variables and populate and display a table of images and descriptions.</p>
<p>There are two things we need to finish before we can call this application complete. First, we need to add a textfield so users can input their desired search strings. Second, when a user taps on a row in the table, we want to display a larger version of the image above the table. We&#8217;ll do this by sliding a view onto the screen from right. </p>
<h5>Summary of Links</h5>
<blockquote>
<ol>
<li><a   target="_blank"  href="http://iphonedevelopertips.com/networking/uber-iphone-json-flickr-tutorial-part-1.html">iPhone, JSON and Flickr Tutorial &#8211; Part 1</a></li>
<li><a   target="_blank"  href="http://iphonedevelopertips.com/networking/iphone-json-flickr-tutorial-part-3.html">iPhone, JSON and Flickr Tutorial &#8211; Part 3</a></li>
<li><a   target="_blank"  href="http://iphonedevelopertips.com/wp-content/uploads/2009/09/iPhone-JSON-Flickr-Part2.zip">Download iPhone, JSON and Flickr &#8211; Part 2 Xcode Project</a></li>
<li><a  target="_blank"  href="http://www.flickr.com/services/api/keys/apply/">Get Flickr API key</a></li>
<li><a  target="_blank"  href="http://www.flickr.com/services/api/">Flickr API documentation</a></li>
<li><a  target="_blank"  href="http://code.google.com/p/json-framework/downloads/list">Download JSON framework</a> </li>
<li><a  target="_blank"  href="http://groups.google.com/group/json-framework">JSON Framework Google Group</a> </li>
<li><a  target="_blank"  href="http://devblog.brautaset.org/2009/05/31/json-framework-v2-2-released/">Stig Brautaset&#8217;s Blog</a> </li>
</ol>
</blockquote>
<p>Stay tuned for Part 3 of the iPhone, JSON and Flickr tutorial&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/networking/iphone-json-flickr-tutorial-part-2.html/feed</wfw:commentRss>
		<slash:comments>13</slash:comments>
<enclosure url="http://iphonedevelopertips.com/wp-content/uploads/2009/08/iPhoneJSONFlickr.mov" length="9127313" type="video/quicktime" />
		</item>
		<item>
		<title>iPhone JSON Flickr Tutorial &#8211; Part 1</title>
		<link>http://iPhoneDeveloperTips.com/networking/iphone-json-flickr-tutorial-part-1.html</link>
		<comments>http://iPhoneDeveloperTips.com/networking/iphone-json-flickr-tutorial-part-1.html#comments</comments>
		<pubDate>Wed, 26 Aug 2009 13:39:42 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Networking]]></category>
		<category><![CDATA[flickr]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[json]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=3442</guid>
		<description><![CDATA[Two consistently popular posts on iPhone Dev Tips are JSON Frameworks for iPhone Part 1 and Part 2. Seems a good time to revisit the combination of the iPhone and JSON, this time creating a complete working application. This is part one of a three part series in which I&#8217;ll build a Flickr photo viewer, [...]]]></description>
			<content:encoded><![CDATA[<p>Two consistently popular posts on iPhone Dev Tips are JSON Frameworks for iPhone <a  target="_blank"  href="http://iphonedevelopertips.com/networking/json-framework-for-iphone.html">Part 1</a> and <a href="http://iphonedevelopertips.com/cocoa/json-framework-for-iphone-part-2.html">Part 2</a>. Seems a good time to revisit the combination of the iPhone and JSON, this time creating a complete working application. </p>
<p>This is part one of a three part series in which I&#8217;ll build a Flickr photo viewer, a pretty simple application, however, we&#8217;ll cover some interesting stuff with the primary goal of understanding the nuts and bolts of working with JSON to build a complete working application accessing web-services.<br />
<span id="more-3442"></span></p>
<p>Let&#8217;s start by looking at the finished application &#8211; the video below shows the end result we&#8217;re after &#8211; it starts with a search box (UITextField) and an empty table. Once you enter a search string, the application will construct a URL to call a photo search method at Flickr. The return data will be used to populate a table with thumbnails images and any title associated with the photo.</p>
<p>As you browse the table, you can touch an image and a second request will be submitted to Flickr for a larger variation of the same image. The larger image will be displayed above the table. As you select additional rows in the table, the current image will slide off the screen and a new image will slide into place. Watch the video below, it&#8217;ll make more sense once you see everything pulled together.</p>
<p><center><br />
<embed src="http://iphonedevelopertips.com/wp-content/uploads/2009/08/iPhoneJSONFlickr.mov" width="256" height="405" autoplay="false"><br />
</center><br />
</p>
<h5>Setting up JSON Framework</h5>
<p>The library used in this application is one written by <a  target="_blank"  href="http://code.brautaset.org/">Stig Brautaset</a> and hosted at <a  target="_blank"  href="http://code.google.com/p/json-framework/">code.google.com</a>. I&#8217;ve used this library in two applications that are in the App Store, Today&#8217;s Horoscope, <a href="http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=306121890&#038;mt=8">free</a> and <a href="http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=317961858&#038;mt=8">paid</a>. This is an excellent library, you can&#8217;t go wrong.</p>
<blockquote>
<ol>
<li>Download the <a  target="_blank"  href="http://code.google.com/p/json-framework/downloads/list">iPhone JSON framework</a> </li>
</ol>
</blockquote>
<p>There are a few variations on the install process, the easiest and least prone to errors is to copy the source files directly into your project. In the project that is attached to this post, this is done for you, however, it&#8217;s shown here if you need to add the framework to an existing or future project.</p>
<blockquote>
<ol>
<li>Open the dmg file from the download.</li>
<li>Drag the JSON folder and drop it on the &#8216;Classes&#8217; group in the &#8216;Groups &#038; Files&#8217; in your Xcode project.</li>
<li>Check the &#8220;Copy items into destination group&#8217;s folder&#8221; option.</li>
<li>You can now use <strong>#import &#8220;JSON.h&#8221;</strong> in your source files.</li>
</ol>
</blockquote>
<h5>Xcode Project Download &#8211; Part 1</h5>
<p>To keep this post to a manageable length, I&#8217;ll show only the code that is most pertinent to JSON, Flickr and the overall application flow, the code on the periphery you&#8217;ll be familiar with and you can browse through at any time in the project source code. </p>
<blockquote>
<ol>
<li>Download <a href="http://iphonedevelopertips.com/wp-content/uploads/2009/08/iPhone-JSON-Flickr-Part1.zip">iPhone, JSON and Flickr &#8211; Part 1</a> Xcode project</li>
</ol>
</blockquote>
<h5>Get Flickr API Key</h5>
<p>If you don&#8217;t already have a Flickr API key, that&#8217;s the next step.</p>
<blockquote>
<ol>
<li><a  target="_blank"  href="http://www.flickr.com/services/api/keys/apply/">Get Flickr API key</a>. </li>
</ol>
</blockquote>
<p>The key will be an alpha-numeric value that you pass into all requests when calling a Flickr API. You&#8217;ll need to replace the key in the project for this post with your own Flickr key. Once you have the key, you&#8217;ll need to copy/paste the same into the <strong>FlickrAPIKey</strong> variable in the file <strong>JSONFlickrViewController.m</strong>.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #a61390;">const</span> FlickrAPIKey <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;your-key-here&quot;</span>;</pre></div></div>

<h5>Flickr APIs</h5>
<p>Flickr has an impressive list of APIs you can access, take a look here <a  target="_blank"  href="http://www.flickr.com/services/api/">Flickr API&#8217;s</a>.</p>
<p>We&#8217;ll be using REST request format for calling Flickr, here is basic layout for how calls will look:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">http<span style="color: #002200;">:</span><span style="color: #11740a; font-style: italic;">//api.flickr.com/services/rest/?method=flickr.test.echo&amp;name=value</span></pre></div></div>

<p>For our interests, we want to search for photos, so the method we are after is: <strong>flickr.photos.search</strong>. The Flickr API page for this method is <a  target="_blank" href="http://www.flickr.com/services/api/flickr.photos.search.html">here</a>.</p>
<p>The full URL we&#8217;ll pass to Flickr will look as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>urlString <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;http://api.flickr.com/services/rest/?method=flickr.photos.search&amp;api_key=%@&amp;tags=%@&amp;per_page=25&amp;format=json&amp;nojsoncallback=1&quot;</span>, 
     FlickrAPIKey, text<span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>The parameters to the query are:</p>
<blockquote>
<ol>
<li><strong>api_key</strong>: your specific developer key</li>
<li><strong>tags</strong>: the search text</li>
<li><strong>per_page</strong>: the number of images to return</li>
<li><strong>format=json&#038;nojsoncallback:</strong> request the results to be returned as JSON</li>
</ol>
</blockquote>
<h5>Earth to Flickr, Come in Flickr</h5>
<p>With our URL complete, our next step is to make sure we can talk to the appropriate Flickr API and retrieve images based on a search string.</p>
<p>We have a very basic application framework at this point, an App Delegate class and a bare bones view controller. The interface definition for the view controller follows:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> JSONFlickrViewController <span style="color: #002200;">:</span> UIViewController
<span style="color: #002200;">&#123;</span>
  <span style="color: #400080;">NSMutableArray</span>  <span style="color: #002200;">*</span>photoTitles;         <span style="color: #11740a; font-style: italic;">// Titles of images</span>
  <span style="color: #400080;">NSMutableArray</span>  <span style="color: #002200;">*</span>photoSmallImageData; <span style="color: #11740a; font-style: italic;">// Image data (thumbnail)</span>
  <span style="color: #400080;">NSMutableArray</span>  <span style="color: #002200;">*</span>photoURLsLargeImage; <span style="color: #11740a; font-style: italic;">// URL to larger image</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>At this point all we are concerned about is capturing the names of the images from Flickr, the image data for our thumbnail image and also, the URL&#8217;s for the larger images. The initialization code consists of allocating a view, initializing the arrays, and calling the method to search Flickr for photos matching a search string:</p>

<div class="wp_syntax"><div 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>init
<span style="color: #002200;">&#123;</span>
  <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>
    self.view <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</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><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIScreen mainScreen<span style="color: #002200;">&#93;</span> applicationFrame<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Initialize our arrays</span>
    photoTitles <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>;
    photoSmallImageData <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>;
    photoURLsLargeImage <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: #11740a; font-style: italic;">// Notice that I am hard-coding the search tag at this point (@&quot;iPhone&quot;)    </span>
    <span style="color: #002200;">&#91;</span>self searchFlickrPhotos<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;iPhone&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #002200;">&#125;</span>
  <span style="color: #a61390;">return</span> self;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>The call to <strong>[self searchFlickrPhotos:@"iPhone"];</strong> begins the process of creating the URL to call Flickr, which in turn will kick off an asynchronous request to download the image data.</p>

<div class="wp_syntax"><div 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>searchFlickrPhotos<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>text
<span style="color: #002200;">&#123;</span>
  <span style="color: #11740a; font-style: italic;">// Build the string to call the Flickr API</span>
  <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>urlString <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;http://api.flickr.com/services/rest/?method=flickr.photos.search&amp;api_key=%@&amp;tags=%@&amp;per_page=15&amp;format=json&amp;nojsoncallback=1&quot;</span>, FlickrAPIKey, text<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Create NSURL string from formatted string</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>urlString<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Setup and start async download</span>
  <span style="color: #400080;">NSURLRequest</span> <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: #400080;">NSURLRequest</span> alloc<span style="color: #002200;">&#93;</span> initWithURL<span style="color: #002200;">:</span> url<span style="color: #002200;">&#93;</span>;
  <span style="color: #400080;">NSURLConnection</span> <span style="color: #002200;">*</span>connection <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSURLConnection</span> alloc<span style="color: #002200;">&#93;</span> initWithRequest<span style="color: #002200;">:</span>request delegate<span style="color: #002200;">:</span>self<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>request release<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>At this point we&#8217;ve made our request to Flickr to search for photos that are tagged with the text &#8220;iPhone&#8221; From here, we need to process the incoming data. The method <strong>didReceiveData:</strong> is the callback that will deal with the incoming data. Within this method we&#8217;ll save the title of the image, the image data for the thumbnail as well as the URL for the large image. Here&#8217;s what that code looks like:</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
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
</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>connection<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSURLConnection</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>connection didReceiveData<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSData</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>data 
<span style="color: #002200;">&#123;</span>
  <span style="color: #11740a; font-style: italic;">// Store incoming data into a string</span>
  <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>jsonString <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> alloc<span style="color: #002200;">&#93;</span> initWithData<span style="color: #002200;">:</span>data encoding<span style="color: #002200;">:</span>NSUTF8StringEncoding<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Create a dictionary from the JSON string</span>
  <span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span>results <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>jsonString JSONValue<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Build an array from the dictionary for easy access to each entry</span>
  <span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>photos <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>results objectForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;photos&quot;</span><span style="color: #002200;">&#93;</span> objectForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;photo&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Loop through each entry in the dictionary...</span>
  <span style="color: #a61390;">for</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span>photo <span style="color: #a61390;">in</span> photos<span style="color: #002200;">&#41;</span>
  <span style="color: #002200;">&#123;</span>
    <span style="color: #11740a; font-style: italic;">// Get title of the image</span>
    <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>title <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>photo objectForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;title&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Save the title to the photo titles array</span>
    <span style="color: #002200;">&#91;</span>photoTitles addObject<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>title.length &gt; <span style="color: #2400d9;">0</span> ? title <span style="color: #002200;">:</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Untitled&quot;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Build the URL to where the image is stored (see the Flickr API)</span>
    <span style="color: #11740a; font-style: italic;">// In the format http://farmX.static.flickr.com/server/id_secret.jpg</span>
    <span style="color: #11740a; font-style: italic;">// Notice the &quot;_s&quot; which requests a &quot;small&quot; image 75 x 75 pixels</span>
    <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>photoURLString <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;http://farm%@.static.flickr.com/%@/%@_%@_s.jpg&quot;</span>, 
      <span style="color: #002200;">&#91;</span>photo objectForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;farm&quot;</span><span style="color: #002200;">&#93;</span>, <span style="color: #002200;">&#91;</span>photo objectForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;server&quot;</span><span style="color: #002200;">&#93;</span>, 
      <span style="color: #002200;">&#91;</span>photo objectForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;id&quot;</span><span style="color: #002200;">&#93;</span>, <span style="color: #002200;">&#91;</span>photo objectForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;secret&quot;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
    NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;photoURLString: %@&quot;</span>, photoURLString<span style="color: #002200;">&#41;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// The performance (scrolling) of the table will be much better if we</span>
    <span style="color: #11740a; font-style: italic;">// build an array of the image data here, and then add this data as</span>
    <span style="color: #11740a; font-style: italic;">// the cell.image value (see cellForRowAtIndexPath:)</span>
    <span style="color: #002200;">&#91;</span>photoSmallImageData addObject<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSData</span> dataWithContentsOfURL<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSURL</span> URLWithString<span style="color: #002200;">:</span>photoURLString<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Build and save the URL to the large image so we can zoom</span>
    <span style="color: #11740a; font-style: italic;">// in on the image if requested</span>
    photoURLString <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;http://farm%@.static.flickr.com/%@/%@_%@_m.jpg&quot;</span>, 
      <span style="color: #002200;">&#91;</span>photo objectForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;farm&quot;</span><span style="color: #002200;">&#93;</span>, <span style="color: #002200;">&#91;</span>photo objectForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;server&quot;</span><span style="color: #002200;">&#93;</span>, 
      <span style="color: #002200;">&#91;</span>photo objectForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;id&quot;</span><span style="color: #002200;">&#93;</span>, <span style="color: #002200;">&#91;</span>photo objectForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;secret&quot;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>photoURLsLargeImage addObject<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSURL</span> URLWithString<span style="color: #002200;">:</span>photoURLString<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;        
&nbsp;
    NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;photoURLsLareImage: %@<span style="color: #2400d9;">\n</span><span style="color: #2400d9;">\n</span>&quot;</span>, photoURLString<span style="color: #002200;">&#41;</span>; 
  <span style="color: #002200;">&#125;</span> 
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>Look closely at line 7: <strong>NSDictionary *results = [jsonString JSONValue];</strong> which is where the magic occurs, converting the incoming JSON data into a dictionary, which makes our parsing the Flickr data as simple as working with key-value pairs. We&#8217;ll look closer at the JSON return values from Flickr, converting to a dictionary and building the arrays of image data in Part 2 of this tutorial.</p>
<h5>Flickr Image Data</h5>
<p>Notice in the code above how I added to NSLog statements to verify the return results. A partial listing of the output is shown below:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2009/08/part1-output.png" /></p>
<p>As one last sanity check, you can copy/paste a few of the links shown in the Xcode console into a browser window to see what photos Flickr is returning.</p>
<h5>Looking Ahead to Part 2</h5>
<p>In Part 2 of this tutorial we&#8217;ll start by looking at the raw data returned from Flickr and walk through how to map that information into dictionaries and arrays. We will also extend the current application to store the results from our query into a table. We&#8217;ll also add a textfield to the interface so we can search for photos matching any tag. </p>
<h5>Summary of Links</h5>
<blockquote>
<ol>
<li><a   target="_blank"  href="http://iphonedevelopertips.com/networking/iphone-json-flickr-tutorial-part-2.html">iPhone, JSON and Flickr Tutorial &#8211; Part 2</a></li>
<li><a   target="_blank"  href="http://iphonedevelopertips.com/networking/iphone-json-flickr-tutorial-part-3.html">iPhone, JSON and Flickr Tutorial &#8211; Part 3</a></li>
<li><a   target="_blank"  href="http://iphonedevelopertips.com/wp-content/uploads/2009/08/iPhone-JSON-Flickr-Part1.zip">Download iPhone, JSON and Flickr &#8211; Part 1 Xcode Project</a></li>
<li><a  target="_blank"  href="http://www.flickr.com/services/api/keys/apply/">Get Flickr API key</a></li>
<li><a  target="_blank"  href="http://www.flickr.com/services/api/">Flickr API documentation</a></li>
<li><a  target="_blank"  href="http://code.google.com/p/json-framework/downloads/list">Download JSON framework</a> </li>
<li><a  target="_blank"  href="http://groups.google.com/group/json-framework">JSON Framework Google Group</a> </li>
<li><a  target="_blank"  href="http://devblog.brautaset.org/2009/05/31/json-framework-v2-2-released/">Stig Brautaset&#8217;s Blog</a> </li>
</ol>
</blockquote>
]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/networking/iphone-json-flickr-tutorial-part-1.html/feed</wfw:commentRss>
		<slash:comments>16</slash:comments>
<enclosure url="http://iphonedevelopertips.com/wp-content/uploads/2009/08/iPhoneJSONFlickr.mov" length="9127313" type="video/quicktime" />
		</item>
		<item>
		<title>JSON Framework for iPhone</title>
		<link>http://iPhoneDeveloperTips.com/networking/json-framework-for-iphone.html</link>
		<comments>http://iPhoneDeveloperTips.com/networking/json-framework-for-iphone.html#comments</comments>
		<pubDate>Mon, 13 Oct 2008 15:34:30 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Networking]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[json]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=704</guid>
		<description><![CDATA[Editor&#8217;s Note: Due to the popularity of this iPhone JSON series, a new three part tutorial series on working with JSON was published in August 2009. You can find the latest JSON iPhone tutorial series at the links below: iPhone JSON and Flickr Tutorial &#8211; Part 1 iPhone JSON and Flickr Tutorial &#8211; Part 2 [...]]]></description>
			<content:encoded><![CDATA[<p><em><strong>Editor&#8217;s Note</strong>: Due to the popularity of this iPhone JSON series, a new three part tutorial series on working with JSON was published in August 2009. You can find the latest JSON iPhone tutorial series at the links below:</p>
<blockquote>
<ol>
<li><a   target="_blank"  href="http://iphonedevelopertips.com/networking/uber-iphone-json-flickr-tutorial-part-1.html">iPhone JSON and Flickr Tutorial &#8211; Part 1</a></li>
<li><a   target="_blank"  href="http://iphonedevelopertips.com/networking/iphone-json-flickr-tutorial-part-2.html">iPhone JSON and Flickr Tutorial &#8211; Part 2</a></li>
<li><a   target="_blank"  href="http://iphonedevelopertips.com/networking/iphone-json-flickr-tutorial-part-3.html">iPhone JSON and Flickr Tutorial &#8211; Part 3</a></li>
</ol>
</blockquote>
<p></em></p>
<p>If you&#8217;ve found a need to work with JSON and the iPhone, I want to point you to an excellent Google code project: <a href="http://code.google.com/p/json-framework/" target="_blank">http://code.google.com/p/json-framework/</a>. The author of this framework can be found <a href="http://devblog.brautaset.org/" target="_blank">here</a>.</p>
<p>This framework implements a strict JSON parser and generator in Objective-C. It&#8217;s easy to work with and can be used across any number of projects with ease.<br />
<span id="more-704"></span></p>
<p>To use this framework it was pretty straightforward to get things in place, copy the included &quot;SDKs&quot; folder onto your local system (into ~/Library/SDKs). From within a project, modify the target build options to point to the SDK and add a link option to include the necessary library into the build.</p>
<p>From this point forward, the SDK is now available for use within any iPhone project, simply update the build settings and you are good to go.</p>
<p>You can find Part 2 of working with JSON <a href="http://iphonedevelopertips.com/cocoa/json-framework-for-iphone-part-2.html">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/networking/json-framework-for-iphone.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Troubleshooting NSURLConnection</title>
		<link>http://iPhoneDeveloperTips.com/networking/troubleshooting-nsurlconnection.html</link>
		<comments>http://iPhoneDeveloperTips.com/networking/troubleshooting-nsurlconnection.html#comments</comments>
		<pubDate>Tue, 07 Oct 2008 07:21:13 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Networking]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=654</guid>
		<description><![CDATA[I was recently helping a friend debug an interesting piece of code to asynchronously download images. Without giving away the application idea, let me explain what we were attempting to do from the 30,000 foot view and then share what we discovered, hopefully saving you time should you ever need to implement similar functionality. The [...]]]></description>
			<content:encoded><![CDATA[<p>I was recently helping a <a target="_blank" href="http://mobuser.com">friend</a> debug an interesting piece of code to asynchronously download images. Without giving away the application idea, let me explain what we were attempting to do from the 30,000 foot view and then share what we discovered, hopefully saving you time should you ever need to implement similar functionality.<br />
<span id="more-654"></span></p>
<p>The basic idea was to download thumbnail images in the background, and update rows in a table in realtime, as the images were retrieved. This lends to a really nice visual effect on the iPhone. Picture a table with two columns; the right column displays text and the left column shows an avatar (as png file). For each visible row, the text is immediately shown, and the image on the left starts out with a placeholder image (packaged with the application). As each avatar is downloaded, the applicable row in the table is updated with the new image. </p>
<p>The basic idea was to create a URL request (NSURLRequest) specifying the desired URL. With this in hand, create a URL connection (NSURLConnection) to load the URL request.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSURLRequest</span> <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: #400080;">NSURLRequest</span> alloc<span style="color: #002200;">&#93;</span> initWithURL<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSURL</span> URLWithString<span style="color: #002200;">:</span>_url<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span><span style="color: #400080;">NSURLConnection</span> connectionWithRequest<span style="color: #002200;">:</span>request delegate<span style="color: #002200;">:</span>self<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>connection start<span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

<p>From the reference documentation, the description for the <em><strong>start</strong></em> method is as follows: &#8220;Causes the receiver to begin loading data, if it has not already.&#8221; Seems the right thing to do after creating the URL and connection requests.</p>
<p>Using delegate methods of NSURLConnection, you can receive callbacks for things such as when data is received and when a connection has finished loading. This is handy, as our approach was to initiate additional requests for images while waiting for others to download.</p>
<p>The trouble we ran into was that we kept getting time out errors. The specific error was often displayed as &#8220;EXC_BAD_ACCESS&#8221; in the debug console. What was odd, was that we could account for each URL request, connection and download completing. It was obvious that a connection was being created somewhere on the way, and timing out, question was, what connection?</p>
<p>Here&#8217;s where we went wrong&#8230;when calling connectionWithRequest: (line #2 above) an initialized URL connection is returned and the data is immediately requested. The call to <em><strong>start</strong></em> must of begun a new connection request, which would eventually timeout as the original request was already underway. When we removed the call to <em><strong>start</strong></em> the application worked like a champ.</p>
<p>This was a surprisingly tricky bug to track down. From reading the documentation one would think calling <strong><em>start</em></strong> would be a reasonable thing to do, in other words, if the receiver hasn&#8217;t begun to download data, begin to do so (it&#8217;s obviously not quite that simple). It&#8217;s not clear to me when one would use this method. If anyone has some insight, I&#8217;d love to hear it.</p>
]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/networking/troubleshooting-nsurlconnection.html/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
