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

<channel>
	<title>iPhone Programming Tutorials &#187; objective-c</title>
	<atom:link href="/tag/objective-c/feed/" rel="self" type="application/rss+xml" />
	<link>http://icodeblog.com</link>
	<description>iPhone Programming Tutorials</description>
	<lastBuildDate>Tue, 19 Nov 2013 19:34:46 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=4.1.1</generator>
	<item>
		<title>Using Method-Swizzling to help with Test Driven Development</title>
		<link>http://icodeblog.com/2012/08/08/using-method-swizzling-to-help-with-test-driven-development/</link>
		<comments>http://icodeblog.com/2012/08/08/using-method-swizzling-to-help-with-test-driven-development/#comments</comments>
		<pubDate>Wed, 08 Aug 2012 08:00:22 +0000</pubDate>
		<dc:creator><![CDATA[dpettigrew]]></dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Advanced]]></category>
		<category><![CDATA[hamcrest]]></category>
		<category><![CDATA[iOS]]></category>
		<category><![CDATA[mock]]></category>
		<category><![CDATA[mocking]]></category>
		<category><![CDATA[objective-c]]></category>
		<category><![CDATA[ocunit]]></category>
		<category><![CDATA[runtime]]></category>
		<category><![CDATA[swizzling]]></category>
		<category><![CDATA[tdd]]></category>
		<category><![CDATA[test driven development]]></category>
		<category><![CDATA[testing]]></category>

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

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

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

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

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

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

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

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

<p>The method under test here is:-</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>loadCollectionFromEndpoint<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>endpoint successBlock<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>JSONLoaderLoadSuccessBlock<span style="color: #002200;">&#41;</span>successBlock failBlock<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>JSONLoaderLoadFailBlock<span style="color: #002200;">&#41;</span>failBlock</pre></td></tr></table></div>

<p>This method calls the AsyncURLConnection class method</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>request<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>requestUrl completeBlock<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>completeBlock_t<span style="color: #002200;">&#41;</span>completeBlock errorBlock<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>errorBlock_t<span style="color: #002200;">&#41;</span>errorBlock;</pre></td></tr></table></div>

<p>I will replace the existing implementation of that method with one that does not actually make an HTTP request, but instead gets JSON from a local file.</p>
<p>The unit test uses <a href="https://github.com/hamcrest/OCHamcrest">OCHamcrest</a> matchers and XCode&#8217;s built in <a href="http://www.sente.ch/software/ocunit/">OCUnit</a>.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>test_loadPopularCoaches_success
<span style="color: #002200;">&#123;</span>
    <span style="color: #11740a; font-style: italic;">// Test set-up</span>
    Swizzler <span style="color: #002200;">*</span>swizzler <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>Swizzler new<span style="color: #002200;">&#93;</span>;
    <span style="color: #11740a; font-style: italic;">// Replace the actual AsyncURLConnection method with our own test one.</span>
    <span style="color: #002200;">&#91;</span>swizzler swizzleClassMethod<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>AsyncURLConnection class<span style="color: #002200;">&#93;</span> selector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>request<span style="color: #002200;">:</span>completeBlock<span style="color: #002200;">:</span>errorBlock<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span> swizzleClass<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>RequestPopularCoachesPassMock class<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
    __block <span style="color: #a61390;">BOOL</span> hasCalledBack <span style="color: #002200;">=</span> <span style="color: #a61390;">NO</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Test</span>
    <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>endpoint <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;%@?per_page=6&amp;amp;page=1&amp;amp;popular=true&quot;</span>, kCoachesEndpoint<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>JSONLoader sharedJSONLoader<span style="color: #002200;">&#93;</span> loadCollectionFromEndpoint<span style="color: #002200;">:</span>endpoint successBlock<span style="color: #002200;">:^</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span>collection<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>coaches <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>collection valueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;coaches&quot;</span><span style="color: #002200;">&#93;</span>;
        assertThat<span style="color: #002200;">&#40;</span>coaches, hasCountOf<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">6</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>; 
        <span style="color: #a61390;">int</span> i <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;
        <span style="color: #a61390;">for</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span>coach <span style="color: #a61390;">in</span> coaches<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
            STAssertTrue<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>coach isKindOfClass<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDictionary</span> class<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Incorrect class&quot;</span><span style="color: #002200;">&#41;</span>;
            assertThat<span style="color: #002200;">&#40;</span>coach, equalTo<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>popularCoaches objectAtIndex<span style="color: #002200;">:</span>i<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>; <span style="color: #11740a; font-style: italic;">// popularCoaches defined elsewhere with expected data</span>
            <span style="color: #002200;">++</span>i;
        <span style="color: #002200;">&#125;</span>
        assertThat<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>collection valueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;per_page&quot;</span><span style="color: #002200;">&#93;</span>, equalToInt<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">6</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
        assertThat<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>collection valueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;page&quot;</span><span style="color: #002200;">&#93;</span>, equalToInt<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">1</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
        assertThat<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>collection valueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;total&quot;</span><span style="color: #002200;">&#93;</span>, equalToInt<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">11</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
        assertThat<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>collection valueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;total_pages&quot;</span><span style="color: #002200;">&#93;</span>, equalToInt<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">2</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
        hasCalledBack <span style="color: #002200;">=</span> <span style="color: #a61390;">YES</span>;
    <span style="color: #002200;">&#125;</span> failBlock<span style="color: #002200;">:^</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSError</span> <span style="color: #002200;">*</span>error<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;%@&quot;</span>, <span style="color: #002200;">&#91;</span>error localizedDescription<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
        STFail<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Unexpected error returned&quot;</span><span style="color: #002200;">&#41;</span>;
        hasCalledBack <span style="color: #002200;">=</span> <span style="color: #a61390;">YES</span>;
    <span style="color: #002200;">&#125;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// see http://drewsmitscode.posterous.com/testing-asynchronous-code-in-objective-c</span>
    <span style="color: #400080;">NSDate</span> <span style="color: #002200;">*</span>loopUntil <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDate</span> dateWithTimeIntervalSinceNow<span style="color: #002200;">:</span><span style="color: #2400d9;">1</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">while</span> <span style="color: #002200;">&#40;</span>hasCalledBack <span style="color: #002200;">==</span> <span style="color: #a61390;">NO</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSRunLoop</span> currentRunLoop<span style="color: #002200;">&#93;</span> runMode<span style="color: #002200;">:</span>NSDefaultRunLoopMode beforeDate<span style="color: #002200;">:</span>loopUntil<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
    swizzler <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

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

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

<h2>Conclusion</h2>
<p>In this blog post I have demonstrated how to use the Objective-C runtime dynamic method replacement technique known as *method swizzling* to assist with unit testing in a situation where there is a 3rd party web service that I want to mock. This technique can be used during development to assist with integrating of an iOS app with a web service. It helps speed up development and prototyping. It can also be applied during development in the normal running of your app if you want to mock out portions of or all of a web service.</p>
]]></content:encoded>
			<wfw:commentRss>http://icodeblog.com/2012/08/08/using-method-swizzling-to-help-with-test-driven-development/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Adding Local Weather Conditions To Your App (Part 1/2: Implementing CoreLocation)</title>
		<link>http://icodeblog.com/2010/09/03/adding-local-weather-conditions-to-your-app-part-12-implementing-corelocation/</link>
		<comments>http://icodeblog.com/2010/09/03/adding-local-weather-conditions-to-your-app-part-12-implementing-corelocation/#comments</comments>
		<pubDate>Fri, 03 Sep 2010 22:45:12 +0000</pubDate>
		<dc:creator><![CDATA[Matt Tuzzolo]]></dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Coordinates]]></category>
		<category><![CDATA[CoreLocation]]></category>
		<category><![CDATA[GPS]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[Latitude]]></category>
		<category><![CDATA[Longitude]]></category>
		<category><![CDATA[objective-c]]></category>
		<category><![CDATA[xcode]]></category>

		<guid isPermaLink="false">http://icodeblog.com/?p=2228</guid>
		<description><![CDATA[Knowing the latitude and longitude of your users can open up all kinds of possibilities in your apps.  In an upcoming post, we&#8217;ll be discussing how you can use your user&#8217;s location to determine their local weather conditions and forecast.  But for now, we&#8217;re going to focus on Part 1 of this two part tutorial: CoreLocation.
Apple&#8217;s done a great job of abstracting GPS, Cellular Triangulation, and Wifi Access Point location lookups into CoreLocation; making it extremely easy to  ...]]></description>
				<content:encoded><![CDATA[<p>Knowing the latitude and longitude of your users can open up all kinds of possibilities in your apps.  In an upcoming post, we&#8217;ll be discussing how you can use your user&#8217;s location to determine their local weather conditions and forecast.  But for now, we&#8217;re going to focus on Part 1 of this two part tutorial: CoreLocation.</p>
<p>Apple&#8217;s done a great job of abstracting GPS, Cellular Triangulation, and Wifi Access Point location lookups into CoreLocation; making it extremely easy to determine the approximate location of your user regardless of their device and network connectivity.  Additionally, Apple&#8217;s new iPhone Simulator finally supports CoreLocation as well.  This significantly eases the process of testing your location code.</p>
<p>The first thing you&#8217;ll need to do is add the CoreLocation framework to your project.  This is pretty straightforward.  Command-Click on &#8216;Frameworks&#8217; -&gt; Add -&gt; Existing Frameworks</p>
<p><a href="/wp-content/uploads/2010/09/Screen-shot-2010-09-03-at-12.00.35-PM1.png"><img class="alignnone size-medium wp-image-2232" title="Screen shot 2010-09-03 at 12.00.35 PM" src="/wp-content/uploads/2010/09/Screen-shot-2010-09-03-at-12.00.35-PM-300x193.png" alt="" width="300" height="193" /></a></p>
<p>Select &#8216;CoreLocation.framework&#8217; and click &#8216;add&#8217;.</p>
<p><a href="/wp-content/uploads/2010/09/Screen-shot-2010-09-03-at-12.00.49-PM1.png"><img class="alignnone size-medium wp-image-2231" title="Screen shot 2010-09-03 at 12.00.49 PM" src="/wp-content/uploads/2010/09/Screen-shot-2010-09-03-at-12.00.49-PM-191x300.png" alt="" width="191" height="300" /></a></p>
<p>The class below implements the LocationManager delegate methods required to get the user&#8217;s location.  You can just as easily implement the LocationManagerDelegate protocol in your AppDelegate or elsewhere in your App.  Though I&#8217;ve found that having this class makes it super easy to drop in location support into new projects instead of having to cut/paste delegate methods (ugly).  Anyway.  Take a look:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">//</span>
<span style="color: #11740a; font-style: italic;">//  LocationGetter.h</span>
<span style="color: #11740a; font-style: italic;">//  CoreLocationExample</span>
<span style="color: #11740a; font-style: italic;">//</span>
<span style="color: #11740a; font-style: italic;">//  Created by Matt on 9/3/10.</span>
<span style="color: #11740a; font-style: italic;">//  Copyright 2009 iCodeBlog. All rights reserved.</span>
<span style="color: #11740a; font-style: italic;">//</span>
&nbsp;
<span style="color: #6e371a;">#import &lt;uikit/UIKit.h&gt;</span>
<span style="color: #6e371a;">#import &lt;coreLocation/CoreLocation.h&gt;</span>
&nbsp;
<span style="color: #a61390;">@protocol</span> LocationGetterDelegate
@required
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> newPhysicalLocation<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CLLocation <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>location;
<span style="color: #a61390;">@end</span>
&nbsp;
<span style="color: #a61390;">@interface</span> LocationGetter <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span>  <span style="color: #002200;">&#123;</span>
    CLLocationManager <span style="color: #002200;">*</span>locationManager;
    <span style="color: #a61390;">id</span> delegate;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>startUpdates;
&nbsp;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, retain<span style="color: #002200;">&#41;</span> CLLocationManager <span style="color: #002200;">*</span>locationManager;
<span style="color: #a61390;">@property</span><span style="color: #002200;">&#40;</span>nonatomic , retain<span style="color: #002200;">&#41;</span> <span style="color: #a61390;">id</span> delegate;
<span style="color: #a61390;">@end</span></pre></td></tr></table></div>

<p>Notice that we&#8217;re defining our own protocol with a method that takes the new CLLocation as a parameter.  We&#8217;ll be implementing that delegate method in a minute.  Now for the class</p>

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


<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">//  LocationGetter.m</span>
<span style="color: #11740a; font-style: italic;">//  CoreLocationExample</span>
<span style="color: #11740a; font-style: italic;">//</span>
<span style="color: #11740a; font-style: italic;">//  Created by Matt on 9/3/10.</span>
<span style="color: #11740a; font-style: italic;">//  Copyright 2009 iCodeBlog. All rights reserved.</span>
<span style="color: #11740a; font-style: italic;">//</span>
&nbsp;
<span style="color: #6e371a;">#import &quot;LocationGetter.h&quot;</span>
<span style="color: #6e371a;">#import &lt;coreLocation/CoreLocation.h&gt;</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> LocationGetter
&nbsp;
<span style="color: #a61390;">@synthesize</span> locationManager, delegate;
<span style="color: #a61390;">BOOL</span> didUpdate <span style="color: #002200;">=</span> <span style="color: #a61390;">NO</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>startUpdates
<span style="color: #002200;">&#123;</span>
    NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Starting Location Updates&quot;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>locationManager <span style="color: #002200;">==</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span>
        locationManager <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>CLLocationManager alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
&nbsp;
    locationManager.delegate <span style="color: #002200;">=</span> self;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// locationManager.distanceFilter = 1000;  // update is triggered after device travels this far (meters)</span>
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Alternatively you can use kCLLocationAccuracyHundredMeters or kCLLocationAccuracyHundredMeters, though higher accuracy takes longer to resolve</span>
    locationManager.desiredAccuracy <span style="color: #002200;">=</span> kCLLocationAccuracyKilometer;
    <span style="color: #002200;">&#91;</span>locationManager startUpdatingLocation<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>locationManager<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CLLocationManager <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>manager didFailWithError<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSError</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>error
<span style="color: #002200;">&#123;</span>
    UIAlertView <span style="color: #002200;">*</span>alert <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIAlertView alloc<span style="color: #002200;">&#93;</span> initWithTitle<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Error&quot;</span> message<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Your location could not be determined.&quot;</span> delegate<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span> cancelButtonTitle<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;OK&quot;</span> otherButtonTitles<span style="color: #002200;">:</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>alert show<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>alert release<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// Delegate method from the CLLocationManagerDelegate protocol.</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>locationManager<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CLLocationManager <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>manage didUpdateToLocation<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CLLocation <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>newLocation fromLocation<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CLLocation <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>oldLocation
<span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>didUpdate<span style="color: #002200;">&#41;</span>
        <span style="color: #a61390;">return</span>;
&nbsp;
    didUpdate <span style="color: #002200;">=</span> <span style="color: #a61390;">YES</span>;
    <span style="color: #11740a; font-style: italic;">// Disable future updates to save power.</span>
    <span style="color: #002200;">&#91;</span>locationManager stopUpdatingLocation<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// let our delegate know we're done</span>
    <span style="color: #002200;">&#91;</span>delegate newPhysicalLocation<span style="color: #002200;">:</span>newLocation<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>dealloc
<span style="color: #002200;">&#123;</span>
    <span style="color: #002200;">&#91;</span>locationManager release<span style="color: #002200;">&#93;</span>;
&nbsp;
    <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>CoreLocation gives you a few options for the accuracy of the user&#8217;s location.  The more accurate the measurement, typically the longer it takes LocationManager to call it&#8217;s delegate method didUpdateToLocation.  It&#8217;s just something to keep in mind when deciding what level of accuracy to use.</p>
<p>Next we need to actually invoke this code and start getting location updates.  I usually do this in my AppDelegate&#8217;s didFinishLaunchingWithOptions, though you could also do it in viewDidLoad somewhere if you didn&#8217;t need to know the user&#8217;s location on app startup.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"> <span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span>application<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIApplication <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>application didFinishLaunchingWithOptions<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>launchOptions <span style="color: #002200;">&#123;</span>
&nbsp;
    UIActivityIndicatorView <span style="color: #002200;">*</span>spinner <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> initWithActivityIndicatorStyle<span style="color: #002200;">:</span>UIActivityIndicatorViewStyleWhiteLarge<span style="color: #002200;">&#93;</span>;
	spinner.center <span style="color: #002200;">=</span> CGPointMake<span style="color: #002200;">&#40;</span>self.viewController.view.frame.size.width <span style="color: #002200;">/</span> <span style="color: #2400d9;">2</span>, self.viewController.view.frame.size.height <span style="color: #002200;">/</span> <span style="color: #2400d9;">2</span><span style="color: #002200;">&#41;</span>;
    <span style="color: #002200;">&#91;</span>spinner startAnimating<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #002200;">&#91;</span>viewController.view addSubview<span style="color: #002200;">:</span>spinner<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// get our physical location</span>
    LocationGetter <span style="color: #002200;">*</span>locationGetter <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>LocationGetter alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
    locationGetter.delegate <span style="color: #002200;">=</span> self;
    <span style="color: #002200;">&#91;</span>locationGetter startUpdates<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Add the view controller's view to the window and display.</span>
    <span style="color: #002200;">&#91;</span>window addSubview<span style="color: #002200;">:</span>viewController.view<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>window makeKeyAndVisible<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></td></tr></table></div>

<p>Notice that I&#8217;ve set locationGetter&#8217;s delegate to self.  So in your .h, make sure to add LocationGetterDelegate to the interface.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">//</span>
<span style="color: #11740a; font-style: italic;">//  CoreLocationExampleAppDelegate.h</span>
<span style="color: #11740a; font-style: italic;">//  CoreLocationExample</span>
<span style="color: #11740a; font-style: italic;">//</span>
<span style="color: #11740a; font-style: italic;">//  Created by Matt Tuzzolo on 9/3/10.</span>
<span style="color: #11740a; font-style: italic;">//  Copyright iCodeBlog 2010. All rights reserved.</span>
<span style="color: #11740a; font-style: italic;">//</span>
&nbsp;
<span style="color: #6e371a;">#import &lt;uikit/UIKit.h&gt;</span>
<span style="color: #6e371a;">#import &quot;LocationGetter.h&quot;</span>
&nbsp;
<span style="color: #a61390;">@class</span> CoreLocationExampleViewController;
&nbsp;
<span style="color: #a61390;">@interface</span> CoreLocationExampleAppDelegate <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span>  <span style="color: #002200;">&#123;</span>
    UIWindow <span style="color: #002200;">*</span>window;
    CoreLocationExampleViewController <span style="color: #002200;">*</span>viewController;
    CLLocation <span style="color: #002200;">*</span>lastKnownLocation;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, retain<span style="color: #002200;">&#41;</span> IBOutlet UIWindow <span style="color: #002200;">*</span>window;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, retain<span style="color: #002200;">&#41;</span> IBOutlet CoreLocationExampleViewController <span style="color: #002200;">*</span>viewController;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, retain<span style="color: #002200;">&#41;</span> CLLocation <span style="color: #002200;">*</span>lastKnownLocation;
&nbsp;
<span style="color: #a61390;">@end</span></pre></td></tr></table></div>

<p>I&#8217;ve also added a CLLocation *lastKnownLocation that we&#8217;ll use in our delegate method (which comes next):</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;"># pragma mark -</span>
<span style="color: #6e371a;"># pragma mark LocationGetter Delegate 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>newPhysicalLocation<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CLLocation <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>location <span style="color: #002200;">&#123;</span>
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Store for later use</span>
    self.lastKnownLocation <span style="color: #002200;">=</span> location;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Remove spinner from view</span>
    <span style="color: #a61390;">for</span> <span style="color: #002200;">&#40;</span>UIView <span style="color: #002200;">*</span>v <span style="color: #a61390;">in</span> <span style="color: #002200;">&#91;</span>self.viewController.view subviews<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#123;</span>
        <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>v class<span style="color: #002200;">&#93;</span> <span style="color: #002200;">==</span> <span style="color: #002200;">&#91;</span>UIActivityIndicatorView class<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>
        <span style="color: #002200;">&#123;</span>
            <span style="color: #002200;">&#91;</span>v removeFromSuperview<span style="color: #002200;">&#93;</span>;
            <span style="color: #a61390;">break</span>;
        <span style="color: #002200;">&#125;</span>
    <span style="color: #002200;">&#125;</span>
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Alert user</span>
    UIAlertView <span style="color: #002200;">*</span>alert <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIAlertView alloc<span style="color: #002200;">&#93;</span> initWithTitle<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Location Found&quot;</span> message<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;Found physical location.  %f %f&quot;</span>, self.lastKnownLocation.coordinate.latitude, self.lastKnownLocation.coordinate.longitude<span style="color: #002200;">&#93;</span> delegate<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span> cancelButtonTitle<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;OK&quot;</span> otherButtonTitles<span style="color: #002200;">:</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>alert show<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>alert release<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// ... continue with initialization of your app</span>
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>This last piece takes care of storing the location, removing the spinner, and firing off an alert.  If this was your code, you&#8217;d probably want to re-enable any UI elements that you&#8217;ve disabled and let the user start using your app.</p>
<p>Now build and run your app and you should see:</p>
<p><a href="/wp-content/uploads/2010/09/Screen-shot-2010-09-07-at-9.45.33-AM1.png"><img src="/wp-content/uploads/2010/09/Screen-shot-2010-09-07-at-9.45.33-AM1.png" alt="" title="Screen shot 2010-09-07 at 9.45.33 AM" width="280" height="516" class="alignnone size-full wp-image-2307" /></a></p>
<p>That&#8217;s it!</p>
<p>Here&#8217;s the <a href="/wp-content/uploads/2010/09/CoreLocationExample1.zip">Example Project</a> for the post.</p>
<p>For those of you who don&#8217;t know me yet, my name is Matt Tuzzolo (<a href="http://www.twitter.com/matt_tuzzolo">@matt_tuzzolo</a>).  This is my first iCodeBlog post.  I hope you enjoyed it.</p>
]]></content:encoded>
			<wfw:commentRss>http://icodeblog.com/2010/09/03/adding-local-weather-conditions-to-your-app-part-12-implementing-corelocation/feed/</wfw:commentRss>
		<slash:comments>29</slash:comments>
		</item>
		<item>
		<title>UITextField &#8211; A Complete API Overview</title>
		<link>http://icodeblog.com/2010/01/04/uitextfield-a-complete-api-overview/</link>
		<comments>http://icodeblog.com/2010/01/04/uitextfield-a-complete-api-overview/#comments</comments>
		<pubDate>Mon, 04 Jan 2010 17:58:18 +0000</pubDate>
		<dc:creator><![CDATA[brandontreb]]></dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Intermediate]]></category>
		<category><![CDATA[iPhone Coding]]></category>
		<category><![CDATA[iphone programming]]></category>
		<category><![CDATA[objective-c]]></category>
		<category><![CDATA[UITextField]]></category>

		<guid isPermaLink="false">http://icodeblog.com/?p=1547</guid>
		<description><![CDATA[The UITextField is probably one of the most commonly used UI controls on the iPhone. It is the primary method of user input via the keyboard and provides a great deal of additional functionality.
With the success of our las API tutorial on <a href="/2009/08/26/objective-c-tutorial-nsarray/">NSArray</a>, I thought I would do another walkthrough, this time on UITextField. I will be explaining all of the properties for it as well as bringing up some functionality that you may not have known about.
Text Attributes
The  ...]]></description>
				<content:encoded><![CDATA[<p style="clear: both;">The UITextField is probably one of the most commonly used UI controls on the iPhone. It is the primary method of user input via the keyboard and provides a great deal of additional functionality.</p>
<p>With the success of our las API tutorial on <a href="/2009/08/26/objective-c-tutorial-nsarray/">NSArray</a>, I thought I would do another walkthrough, this time on UITextField. I will be explaining all of the properties for it as well as bringing up some functionality that you may not have known about.</p>
<h2>Text Attributes</h2>
<p style="clear: both;">The attributes have to do with the actual text inside of the UITextField.<br />
<style type="text/css"> table.api td {border:1px solid #777;} </style>
</p>
<table class="api" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>text</td>
<td>The text displayed in the UITextField</td>
</tr>
<tr>
<td>placeholder</td>
<td>The text that gets displayed prior to the user entering in anything. This text is usually a lighter color than the primary text to denote that it will be replaced.</td>
</tr>
<tr>
<td>font</td>
<td>The font of the text to be displayed. You can set it like this</td>
</tr>
<tr>
<td>textColor</td>
<td>The color of the text that is displayed</td>
</tr>
<tr>
<td>textAlignment</td>
<td>How the text is aligned in the UITextField. The possible values for this are UITextAlignmentLeft, UITextAlignmentRight, UITextAlignmentCenter</td>
</tr>
</tbody>
</table>
<p style="clear: both;">
<p style="clear: both;">Here are some examples of using these properties.</p>
<p style="clear: both;">

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Setting the text</span>
<span style="color: #002200;">&#91;</span>myTextField setText<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;This is some text!&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Setting the placeholder</span>
<span style="color: #002200;">&#91;</span>myTextField setPlaceholder<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Type text here&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Setting the font.</span>
<span style="color: #002200;">&#91;</span>myTextField setFont<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>UIFont fontWithName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Times New Roman&quot;</span> size<span style="color: #002200;">:</span><span style="color: #2400d9;">14</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Setting the text color</span>
<span style="color: #002200;">&#91;</span>myTextField setTextColor<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>UIColor blueColor<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Setting the text alignment</span>
<span style="color: #002200;">&#91;</span>myTextField setTextAlignment<span style="color: #002200;">:</span>UITextAlignmentCenter<span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

<p style="clear: both;">
<p style="clear: both;"><img class="alignnone size-full wp-image-1570" style="display: inline; float: left; margin: 0 10px 10px 0;" src="/wp-content/uploads/2009/12/screenshot_011.png" alt="" width="298" height="50" align="left" />Here is what the UITextField would look like after we update these properties.</p>
<p style="clear: both;"><img class="alignnone size-full wp-image-1571" style="display: inline; float: left; margin: 0 10px 10px 0;" title="screenshot_02" src="/wp-content/uploads/2009/12/screenshot_021.png" alt="" width="298" height="52" align="left" /><br style="clear: both;" /></p>
<h2>Adjusting the size of the text in the UITextField</h2>
<p style="clear: both;">
<p style="clear: both;">
<p style="clear: both;">The text displayed in our UITextField can be dynamically sized based on the width of the UITextField. The benefit of this is all of the text being typed will be visible on the screen. It will shrink the text down until it reaches the default font size of 17. So, for this to make sense, you must set the font size of the UITextField to something larger than 17.</p>
<table class="api" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>adjustsFontSizeToFitWidth</td>
<td>Boolean value denoting whether to fit the font size to the width of the UITextField.</td>
</tr>
</tbody>
</table>
<p style="clear: both;">
<p style="clear: both;">Here is an example of using these properties.</p>
<p style="clear: both;">

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">&#91;</span>myTextField setFont<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>UIFont fontWithName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Times New Roman&quot;</span> size<span style="color: #002200;">:</span><span style="color: #2400d9;">30</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>myTextField setAdjustsFontSizeToFitWidth<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

<p style="clear: both;">
<p style="clear: both;"><img class="alignnone size-full wp-image-1577" style="display: inline; float: left; margin: 0 10px 10px 0;" title="screenshot_03" src="/wp-content/uploads/2009/12/screenshot_031.png" alt="" width="298" height="55"  />Here are some screenshots of the text shrinking when typing in the UITextField.</p>
<p style="clear: both;"><img class="alignnone size-full wp-image-1578" style="display: inline; float: left; margin: 0 10px 10px 0;" title="screenshot_04" src="/wp-content/uploads/2009/12/screenshot_041.png" alt="" width="298" height="48" /></p>
<p style="clear: both;"><img class="alignnone size-full wp-image-1579" style="display: inline; float: left; margin: 0 10px 10px 0;" title="screenshot_05" src="/wp-content/uploads/2009/12/screenshot_051.png" alt="" width="298" height="52"  /><br style="clear: both;" /></p>
<h2>Managing the editor&#8217;s behavior</h2>
<p style="clear: both;">
<p style="clear: both;">
<p style="clear: both;">These two properties are pretty straight forward.</p>
<table class="api" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>editing</td>
<td>Read-only boolean value letting you know if the user is currently editing the UITextField</td>
</tr>
<tr>
<td>clearsOnBeginEditing</td>
<td>Clears the text in the field every time the user begins to edit it.</td>
</tr>
</tbody>
</table>
<p style="clear: both;">
<p style="clear: both;">Not very exciting and probably doesn&#8217;t even deserve an example&#8230;</p>
<p style="clear: both;">
<p style="clear: both;">
<p style="clear: both;">
<h2>Setting the view&#8217;s background appearance</h2>
<p style="clear: both;">
<p style="clear: both;">
<p style="clear: both;">This group of properties defines how the UITextField will look. If you have ever seen a fancy input box, this is how they are doing it.</p>
<table  class="api" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>borderStyle</td>
<td>Defines the type of border for the UITextField. Possible choices are UITextBorderStyleNone, UITextBorderStyleLine, UITextBorderStyleBezel, and UITextBorderStyleRoundedRect. The default is UITextBorderStyleNone.</td>
</tr>
<tr>
<td>background</td>
<td>A UIImage representing the background image of the UITextField when it&#8217;s enabled. If this field is altered the borderStyle property is ignored.</td>
</tr>
<tr>
<td>backgroundDisabled</td>
<td>A UIImage representing the background image of the UITextField when it&#8217;s disabled.</td>
</tr>
</tbody>
</table>
<p style="clear: both;">
<p style="clear: both;">Here is are some example of using each of the border styles</p>
<p style="clear: both;">

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Border Style None</span>
<span style="color: #002200;">&#91;</span>myTextField setBorderStyle<span style="color: #002200;">:</span>UITextBorderStyleNone<span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

<p style="clear: both;">
<p style="clear: both;"><img class="alignnone size-full wp-image-1635" style="display: inline; float: left; margin: 0 10px 10px 0;" title="screenshot_05" src="/wp-content/uploads/2010/01/screenshot_051.png" alt="" width="290" height="43" align="left" /></p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Border Style Line</span>
<span style="color: #002200;">&#91;</span>myTextField setBorderStyle<span style="color: #002200;">:</span>UITextBorderStyleLine<span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

<p style="clear: both;">
<p style="clear: both;"><img class="alignnone size-full wp-image-1637" style="display: inline; float: left; margin: 0 10px 10px 0;" title="screenshot_03" src="/wp-content/uploads/2010/01/screenshot_031.png" alt="" width="288" height="41" align="left" /></p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Border Style Bezel</span>
<span style="color: #002200;">&#91;</span>myTextField setBorderStyle<span style="color: #002200;">:</span>UITextBorderStyleBezel<span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

<p style="clear: both;">
<p style="clear: both;"><img class="alignnone size-full wp-image-1636" style="display: inline; float: left; margin: 0 10px 10px 0;" title="screenshot_02" src="/wp-content/uploads/2010/01/screenshot_022.png" alt="" width="291" height="42" align="left" /></p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Border Style Rounded Rect</span>
<span style="color: #002200;">&#91;</span>myTextField setBorderStyle<span style="color: #002200;">:</span>UITextBorderStyleRoundedRect<span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

<p style="clear: both;">
<p style="clear: both;"><a class="image-link" href="/wp-content/uploads/2010/01/screenshot_041.png"><img class="alignnone size-full wp-image-1638" style="display: inline; float: left; margin: 0 10px 10px 0;" title="screenshot_04" src="/wp-content/uploads/2010/01/screenshot_041.png" alt="" width="288" height="41" align="left" /></a></p>
<p style="clear: both;"><a class="image-link" href="/wp-content/uploads/2010/01/bg1.png"><img class="alignnone size-full wp-image-1641" style="display: inline; float: left; margin: 0 10px 10px 0;" title="bg" src="/wp-content/uploads/2010/01/bg1.png" alt="" width="185" height="28" align="left" /></a>The border style is not terribly exciting. However, you can really spruce up your UITextFields using the background property. Here is an example of setting the background property to this image.</p>
<p style="clear: both;">

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;">myTextField.textAlignment <span style="color: #002200;">=</span> UITextAlignmentCenter;
myTextField.textColor <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIColor whiteColor<span style="color: #002200;">&#93;</span>;
myTextField.borderStyle <span style="color: #002200;">=</span> UITextBorderStyleNone;
myTextField.background <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIImage imageNamed<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;bg.png&quot;</span><span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

<p style="clear: both;">
<p style="clear: both;"><img class="alignnone size-full wp-image-1642" style="display: inline; float: left; margin: 0 10px 10px 0;" title="screenshot_01" src="/wp-content/uploads/2010/01/screenshot_015.png" alt="" width="288" height="46" align="left" />and the result&#8230; Looks pretty good ehh? One GOTCHA that I want to point out here is, to get the background property to work correctly, you must set the boderStyle to anything other than UITextBorderStyleRoundedRect. Otherwise, the default UITextField will be displayed.<br />
Setting the view&#8217;s background appearance</p>
<p style="clear: both;">
<h2>Managing Overlay Views</h2>
<p>Another interesting way of customizing your UITextFields is to use an overlay. UITextField offers a left and right overlay for your UITextFields. Here are the properties:</p>
<table class="api" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>clearButtonMode</td>
<td>The circled X that gets displayed when typing. Used to clear out the text. Possible values: UITextFieldViewModeNever, UITextFieldViewModeWhileEditing, UITextFieldViewModeUnlessEditing, UITextFieldViewModeAlways</td>
</tr>
<tr>
<td>leftView</td>
<td>The view that appears to the left inside a UITextField. This could be something like a magnifying glass for search.</td>
</tr>
<tr>
<td>leftViewMode</td>
<td>Works like clearButtonMode, but toggles the leftView.</td>
</tr>
<tr>
<td>rightView</td>
<td>Same as leftView, except it aligns to the right.</td>
</tr>
<tr>
<td>rightViewMode</td>
<td>Same as leftViewMode but controls the rightView</td>
</tr>
</tbody>
</table>
<p>Let&#8217;s take a look at how adjusting the leftView works:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;">UIImageView <span style="color: #002200;">*</span> myView <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  imageNamed <span style="color: #002200;">:</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;wordpress.png&quot;</span> <span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>myTextField  setLeftView <span style="color: #002200;">:</span>myView<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span> myTextField   setLeftViewMode<span style="color: #002200;">:</span> UITextFieldViewModeAlways<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>myView release <span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

<p><img class="alignnone size-full wp-image-1651" title="screenshot_01" src="/wp-content/uploads/2010/01/screenshot_0111.png" alt="" width="291" height="44" /></p>
<p>As you can see, the text aligns after the image.  This is a very simple way to really spruce up your UITextFields.</p>
<p>The last thing we are going to discuss is showing and hiding the keyboard.</p>
<h2>Showing and Hiding The Keyboard</h2>
<p>To show the keyboard:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">&#91;</span>myTextField becomeFirstResponder<span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

<p>To hide the keyboard</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">&#91;</span>myTextField resignFirstResponder<span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

<p>Well, I hope you have enjoyed this tutorial on the <strong>UITextField</strong>.  I would love to see links to some interesting custom UITextFields in the comments, so please post them.  Thanks for reading and happy iCoding!</p>
]]></content:encoded>
			<wfw:commentRss>http://icodeblog.com/2010/01/04/uitextfield-a-complete-api-overview/feed/</wfw:commentRss>
		<slash:comments>29</slash:comments>
		</item>
		<item>
		<title>Objective C 2.0: An Intro &#8211; Part 2</title>
		<link>http://icodeblog.com/2009/06/29/objective-c-20-an-intro-part-2/</link>
		<comments>http://icodeblog.com/2009/06/29/objective-c-20-an-intro-part-2/#comments</comments>
		<pubDate>Tue, 30 Jun 2009 01:47:15 +0000</pubDate>
		<dc:creator><![CDATA[Collin]]></dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[beginner]]></category>
		<category><![CDATA[Introduction]]></category>
		<category><![CDATA[objective-c]]></category>

		<guid isPermaLink="false">http://icodeblog.com/?p=1008</guid>
		<description><![CDATA[Introduction
Hello everyone, welcome to my fourth screeencast. This is the second in my series introducing readers to Objective C. Let&#8217;s dive in.
Skill Level Beginner
Source Code

<a title="Using NSXMLParser to Pull UIImages from the Web" href="http://losectrl-gaincommand.com/iCodeBlogHelper/Tutorial4/iCodeBlogGetsClassy.zip">Available Here</a>
Screencast
I film myself coding out the entire sample project for each post. I personally think going through the Screencast is the best way to learn. But feel free to look through the slides and text if that suites you better.
<a href="http://vimeo.com/5513767">iCodeBlog Tutorial: Objective C 2.0 An  ...]]></description>
				<content:encoded><![CDATA[<h3><span style="color: #ff6600;">Introduction</span></h3>
<p>Hello everyone, welcome to my fourth screeencast. This is the second in my series introducing readers to Objective C. Let&#8217;s dive in.</p>
<h2><span style="color: #000000;">Skill Level</span> <span style="color: #008000;">Beginner</span></h2>
<h1><span style="color: #ff6600;">Source Code<br />
</span></h1>
<h3><a title="Using NSXMLParser to Pull UIImages from the Web" href="http://losectrl-gaincommand.com/iCodeBlogHelper/Tutorial4/iCodeBlogGetsClassy.zip"><span style="text-decoration: underline;">Available Here</span></a></h3>
<h1><span style="color: #ff6600;">Screencast</span></h1>
<p>I film myself coding out the entire sample project for each post. I personally think going through the Screencast is the best way to learn. But feel free to look through the slides and text if that suites you better.</p>
<p><a href="http://vimeo.com/5513767">iCodeBlog Tutorial: Objective C 2.0 An Intro Part 2</a> from <a href="http://vimeo.com/user2008025">Collin Ruffenach</a> on <a href="http://vimeo.com">Vimeo</a>.</p>
<h1><span style="color: #ff6600;">Tutorial</span></h1>
<p><img class="aligncenter size-full wp-image-1010" src="/wp-content/uploads/2009/06/objectivec20anintropt20011.png" alt="objectivec20anintropt2001" width="620" height="465" /></p>
<p><img class="aligncenter size-full wp-image-1010" src="/wp-content/uploads/2009/06/objectivec20anintropt20021.png" alt="objectivec20anintropt2002" width="620" height="465" /></p>
<p><img class="aligncenter size-full wp-image-1010" src="/wp-content/uploads/2009/06/objectivec20anintropt20031.png" alt="objectivec20anintropt2003" width="620" height="465" /></p>
<p><img class="aligncenter size-full wp-image-1010" src="/wp-content/uploads/2009/06/objectivec20anintropt20041.png" alt="objectivec20anintropt2004" width="620" height="465" /></p>
<p><img class="aligncenter size-full wp-image-1010" src="/wp-content/uploads/2009/06/objectivec20anintropt20051.png" alt="objectivec20anintropt2005" width="620" height="465" /></p>
<p><img class="aligncenter size-full wp-image-1010" src="/wp-content/uploads/2009/06/objectivec20anintropt20061.png" alt="objectivec20anintropt2006" width="620" height="465" /></p>
<p><img class="aligncenter size-full wp-image-1010" src="/wp-content/uploads/2009/06/objectivec20anintropt20071.png" alt="objectivec20anintropt2007" width="620" height="465" /></p>
<p><img class="aligncenter size-full wp-image-1010" src="/wp-content/uploads/2009/06/objectivec20anintropt20081.png" alt="objectivec20anintropt2008" width="620" height="465" /></p>
<p><img class="aligncenter size-full wp-image-1010" src="/wp-content/uploads/2009/06/objectivec20anintropt20091.png" alt="objectivec20anintropt2009" width="620" height="465" /></p>
<p><img class="aligncenter size-full wp-image-1010" src="/wp-content/uploads/2009/06/objectivec20anintropt20101.png" alt="objectivec20anintropt2010" width="620" height="465" /></p>
<p><img class="aligncenter size-full wp-image-1010" src="/wp-content/uploads/2009/06/objectivec20anintropt20111.png" alt="objectivec20anintropt2011" width="620" height="465" /></p>
<p><img class="aligncenter size-full wp-image-1010" src="/wp-content/uploads/2009/06/objectivec20anintropt20121.png" alt="objectivec20anintropt2012" width="620" height="465" /></p>
<h2><span style="color: #ff6600;">iCodeBlogClass.m:</span></h2>
<pre style="padding-left: 60px;">@implementation iCodeBlogClass

@synthesize name;

-init
{
	return self;
}

@end</pre>
<p><img class="aligncenter size-full wp-image-1010" src="/wp-content/uploads/2009/06/objectivec20anintropt20131.png" alt="objectivec20anintropt2013" width="620" height="465" /></p>
<p><img class="aligncenter size-full wp-image-1010" src="/wp-content/uploads/2009/06/objectivec20anintropt20141.png" alt="objectivec20anintropt2014" width="620" height="465" /></p>
<p><img class="aligncenter size-full wp-image-1010" src="/wp-content/uploads/2009/06/objectivec20anintropt20151.png" alt="objectivec20anintropt2015" width="620" height="465" /></p>
<h2><span style="color: #ff6600;">iCodeBlogGetsClassy.h:</span></h2>
<pre style="padding-left: 60px;">#import "iCodeBlogClass.h"</pre>
<p><img class="aligncenter size-full wp-image-1010" src="/wp-content/uploads/2009/06/objectivec20anintropt20161.png" alt="objectivec20anintropt2016" width="620" height="465" /></p>
<p><img class="aligncenter size-full wp-image-1010" src="/wp-content/uploads/2009/06/objectivec20anintropt20131.png" alt="objectivec20anintropt2013" width="620" height="465" /></p>
<p><img class="aligncenter size-full wp-image-1010" src="/wp-content/uploads/2009/06/objectivec20anintropt20141.png" alt="objectivec20anintropt2014" width="620" height="465" /></p>
<p><img class="aligncenter size-full wp-image-1010" src="/wp-content/uploads/2009/06/objectivec20anintropt20151.png" alt="objectivec20anintropt2015" width="620" height="465" /></p>
<h2><span style="color: #ff6600;">iCodeBlogGetsClassy.h:</span></h2>
<pre style="padding-left: 60px;">iCodeBlogClass *myNewObject = [[iCodeBlogClass alloc] init];
NSLog(@"%@", myNewObject);</pre>
<p><img class="aligncenter size-full wp-image-1010" src="/wp-content/uploads/2009/06/objectivec20anintropt20171.png" alt="objectivec20anintropt2017" width="620" height="465" /></p>
<p><img class="aligncenter size-full wp-image-1010" src="/wp-content/uploads/2009/06/objectivec20anintropt20181.png" alt="objectivec20anintropt2018" width="620" height="465" /></p>
<p><img class="aligncenter size-full wp-image-1010" src="/wp-content/uploads/2009/06/objectivec20anintropt20191.png" alt="objectivec20anintropt2019" width="620" height="465" /></p>
<p><img class="aligncenter size-full wp-image-1010" src="/wp-content/uploads/2009/06/objectivec20anintropt20201.png" alt="objectivec20anintropt2020" width="620" height="465" /></p>
<p><img class="aligncenter size-full wp-image-1010" src="/wp-content/uploads/2009/06/objectivec20anintropt20211.png" alt="objectivec20anintropt2021" width="620" height="465" /></p>
<p><img class="aligncenter size-full wp-image-1010" src="/wp-content/uploads/2009/06/objectivec20anintropt20221.png" alt="objectivec20anintropt2020" width="622" height="465" /></p>
<p><img class="aligncenter size-full wp-image-1010" src="/wp-content/uploads/2009/06/objectivec20anintropt20231.png" alt="objectivec20anintropt2023" width="620" height="465" /></p>
<p><img class="aligncenter size-full wp-image-1010" src="/wp-content/uploads/2009/06/objectivec20anintropt20241.png" alt="objectivec20anintropt2024" width="620" height="465" /></p>
<h2><span style="color: #ff6600;">iCodeBlogClass.h:</span></h2>
<pre style="padding-left: 60px;">#import &lt;Foundation/Foundation.h&gt;

@interface iCodeBlogClass : NSObject
{
	NSString *name;
}

@property (nonatomic, retain) NSString *name;

@end</pre>
<p><img class="aligncenter size-full wp-image-1010" src="/wp-content/uploads/2009/06/objectivec20anintropt20251.png" alt="objectivec20anintropt2025" width="620" height="465" /></p>
<h2><span style="color: #ff6600;">iCodeBlogClass.m:</span></h2>
<pre style="padding-left: 60px;">@synthesize name;</pre>
<p><img class="aligncenter size-full wp-image-1010" src="/wp-content/uploads/2009/06/objectivec20anintropt20261.png" alt="objectivec20anintropt2026" width="620" height="465" /></p>
<p><img class="aligncenter size-full wp-image-1010" src="/wp-content/uploads/2009/06/objectivec20anintropt20271.png" alt="objectivec20anintropt2027" width="620" height="465" /></p>
<h2><span style="color: #ff6600;">iCodeBlogGetsClassy.m:</span></h2>
<pre style="padding-left: 60px;">iCodeBlogClass *myNewObject = [[iCodeBlogClass alloc] init];
[myNewObject setName:@"Collin"];

NSLog(@"%@", [myNewObject name]);</pre>
<p><img class="aligncenter size-full wp-image-1010" src="/wp-content/uploads/2009/06/objectivec20anintropt20281.png" alt="objectivec20anintropt2028" width="620" height="465" /></p>
<p><img class="aligncenter size-full wp-image-1010" src="/wp-content/uploads/2009/06/objectivec20anintropt20291.png" alt="objectivec20anintropt2029" width="620" height="465" /></p>
<p><img class="aligncenter size-full wp-image-1010" src="/wp-content/uploads/2009/06/objectivec20anintropt20301.png" alt="objectivec20anintropt2030" width="620" height="465" /></p>
<h2><span style="color: #ff6600;">iCodeBlogClass.m:</span></h2>
<pre style="padding-left: 60px;">-initWithName:(NSString *)inputName
{
     self.name = inputName;
     return self;
}</pre>
<p><img class="aligncenter size-full wp-image-1010" src="/wp-content/uploads/2009/06/objectivec20anintropt20311.png" alt="objectivec20anintropt2031" width="620" height="465" /></p>
<p><img class="aligncenter size-full wp-image-1010" src="/wp-content/uploads/2009/06/objectivec20anintropt20321.png" alt="objectivec20anintropt2032" width="620" height="465" /></p>
<h2><span style="color: #ff6600;">iCodeBlogGetsClassy.m:</span></h2>
<pre style="padding-left: 60px;">iCodeBlogClass *myNewObject = [[iCodeBlogClass alloc] initWithName:@"Collin"];</pre>
<p><img class="aligncenter size-full wp-image-1010" src="/wp-content/uploads/2009/06/objectivec20anintropt20331.png" alt="objectivec20anintropt2033" width="620" height="465" /></p>
<p><img class="aligncenter size-full wp-image-1010" src="/wp-content/uploads/2009/06/objectivec20anintropt20341.png" alt="objectivec20anintropt2034" width="620" height="465" /></p>
<p><img class="aligncenter size-full wp-image-1010" src="/wp-content/uploads/2009/06/objectivec20anintropt20351.png" alt="objectivec20anintropt2035" width="620" height="465" /></p>
<p><img class="aligncenter size-full wp-image-1010" src="/wp-content/uploads/2009/06/objectivec20anintropt20361.png" alt="objectivec20anintropt2036" width="620" height="465" /></p>
<p><img class="aligncenter size-full wp-image-1010" src="/wp-content/uploads/2009/06/objectivec20anintropt20371.png" alt="objectivec20anintropt2037" width="620" height="465" /></p>
<p><img class="aligncenter size-full wp-image-1010" src="/wp-content/uploads/2009/06/objectivec20anintropt20381.png" alt="objectivec20anintropt2038" width="620" height="465" /></p>
<p><img class="aligncenter size-full wp-image-1010" src="/wp-content/uploads/2009/06/objectivec20anintropt20391.png" alt="objectivec20anintropt2039" width="620" height="465" /></p>
<h2><span style="color: #ff6600;">iCodeBlogGetsClassy.m:</span></h2>
<pre style="padding-left: 60px;">iCodeBlogClass *person1 = [[iCodeBlogClass alloc] initWithName:@"Collin"];</pre>
<p><img class="aligncenter size-full wp-image-1010" src="/wp-content/uploads/2009/06/objectivec20anintropt20401.png" alt="objectivec20anintropt2040" width="620" height="465" /></p>
<h2><span style="color: #ff6600;">iCodeBlogGetsClassy.m:</span></h2>
<pre style="padding-left: 60px;">	iCodeBlogClass *person1 = [[iCodeBlogClass alloc] initWithName:@"Collin"];
	iCodeBlogClass *person2 = [[iCodeBlogClass alloc] initWithName:@"Martin"];
	iCodeBlogClass *person3 = [[iCodeBlogClass alloc] initWithName:@"vtsib"];
	iCodeBlogClass *person4 = [[iCodeBlogClass alloc] initWithName:@"AppStoreMod"];
	iCodeBlogClass *person5 = [[iCodeBlogClass alloc] initWithName:@"Matt"];
	iCodeBlogClass *person6 = [[iCodeBlogClass alloc] initWithName:@"Brendan"];
	iCodeBlogClass *person7 = [[iCodeBlogClass alloc] initWithName:@"Geoff"];
	iCodeBlogClass *person8 = [[iCodeBlogClass alloc] initWithName:@"RobotGrrl"];
	iCodeBlogClass *person9 = [[iCodeBlogClass alloc] initWithName:@"brandon"];
	iCodeBlogClass *person10 = [[iCodeBlogClass alloc] initWithName:@"Aaron"];
	iCodeBlogClass *person11 = [[iCodeBlogClass alloc] initWithName:@"Brian"];
	iCodeBlogClass *person12 = [[iCodeBlogClass alloc] initWithName:@"Fernando"];
	iCodeBlogClass *person13 = [[iCodeBlogClass alloc] initWithName:@"Chad"];</pre>
<p><img class="aligncenter size-full wp-image-1010" src="/wp-content/uploads/2009/06/objectivec20anintropt20411.png" alt="objectivec20anintropt2041" width="620" height="465" /></p>
<h2><span style="color: #ff6600;">iCodeBlogGetsClassy.m:</span></h2>
<pre style="padding-left: 60px;">NSMutableArray *myArray = [[NSMutableArray alloc] init];</pre>
<p><img class="aligncenter size-full wp-image-1010" src="/wp-content/uploads/2009/06/objectivec20anintropt20421.png" alt="objectivec20anintropt2042" width="620" height="465" /></p>
<h2><span style="color: #ff6600;">iCodeBlogGetsClassy.m:</span></h2>
<pre style="padding-left: 60px;">	[myArray addObject:person1];
	[myArray addObject:person2];
	[myArray addObject:person3];
	[myArray addObject:person4];
	[myArray addObject:person5];
	[myArray addObject:person6];
	[myArray addObject:person7];
	[myArray addObject:person8];
	[myArray addObject:person9];
	[myArray addObject:person10];
	[myArray addObject:person11];
	[myArray addObject:person12];
	[myArray addObject:person13];</pre>
<p><img class="aligncenter size-full wp-image-1010" src="/wp-content/uploads/2009/06/objectivec20anintropt20431.png" alt="objectivec20anintropt2043" width="620" height="465" /></p>
<p><img class="aligncenter size-full wp-image-1010" src="/wp-content/uploads/2009/06/objectivec20anintropt20441.png" alt="objectivec20anintropt2044" width="620" height="465" /></p>
<p><img class="aligncenter size-full wp-image-1010" src="/wp-content/uploads/2009/06/objectivec20anintropt20451.png" alt="objectivec20anintropt2045" width="620" height="465" /></p>
<h2><span style="color: #ff6600;">iCodeBlogGetsClassy.m:</span></h2>
<pre style="padding-left: 60px;">	for(iCodeBlogClass *myObject in myArray)
	{
		NSLog(@"%@", myObject);
	}</pre>
<p><img class="aligncenter size-full wp-image-1010" src="/wp-content/uploads/2009/06/objectivec20anintropt20461.png" alt="objectivec20anintropt2046" width="620" height="465" /></p>
<p><img class="aligncenter size-full wp-image-1010" src="/wp-content/uploads/2009/06/objectivec20anintropt20471.png" alt="objectivec20anintropt2047" width="620" height="465" /></p>
<p><img class="aligncenter size-full wp-image-1010" src="/wp-content/uploads/2009/06/objectivec20anintropt20481.png" alt="objectivec20anintropt2048" width="620" height="465" /></p>
<h2><span style="color: #ff6600;">iCodeBlogGetsClassy.m:</span></h2>
<pre style="padding-left: 60px;">-(NSString *)description
{
	return [NSString stringWithFormat:@"The name of the object is %@", self.name];
}
<img class="aligncenter size-full wp-image-1010" src="/wp-content/uploads/2009/06/objectivec20anintropt20491.png" alt="objectivec20anintropt2049" width="620" height="465" /></pre>
]]></content:encoded>
			<wfw:commentRss>http://icodeblog.com/2009/06/29/objective-c-20-an-intro-part-2/feed/</wfw:commentRss>
		<slash:comments>30</slash:comments>
		</item>
		<item>
		<title>Objective C 2.0: An Intro &#8211; Part 1</title>
		<link>http://icodeblog.com/2009/06/18/objective-c-20-an-intro-part-1/</link>
		<comments>http://icodeblog.com/2009/06/18/objective-c-20-an-intro-part-1/#comments</comments>
		<pubDate>Thu, 18 Jun 2009 18:15:08 +0000</pubDate>
		<dc:creator><![CDATA[Collin]]></dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[beginner]]></category>
		<category><![CDATA[Introduction]]></category>
		<category><![CDATA[objective-c]]></category>

		<guid isPermaLink="false">http://icodeblog.com/?p=900</guid>
		<description><![CDATA[Introduction
Hello everyone, welcome to my second screeencast. This is going to be the first in a series of screencasts that are focused at people just beginning to work with Objective C and Cocoa. For many reasons the beginnings of learning cocoa development can be frusterating and lonely to a point. Only now is Objective C and Cocoa development gaining the kind of momentum to drive the creation of resources such as iCodeBlog and others.
Skill Level Beginner
This is not going to  ...]]></description>
				<content:encoded><![CDATA[<h3><span style="color: #ff6600;">Introduction</span></h3>
<p>Hello everyone, welcome to my second screeencast. This is going to be the first in a series of screencasts that are focused at people just beginning to work with Objective C and Cocoa. For many reasons the beginnings of learning cocoa development can be frusterating and lonely to a point. Only now is Objective C and Cocoa development gaining the kind of momentum to drive the creation of resources such as iCodeBlog and others.</p>
<h2><span style="color: #000000;">Skill Level</span> <span style="color: #008000;">Beginner</span></h2>
<p>This is not going to be a tutorial for someone who has never had any experience with programming. I aviod getting into the Object Oriented Methodology side of things. All you will need to know for this tutorail is generally the purpose of Classes, Methods and Objects. If you have done any work with Java, C, C++ or C# you should be able to follow the content no problem.</p>
<h1><span style="color: #ff6600;">Screencast</span></h1>
<p>I film myself coding out the entire sample project for each post. I personally think going through the Screencast is the best way to learn. But feel free to look through the slides and text if that suites you better.</p>
<h3><a title="Objective C 2.0: An Intro Part 1 Screencast" href="http://vimeo.com/5654298" target="_blank"><span style="text-decoration: underline;">Objective C 2.0: An Intro &#8211; Part 1</span></a></h3>
<h1><span style="color: #ff6600;">Tutorial</span></h1>
<h1><span style="color: #ff6600;"><img class="aligncenter size-full wp-image-902" src="/wp-content/uploads/2009/06/picture-34.png" alt="picture-3" width="620" height="387" /></span></h1>
<h1><span style="color: #ff6600;"><img class="aligncenter size-full wp-image-903" src="/wp-content/uploads/2009/06/picture-42.png" alt="picture-4" width="620" height="387" /></span></h1>
<h1><span style="color: #ff6600;"><img class="aligncenter size-full wp-image-904" src="/wp-content/uploads/2009/06/picture-62.png" alt="picture-6" width="620" height="387" /></span></h1>
<h1><span style="color: #ff6600;"><img class="aligncenter size-full wp-image-905" src="/wp-content/uploads/2009/06/picture-72.png" alt="picture-7" width="620" height="387" /></span></h1>
<h1><span style="color: #ff6600;"><img class="aligncenter size-full wp-image-907" src="/wp-content/uploads/2009/06/picture-92.png" alt="picture-9" width="620" height="387" /></span></h1>
<h1><span style="color: #ff6600;"><img class="aligncenter size-full wp-image-908" src="/wp-content/uploads/2009/06/picture-102.png" alt="picture-10" width="620" height="387" /></span></h1>
<h1><span style="color: #ff6600;"><img class="aligncenter size-full wp-image-909" src="/wp-content/uploads/2009/06/picture-112.png" alt="picture-11" width="620" height="387" /></span></h1>
<h1><span style="color: #ff6600;"><img class="aligncenter size-full wp-image-910" src="/wp-content/uploads/2009/06/picture-122.png" alt="picture-12" width="620" height="387" /></span></h1>
<h1><span style="color: #ff6600;"><img class="aligncenter size-full wp-image-911" src="/wp-content/uploads/2009/06/picture-132.png" alt="picture-13" width="620" height="387" /></span></h1>
<h1><span style="color: #ff6600;"><img class="aligncenter size-full wp-image-912" src="/wp-content/uploads/2009/06/picture-142.png" alt="picture-14" width="620" height="387" /></span></h1>
<h1><span style="color: #ff6600;"><img class="aligncenter size-full wp-image-913" src="/wp-content/uploads/2009/06/picture-152.png" alt="picture-15" width="620" height="387" /></span></h1>
<h1><span style="color: #ff6600;"><img class="aligncenter size-full wp-image-914" src="/wp-content/uploads/2009/06/picture-162.png" alt="picture-16" width="620" height="387" /></span></h1>
<h2><span style="color: #ff6600;">Instructions</span></h2>
<ol>
<li><span style="color: #ff6600;"><span style="color: #000000;">Open xCode</span></span></li>
<li><span style="color: #ff6600;"><span style="color: #000000;">File -&gt; New Project</span></span></li>
<li><span style="color: #ff6600;"><span style="color: #000000;">Start a new View based iPhone Project. The type of project you create really doesn&#8217;t matter for this exercise since we will only be programming a for loop to print and not creating any User Interface.</span></span></li>
<li><span style="color: #ff6600;"><span style="color: #000000;">Call the project iCodeBlogCounter</span></span></li>
<li><span style="color: #ff6600;"><span style="color: #000000;">After saving the project you will be confronted with a screen looking something like this.</span></span></li>
<p><span style="color: #ff6600;"><span style="color: #000000;"><img class="aligncenter size-full wp-image-938" src="/wp-content/uploads/2009/06/xcodefirstscreen1.png" alt="xcodefirstscreen" width="620" height="387" /></span></span></p>
<li><span style="color: #ff6600;"><span style="color: #000000;">If you look in the top left hand corner you will see a folder called Classes. If you click the little black triangle to the left of the folder you will see what is included in the classes folder. In there you should see a class called iCodeBlogCounterAppDelegate.m. This is the file we will be working with. Click on it and you will see its contents appear in the editor window.</span></span></li>
<p><span style="color: #ff6600;"><span style="color: #000000;"><img class="aligncenter size-full wp-image-937" src="/wp-content/uploads/2009/06/groupsandfilescloseup1.png" alt="groupsandfilescloseup" width="280" height="275" /></span></span></p>
<li>We will be working with the &#8211; (void)applicationDidFinishLaunching:(UIApplication *)application  method. This method is called when the application finished launching. We will be entering some very simple code that will simple count from 0 to 99 and print the numbers in the terminal window. Here is what the method should look like:</li>
<p><span style="color: #ff6600;"><span style="color: #000000;"><img class="aligncenter size-full wp-image-935" src="/wp-content/uploads/2009/06/applicationdidfinishlaunchingcloseup1.png" alt="applicationdidfinishlaunchingcloseup" width="415" height="173" /></span></span></p>
<p><span style="color: #ff6600;"><span style="color: #000000;">The code here is:</span></span></p>
<pre><span style="color: #ff6600;"><span style="color: #000000;">for(int i = 0; i &lt; 100; i++)
{
     NSLog(@"The current number is: %d", i);
}
</span></span></pre>
<li><span style="color: #ff6600;"><span style="color: #000000;">That is all we need to do for this app. Now time to see it in action. To bring up the terminal window hit SHIFT + APPLE + R, this should bring up a blank window with maybe a line of text in it. Now click Build and Run or hit Apple + R. The terminal windows should say &#8220;The current number is 0&#8243; all the way to &#8220;The current  is 99&#8243;. Here is a screenshot of my window.</span></span></li>
</ol>
<p><span style="color: #ff6600;"><span style="color: #000000;"><img class="aligncenter size-full wp-image-936" src="/wp-content/uploads/2009/06/counterterminalwindow1.png" alt="counterterminalwindow" width="620" height="375" /><br />
</span></span></p>
<h1><span style="color: #ff6600;"><img class="aligncenter size-full wp-image-915" src="/wp-content/uploads/2009/06/picture-172.png" alt="picture-17" width="620" height="387" /></span></h1>
<h1><span style="color: #ff6600;"><img class="aligncenter size-full wp-image-916" src="/wp-content/uploads/2009/06/picture-182.png" alt="picture-18" width="620" height="387" /></span></h1>
<h1><span style="color: #ff6600;"><img class="aligncenter size-full wp-image-917" src="/wp-content/uploads/2009/06/picture-192.png" alt="picture-19" width="620" height="387" /></span></h1>
<h1><span style="color: #ff6600;"><img class="aligncenter size-full wp-image-918" src="/wp-content/uploads/2009/06/picture-202.png" alt="picture-20" width="620" height="387" /></span></h1>
<h1><span style="color: #ff6600;"><img class="aligncenter size-full wp-image-919" src="/wp-content/uploads/2009/06/picture-212.png" alt="picture-21" width="620" height="387" /></span></h1>
<h1><span style="color: #ff6600;"><img class="aligncenter size-full wp-image-920" src="/wp-content/uploads/2009/06/picture-222.png" alt="picture-22" width="620" height="387" /></span></h1>
<h1><span style="color: #ff6600;"><img class="aligncenter size-full wp-image-921" src="/wp-content/uploads/2009/06/picture-242.png" alt="picture-24" width="620" height="387" /></span></h1>
<h1><span style="color: #ff6600;"><img class="aligncenter size-full wp-image-922" src="/wp-content/uploads/2009/06/picture-252.png" alt="picture-25" width="620" height="387" /></span></h1>
<h1><span style="color: #ff6600;"><img class="aligncenter size-full wp-image-923" src="/wp-content/uploads/2009/06/picture-262.png" alt="picture-26" width="620" height="387" /></span></h1>
<h1><span style="color: #ff6600;"><img class="aligncenter size-full wp-image-924" src="/wp-content/uploads/2009/06/picture-282.png" alt="picture-28" width="620" height="387" /></span></h1>
<h2><span style="color: #ff6600;">Instructions</span></h2>
<ol>
<li><span style="color: #ff6600;"><span style="color: #000000;">Open xCode</span></span></li>
<li><span style="color: #ff6600;"><span style="color: #000000;">File -&gt; New Project</span></span></li>
<li><span style="color: #ff6600;"><span style="color: #000000;">Start a new View based iPhone Project. The type of project you create really doesn&#8217;t matter for this exercise since we will only be programming a for loop to print and not creating any User Interface.</span></span></li>
<li><span style="color: #ff6600;"><span style="color: #000000;">Call the project iCodeBlogGetURLText</span></span></li>
<li><span style="color: #ff6600;"><span style="color: #000000;">Once the project is open go into the iCodeBlogURLTextAppDelegate.m file.</span></span></li>
<li><span style="color: #ff6600;"><span style="color: #000000;">Add this code to the </span></span>- (void)applicationDidFinishLaunching:(UIApplication *)application method</li>
<pre><span style="color: #ff6600;"><span style="color: #000000;">NSString *myURLString = @"http://losectrl-gaincommand.com/iCodeBlogHelper/Tutorial2/iCodeBlog.txt";
NSURL *myURL = [NSURL URLWithString:myURLString];
NSString *myString = [[NSString alloc] stringWithContentsofURL:myURL];

NSLog(@"The string from the internet is: %@", myString);
</span></span></pre>
<li><span style="color: #ff6600;"><span style="color: #000000;">If you bring up  the terminal window and Build and Run the App. You should see:</span></span></li>
</ol>
<p><span style="color: #ff6600;"><span style="color: #000000;"><img class="aligncenter size-full wp-image-942" src="/wp-content/uploads/2009/06/urltextfileterminalwindow1.png" alt="urltextfileterminalwindow" width="501" height="443" /><br />
</span></span></p>
<h1><span style="color: #ff6600;"><img class="aligncenter size-full wp-image-925" src="/wp-content/uploads/2009/06/picture-292.png" alt="picture-29" width="620" height="387" /></span></h1>
<h2><span style="color: #ff6600;">Instructions</span></h2>
<ol>
<li><span style="color: #ff6600;"><span style="color: #000000;">Go back to the code we just wrote.<br />
</span></span></li>
<li><span style="color: #ff6600;"><span style="color: #000000;">Erase the 4 lines of code we wrote and replace it with this line:</span></span></li>
<pre><span style="color: #ff6600;"><span style="color: #000000;">NSLog(@"The string from the internet is: %@", [[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://losectrl-gaincommand.com/iCodeBlogHelper/Tutorial2/iCodeBlog.txt"]]);
</span></span></pre>
<li>Running the application again should have the same output.</li>
<li>Here is a a breakdown of out new line of code.</li>
</ol>
<h1><span style="color: #ff6600;"><img class="aligncenter size-full wp-image-926" src="/wp-content/uploads/2009/06/picture-302.png" alt="picture-30" width="620" height="387" /></span></h1>
<h1><span style="color: #ff6600;"><img class="aligncenter size-full wp-image-927" src="/wp-content/uploads/2009/06/picture-312.png" alt="picture-31" width="620" height="387" /></span></h1>
]]></content:encoded>
			<wfw:commentRss>http://icodeblog.com/2009/06/18/objective-c-20-an-intro-part-1/feed/</wfw:commentRss>
		<slash:comments>54</slash:comments>
		</item>
	</channel>
</rss>
