<?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; Matt Tuzzolo</title>
	<atom:link href="/author/matt-tuzzolo/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=3.9.2</generator>
	<item>
		<title>Update: ELCImagePickerController</title>
		<link>http://icodeblog.com/2011/03/03/update-elcimagepickercontroller/</link>
		<comments>http://icodeblog.com/2011/03/03/update-elcimagepickercontroller/#comments</comments>
		<pubDate>Thu, 03 Mar 2011 00:23:14 +0000</pubDate>
		<dc:creator><![CDATA[Matt Tuzzolo]]></dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Featured]]></category>
		<category><![CDATA[Advanced]]></category>
		<category><![CDATA[ELCImagePickerController]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[UIImagePickerController]]></category>

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

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

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

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

<p>Then when the block is finished enumerating, it&#8217;ll call reloadTable as well.  On an album with roughly 1500 photos, it was pretty tough to reach the bottom of the tableview before the block finished enumerating.  So this is a pretty decent solution to the issue.  Originally, I experimented with lazy loading the assets as the user scrolled through the table, but ultimately wasn&#8217;t able to get the performance I wanted out of it.</p>
<p>This release of ELCImagePickerController should perform faster, and with a smaller memory footprint than before.  I hope you enjoy it.</p>
<p>You can follow me on twitter <a href="http://twitter.com/matt_tuzzolo">@matt_tuzzolo</a> or get in touch with us at <a href="http://www.elctech.com">http://www.elctech.com</a></p>
<p>ps. for an extra bonus, check out [ELCAsset toggleSelection];</p>
]]></content:encoded>
			<wfw:commentRss>http://icodeblog.com/2011/03/03/update-elcimagepickercontroller/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Implementing UITableView Sections from an NSArray of NSDictionary Objects</title>
		<link>http://icodeblog.com/2010/12/10/implementing-uitableview-sections-from-an-nsarray-of-nsdictionary-objects/</link>
		<comments>http://icodeblog.com/2010/12/10/implementing-uitableview-sections-from-an-nsarray-of-nsdictionary-objects/#comments</comments>
		<pubDate>Fri, 10 Dec 2010 23:35:06 +0000</pubDate>
		<dc:creator><![CDATA[Matt Tuzzolo]]></dc:creator>
				<category><![CDATA[Featured]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://icodeblog.com/?p=2831</guid>
		<description><![CDATA[If you&#8217;re working with a remote Web Service, your apps are probably displaying TableViews of objects.  As soon as your dataset grows beyond 20 or 30 objects, it&#8217;s time to implement sections in your Table View.  I&#8217;m going to show you how you can do this without too much trouble.  In this example, we&#8217;ll use an array of dictionary objects (Books) to construct a single &#8216;sections&#8217; dictionary that will be the basis for our TableView datasource.
Before we  ...]]></description>
				<content:encoded><![CDATA[<p>If you&#8217;re working with a remote Web Service, your apps are probably displaying TableViews of objects.  As soon as your dataset grows beyond 20 or 30 objects, it&#8217;s time to implement sections in your Table View.  I&#8217;m going to show you how you can do this without too much trouble.  In this example, we&#8217;ll use an array of dictionary objects (Books) to construct a single &#8216;sections&#8217; dictionary that will be the basis for our TableView datasource.</p>
<p>Before we get started, you might want to clone the git repo containing the demo Xcode project for this post:</p>
<pre>
git clone git@github.com:elc/ICB_SectionedTableViewDemo.git
</pre>
<p>First let&#8217;s take a look at the UITableViewDataSource protocol methods that we&#8217;ll be using to get sections going:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="obj-c" style="font-family:monospace;">    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {</pre></td></tr></table></div>

<p>These are the four methods needed to show section headers, as well as the section index (which is the scrubber on the right side).  Normally, with a established dataset (say one that already contains at least one item that starts with each letter of the alphabet), you can take some short cuts and return 26 for numberOfSectionsInTableView, hard code an array of A-Z to use for titleForHeaderInSection, etc.  I&#8217;ve done that a few times for static datasets, but it&#8217;s really the wrong solution if you&#8217;re pulling data from a user-generated or otherwise dynamic datastore.</p>
<p>Instead what we&#8217;ll do is build a new dictionary entirely.  The keys of this dictionary will correspond to our tableview section titles, and the values for those keys will be arrays containing our dictionary objects; these represent cells in the tableview.</p>
<p>Let&#8217;s start by establishing our sections:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="obj-c" style="font-family:monospace;">&nbsp;
    BOOL found;
&nbsp;
    // Loop through the books and create our keys
    for (NSDictionary *book in self.books)
    {
        NSString *c = [[book objectForKey:@&quot;title&quot;] substringToIndex:1];
&nbsp;
        found = NO;
&nbsp;
        for (NSString *str in [self.sections allKeys])
        {
            if ([str isEqualToString:c])
            {
                found = YES;
            }
        }
&nbsp;
        if (!found)
        {
            [self.sections setValue:[[NSMutableArray alloc] init] forKey:c];
        }
    }</pre></td></tr></table></div>

<p>If you&#8217;re implementing this code as you go, make sure you&#8217;ve setup &#8216;sections&#8217; as a NSMutableDictionary property of your datasource (which is usually just your UITableViewController).  So after this block of code runs, you&#8217;ll have a dictionary with a key for each unique first character of book->title.  So if your books are:</p>
<p>On Intelligence<br />
On The Road<br />
Ishmael<br />
Dune</p>
<p>Your self.sections will be:</p>
<p>&#8216;O&#8217;  => empty NSMutableArray<br />
&#8216;I&#8217;    => empty NSMutableArray<br />
&#8216;D&#8217;  => empty NSMutableArray</p>
<p>Notice that the keys in your NSDictionary aren&#8217;t sorted alphabetically.  They would be if your initial datasource (self.books) was sorted alphabetically; but there&#8217;s no guarantee on that.  I&#8217;ll show you how to deal with this in a minute.</p>
<p>So the next step is to populate the empty arrays in self.sections with the appropriate NSDictionary objects from self.books:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="obj-c" style="font-family:monospace;">    // Loop again and sort the books into their respective keys
    for (NSDictionary *book in self.books)
    {
        [[self.sections objectForKey:[[book objectForKey:@&quot;title&quot;] substringToIndex:1]] addObject:book];
    }</pre></td></tr></table></div>

<p>Now the books are compartmentalized into their respective sections.  Next we need to sort the books within each section:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="obj-c" style="font-family:monospace;">    // Sort each section array
    for (NSString *key in [self.sections allKeys])
    {
        [[self.sections objectForKey:key] sortUsingDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@&quot;title&quot; ascending:YES]]];
    }</pre></td></tr></table></div>

<p>Again, we&#8217;re basing our sort off @&#8221;title&#8221;.  I&#8217;ll leave @&#8221;title&#8221; in here for readability, but it would be wise to pull this out into it&#8217;s own NSString *sortByProperty so you can easily change it should you decide you want to base your sections/sorting on Author, Publication Year, etc.</p>
<p>At this point you&#8217;ll probably want to drop this in too:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="obj-c" style="font-family:monospace;">    [self.tableView reloadData];</pre></td></tr></table></div>

<p>So now that we&#8217;ve constructed our self.sections, we can finally implement the UITableViewDataSource protocol methods I mentioned above:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="obj-c" style="font-family:monospace;">#pragma mark -
#pragma mark Table view data source
&nbsp;
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [[self.sections allKeys] count];
}
&nbsp;
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [[[self.sections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:section];
}
&nbsp;
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[self.sections valueForKey:[[[self.sections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:section]] count];
}
&nbsp;
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    return [[self.sections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
}</pre></td></tr></table></div>

<p>This is where things get a little confusing.  Since these methods are called with indexPath.section, which an NSInteger, and not the actual section key, we&#8217;re forced to get a little tricky.  In order to make sure that our section indexes and section titles match up correctly AND are ordered alphabetically, we have to sort allKeys each time and then reference the resulting NSArray.  It&#8217;s kind of ugly, but I&#8217;ve done all the annoying bracket matching for you already.</p>
<p>OK.  So now that we have sections out of the way, let&#8217;s show some cells&#8230;</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="obj-c" style="font-family:monospace;">- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
&nbsp;
    static NSString *CellIdentifier = @&quot;Cell&quot;;
&nbsp;
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }
&nbsp;
    NSDictionary *book = [[self.sections valueForKey:[[[self.sections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
&nbsp;
    cell.textLabel.text = [book objectForKey:@&quot;title&quot;];
    cell.detailTextLabel.text = [book objectForKey:@&quot;description&quot;];
&nbsp;
    return cell;
}</pre></td></tr></table></div>

<p>That&#8217;s it!  You should have a great looking sectioned tableView.  If you have any questions or comments, you can get a hold me<a href="http://twitter.com/matt_tuzzolo">@matt_tuzzolo</a> on Twitter.</p>
<p><a href="/wp-content/uploads/2010/12/Screen-shot-2010-12-10-at-3.02.09-PM.png"><img src="/wp-content/uploads/2010/12/Screen-shot-2010-12-10-at-3.02.09-PM.png" alt="" title="Screen shot 2010-12-10 at 3.02.09 PM" width="438" height="770" class="aligncenter size-full wp-image-2843" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://icodeblog.com/2010/12/10/implementing-uitableview-sections-from-an-nsarray-of-nsdictionary-objects/feed/</wfw:commentRss>
		<slash:comments>41</slash:comments>
		</item>
		<item>
		<title>Adding Local Weather Conditions To Your App (Part 2/2: Accessing Google&#8217;s XML Weather API)</title>
		<link>http://icodeblog.com/2010/09/29/adding-local-weather-conditions-to-your-app-part-22-accessing-googles-xml-weather-api/</link>
		<comments>http://icodeblog.com/2010/09/29/adding-local-weather-conditions-to-your-app-part-22-accessing-googles-xml-weather-api/#comments</comments>
		<pubDate>Wed, 29 Sep 2010 19:36:36 +0000</pubDate>
		<dc:creator><![CDATA[Matt Tuzzolo]]></dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[CoreLocation]]></category>
		<category><![CDATA[Intermediate]]></category>
		<category><![CDATA[MKReverseGeocoder]]></category>
		<category><![CDATA[TouchXML]]></category>
		<category><![CDATA[Weather]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://icodeblog.com/?p=2405</guid>
		<description><![CDATA[<a href="/wp-content/uploads/2010/09/Screen-shot-2010-09-29-at-12.41.17-PM1.png"></a>
In this Part 2 of &#8216;Adding Local Weather Conditions To Your App&#8217;, I&#8217;ll show you how to quickly add current temp, conditions, and today&#8217;s high / low temperature to your app.
If you&#8217;re lucky enough to already have the user&#8217;s zipcode or city and state, this should go very quickly for you.  Otherwise, check out <a href="/2010/09/03/adding-local-weather-conditions-to-your-app-part-12-implementing-corelocation/">Part 1 (Integrating CoreLocation)</a>.
Let&#8217;s get started.
There are a handful of solid XML Weather APIs out there.  The best one I&#8217;ve seen so  ...]]></description>
				<content:encoded><![CDATA[<p><a href="/wp-content/uploads/2010/09/Screen-shot-2010-09-29-at-12.41.17-PM1.png"><img src="/wp-content/uploads/2010/09/Screen-shot-2010-09-29-at-12.41.17-PM1.png" alt="" title="Screen shot 2010-09-29 at 12.41.17 PM" width="378" height="210" class="aligncenter size-full wp-image-2433" /></a><br />
In this Part 2 of &#8216;Adding Local Weather Conditions To Your App&#8217;, I&#8217;ll show you how to quickly add current temp, conditions, and today&#8217;s high / low temperature to your app.</p>
<p>If you&#8217;re lucky enough to already have the user&#8217;s zipcode or city and state, this should go very quickly for you.  Otherwise, check out <a href="/2010/09/03/adding-local-weather-conditions-to-your-app-part-12-implementing-corelocation/">Part 1 (Integrating CoreLocation)</a>.</p>
<p>Let&#8217;s get started.</p>
<p>There are a handful of solid XML Weather APIs out there.  The best one I&#8217;ve seen so far is Wunderground&#8217;s (it&#8217;s extremely well documented) but for the purposes of this tutorial, I decided to use Google&#8217;s &#8220;super secret&#8221; Weather API.  It&#8217;s incredibly simple and should take care of all your basic weather needs.  Though if you&#8217;re planning on releasing a production App, be sure to pick a public API and check out their TOS (some require API keys, or fees for production use).  Here&#8217;s a good list of <a href="http://www.programmableweb.com/apitag/weather">Weather APIs</a></p>
<p>Let&#8217;s look at some example calls to Google:</p>
<p>http://www.google.com/ig/api?weather=01451</p>
<p>http://www.google.com/ig/api?weather=nyc</p>
<p>http://www.google.com/ig/api?weather=Portland,OR</p>
<p>http://www.google.com/ig/api?weather=Jamaica</p>
<p>As you can see, there&#8217;s a bit of flexibility in how you can query the service.  The one piece it&#8217;s lacking is querying by latitude and longitude.  Lucky for you, I&#8217;ll show you how to use MKReverseGeocoder to determine your user&#8217;s City/State, which you can then plug right into the GET request.  First, let&#8217;s take a quick look at the XML that comes back from the API:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;">&lt;?xml version<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;1.0&quot;</span>?&gt;
  &lt;xml_api_reply version<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;1&quot;</span>&gt;
    &lt;weather module_id<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;0&quot;</span> tab_id<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;0&quot;</span> mobile_row<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;0&quot;</span> mobile_zipped<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;1&quot;</span> row<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;0&quot;</span> section<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;0&quot;</span>&gt;
      &lt;forecast_information&gt;
        &lt;city data<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;Portland, OR&quot;</span><span style="color: #002200;">/</span>&gt;
        &lt;postal_code data<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;97217&quot;</span><span style="color: #002200;">/</span>&gt;
        &lt;latitude_e6 data<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;&quot;</span><span style="color: #002200;">/</span>&gt;
        &lt;longitude_e6 data<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;&quot;</span><span style="color: #002200;">/</span>&gt;
        &lt;forecast_date data<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;2010-09-20&quot;</span><span style="color: #002200;">/</span>&gt;
        &lt;current_date_time data<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;2010-09-20 23:46:50 +0000&quot;</span><span style="color: #002200;">/</span>&gt;
        &lt;unit_system data<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;US&quot;</span><span style="color: #002200;">/</span>&gt;
      &lt;<span style="color: #002200;">/</span>forecast_information&gt;
      &lt;current_conditions&gt;
        &lt;condition data<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;Cloudy&quot;</span><span style="color: #002200;">/</span>&gt;
        &lt;temp_f data<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;64&quot;</span><span style="color: #002200;">/</span>&gt;
        &lt;temp_c data<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;18&quot;</span><span style="color: #002200;">/</span>&gt;
        &lt;humidity data<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;Humidity: 55%&quot;</span><span style="color: #002200;">/</span>&gt;
        &lt;icon data<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;/ig/images/weather/cloudy.gif&quot;</span><span style="color: #002200;">/</span>&gt;
        &lt;wind_condition data<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;Wind: SW at 8 mph&quot;</span><span style="color: #002200;">/</span>&gt;
      &lt;<span style="color: #002200;">/</span>current_conditions&gt;
      &lt;forecast_conditions&gt;
        &lt;day_of_week data<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;Mon&quot;</span><span style="color: #002200;">/</span>&gt;
        &lt;low data<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;51&quot;</span><span style="color: #002200;">/</span>&gt;
        &lt;high data<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;66&quot;</span><span style="color: #002200;">/</span>&gt;
        &lt;icon data<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;/ig/images/weather/partly_cloudy.gif&quot;</span><span style="color: #002200;">/</span>&gt;
        &lt;condition data<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;Partly Cloudy&quot;</span><span style="color: #002200;">/</span>&gt;
      &lt;<span style="color: #002200;">/</span>forecast_conditions&gt;
      &lt;forecast_conditions&gt;
        &lt;day_of_week data<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;Tue&quot;</span><span style="color: #002200;">/</span>&gt;
        &lt;low data<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;50&quot;</span><span style="color: #002200;">/</span>&gt;
        &lt;high data<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;68&quot;</span><span style="color: #002200;">/</span>&gt;
        &lt;icon data<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;/ig/images/weather/partly_cloudy.gif&quot;</span><span style="color: #002200;">/</span>&gt;
        &lt;condition data<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;Partly Cloudy&quot;</span><span style="color: #002200;">/</span>&gt;
      &lt;<span style="color: #002200;">/</span>forecast_conditions&gt;
      &lt;forecast_conditions&gt;
        &lt;day_of_week data<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;Wed&quot;</span><span style="color: #002200;">/</span>&gt;
        &lt;low data<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;53&quot;</span><span style="color: #002200;">/</span>&gt;
        &lt;high data<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;68&quot;</span><span style="color: #002200;">/</span>&gt;
        &lt;icon data<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;/ig/images/weather/sunny.gif&quot;</span><span style="color: #002200;">/</span>&gt;
        &lt;condition data<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;Sunny&quot;</span><span style="color: #002200;">/</span>&gt;
     &lt;<span style="color: #002200;">/</span>forecast_conditions&gt;
    &lt;forecast_conditions&gt;
      &lt;day_of_week data<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;Thu&quot;</span><span style="color: #002200;">/</span>&gt;
      &lt;low data<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;53&quot;</span><span style="color: #002200;">/</span>&gt;
      &lt;high data<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;65&quot;</span><span style="color: #002200;">/</span>&gt;
      &lt;icon data<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;/ig/images/weather/rain.gif&quot;</span><span style="color: #002200;">/</span>&gt;
      &lt;condition data<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;Showers&quot;</span><span style="color: #002200;">/</span>&gt;
    &lt;<span style="color: #002200;">/</span>forecast_conditions&gt;
  &lt;<span style="color: #002200;">/</span>weather&gt;
&lt;<span style="color: #002200;">/</span>xml_api_reply&gt;</pre></td></tr></table></div>

<p>All this should be pretty self explanatory.  The two pieces to pay attention to here are current_conditions, and forecast_conditions.  For our demo app, we&#8217;re simply going to display current temperature, conditions, a conditionsIcon, and today&#8217;s high and low temp.  We&#8217;ll be able to pull all this information out of current_conditions and the first forecast_conditions (which is the forecast for today).  In the interest of keeping everything organized, let&#8217;s build a class to hold our weather info.</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;">//  ICB_WeatherConditions.h</span>
<span style="color: #11740a; font-style: italic;">//  LocalWeather</span>
<span style="color: #11740a; font-style: italic;">//</span>
<span style="color: #11740a; font-style: italic;">//  Created by Matt Tuzzolo on 9/28/10.</span>
<span style="color: #11740a; font-style: italic;">//  Copyright 2010 iCodeBlog. All rights reserved.</span>
<span style="color: #11740a; font-style: italic;">//</span>
&nbsp;
<span style="color: #a61390;">@interface</span> ICB_WeatherConditions <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span> <span style="color: #002200;">&#123;</span>
    <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>condition, <span style="color: #002200;">*</span>location;
    <span style="color: #400080;">NSURL</span> <span style="color: #002200;">*</span>conditionImageURL;
    NSInteger currentTemp,lowTemp,highTemp;
<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> <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>condition, <span style="color: #002200;">*</span>location;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic,retain<span style="color: #002200;">&#41;</span> <span style="color: #400080;">NSURL</span> <span style="color: #002200;">*</span>conditionImageURL;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic<span style="color: #002200;">&#41;</span> NSInteger currentTemp, lowTemp, highTemp;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>ICB_WeatherConditions <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>initWithQuery<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>query;
&nbsp;
<span style="color: #a61390;">@end</span></pre></td></tr></table></div>

<p>In the .m we&#8217;re going to pull the data out of the XML and store it in our properties.  There are several 3rd party Objective-C XML parsers.  I&#8217;ve chosen to use Jonathan Wight&#8217;s TouchXML as it&#8217;s become somewhat of a standard for parsing XML on iOS.  You can find it <a href="http://github.com/schwa/TouchXML">here</a>.  You&#8217;ll have to jump through a couple hoops to get TouchXML into your project.  Here&#8217;s an excellent <a href="http://foobarpig.com/iphone/touchxml-installation-guide.html">tutorial on installing TouchXML</a> that will walk you through the whole process if you&#8217;ve never done it before.</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;">//  ICB_WeatherConditions.m</span>
<span style="color: #11740a; font-style: italic;">//  LocalWeather</span>
<span style="color: #11740a; font-style: italic;">//</span>
<span style="color: #11740a; font-style: italic;">//  Created by Matt Tuzzolo on 9/28/10.</span>
<span style="color: #11740a; font-style: italic;">//  Copyright 2010 iCodeBlog. All rights reserved.</span>
<span style="color: #11740a; font-style: italic;">//</span>
&nbsp;
<span style="color: #6e371a;">#import &quot;ICB_WeatherConditions.h&quot;</span>
<span style="color: #6e371a;">#import &quot;TouchXML.h&quot;</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> ICB_WeatherConditions
&nbsp;
<span style="color: #a61390;">@synthesize</span> currentTemp, condition, conditionImageURL, location, lowTemp, highTemp;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>ICB_WeatherConditions <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>initWithQuery<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>query
<span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>self <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>super init<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#123;</span>
        CXMLDocument <span style="color: #002200;">*</span>parser <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>CXMLDocument alloc<span style="color: #002200;">&#93;</span> initWithContentsOfURL<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSURL</span> URLWithString<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;http://www.google.com/ig/api?weather=%@&quot;</span>, query<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span> options<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span> error<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
&nbsp;
        condition         <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>parser nodesForXPath<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;/xml_api_reply/weather/current_conditions/condition&quot;</span> error<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span> objectAtIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span> attributeForName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;data&quot;</span><span style="color: #002200;">&#93;</span> stringValue<span style="color: #002200;">&#93;</span> retain<span style="color: #002200;">&#93;</span>;
        location          <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>parser nodesForXPath<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;/xml_api_reply/weather/forecast_information/city&quot;</span> error<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span> objectAtIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span> attributeForName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;data&quot;</span><span style="color: #002200;">&#93;</span> stringValue<span style="color: #002200;">&#93;</span> retain<span style="color: #002200;">&#93;</span>;
&nbsp;
        currentTemp       <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>parser nodesForXPath<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;/xml_api_reply/weather/current_conditions/temp_f&quot;</span> error<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span> objectAtIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span> attributeForName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;data&quot;</span><span style="color: #002200;">&#93;</span> stringValue<span style="color: #002200;">&#93;</span> integerValue<span style="color: #002200;">&#93;</span>;
        lowTemp           <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>parser nodesForXPath<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;/xml_api_reply/weather/forecast_conditions/low&quot;</span> error<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span> objectAtIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span> attributeForName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;data&quot;</span><span style="color: #002200;">&#93;</span> stringValue<span style="color: #002200;">&#93;</span> integerValue<span style="color: #002200;">&#93;</span>;
        highTemp          <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>parser nodesForXPath<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;/xml_api_reply/weather/forecast_conditions/high&quot;</span> error<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span> objectAtIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span> attributeForName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;data&quot;</span><span style="color: #002200;">&#93;</span> stringValue<span style="color: #002200;">&#93;</span> integerValue<span style="color: #002200;">&#93;</span>;
&nbsp;
        conditionImageURL <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSURL</span> URLWithString<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;http://www.google.com%@&quot;</span>, <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>parser nodesForXPath<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;/xml_api_reply/weather/current_conditions/icon&quot;</span> error<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span> objectAtIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span> attributeForName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;data&quot;</span><span style="color: #002200;">&#93;</span> stringValue<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span> retain<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
&nbsp;
    <span style="color: #a61390;">return</span> self;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>dealloc <span style="color: #002200;">&#123;</span>
    <span style="color: #002200;">&#91;</span>conditionImageURL release<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>condition release<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>location release<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>super dealloc<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></td></tr></table></div>

<p>I&#8217;ve decided to write my own init method to handle making the request to our API.  This will make for a clean implementation in our view controller.</p>
<p>Before we get to implementing ICB_WeatherConditions, I&#8217;ll touch briefly on location.  Part 1/2 of this tutorial covered finding your user&#8217;s latitude and longitude with Core Location.  Use MKReverseGeocoder to find city/state from coordinates.  Start by adding both the MapKit and CoreLocation frameworks to your project.</p>
<p><a href="/wp-content/uploads/2010/09/Screen-shot-2010-09-29-at-11.42.59-AM1.png"><img src="/wp-content/uploads/2010/09/Screen-shot-2010-09-29-at-11.42.59-AM1.png" alt="" title="Screen shot 2010-09-29 at 11.42.59 AM" width="458" height="336" class="alignnone size-full wp-image-2416" /></a></p>
<p><a href="/wp-content/uploads/2010/09/Screen-shot-2010-09-29-at-11.43.12-AM1.png"><img src="/wp-content/uploads/2010/09/Screen-shot-2010-09-29-at-11.43.12-AM1.png" alt="" title="Screen shot 2010-09-29 at 11.43.12 AM" width="358" height="548" class="alignnone size-full wp-image-2415" /></a></p>
<p>MKReverseGeocoder works asynchronously to resolve location info; it has a delegate.  In our example we set the delegate to the view controller (self).  Be sure to add <mkreverseGeocoderDelegate> to your header as well.  Since your view controller is now the delegate for the geocoder, make sure to implement the delegate methods for the MKReverseGeocoderDelegate protocol:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#pragma mark MKReverseGeocoder Delegate Methods</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>reverseGeocoder<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>MKReverseGeocoder <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>geocoder didFindPlacemark<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>MKPlacemark <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>placemark
<span style="color: #002200;">&#123;</span>
    <span style="color: #002200;">&#91;</span>geocoder release<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>self performSelectorInBackground<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>showWeatherFor<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span> withObject<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>placemark.addressDictionary objectForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;ZIP&quot;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>reverseGeocoder<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>MKReverseGeocoder <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>geocoder 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>
    NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;reverseGeocoder:%@ didFailWithError:%@&quot;</span>, geocoder, error<span style="color: #002200;">&#41;</span>;
    <span style="color: #002200;">&#91;</span>geocoder release<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>Now we&#8217;re ready to implement ICB_WeatherConditions.  I usually populate UILabels in viewDidLoad, but since we&#8217;re making API calls and downloading a remote image (the weather conditions icon), I decided to write a method to execute in the background.  This lets us use synchronous requests (which a lot easier to deal with) to handle network requests without locking up the main thread.  Once our network calls have finished, we call back to the main thread to update the UI accordingly.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// This will run in the background</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>showWeatherFor<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>query
<span style="color: #002200;">&#123;</span>
    <span style="color: #400080;">NSAutoreleasePool</span> <span style="color: #002200;">*</span>pool <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSAutoreleasePool</span> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
&nbsp;
    ICB_WeatherConditions <span style="color: #002200;">*</span>weather <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>ICB_WeatherConditions alloc<span style="color: #002200;">&#93;</span> initWithQuery<span style="color: #002200;">:</span>query<span style="color: #002200;">&#93;</span>;
&nbsp;
    self.conditionsImage <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIImage imageWithData<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSData</span> dataWithContentsOfURL<span style="color: #002200;">:</span>weather.conditionImageURL<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span> retain<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #002200;">&#91;</span>self performSelectorOnMainThread<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>updateUI<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span> withObject<span style="color: #002200;">:</span>weather waitUntilDone<span style="color: #002200;">:</span><span style="color: #a61390;">NO</span><span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #002200;">&#91;</span>pool release<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// This happens in the main thread</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>updateUI<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>ICB_WeatherConditions <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>weather
<span style="color: #002200;">&#123;</span>
    self.conditionsImageView.image <span style="color: #002200;">=</span> self.conditionsImage;
    <span style="color: #002200;">&#91;</span>self.conditionsImage release<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #002200;">&#91;</span>self.currentTempLabel setText<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;%d&quot;</span>, weather.currentTemp<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>self.highTempLabel setText<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;%d&quot;</span>, weather.highTemp<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>self.lowTempLabel setText<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;%d&quot;</span>, weather.lowTemp<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>self.conditionsLabel setText<span style="color: #002200;">:</span>weather.condition<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>self.cityLabel setText<span style="color: #002200;">:</span>weather.location<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #002200;">&#91;</span>weather release<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>Of course make sure your NIB is connected to your IBOutlets properly.</p>
<p>Now the final piece:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">&#91;</span>self performSelectorInBackground<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>showWeatherFor<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span> withObject<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;97217&quot;</span><span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

<p>Build and Run:</p>
<p><a href="/wp-content/uploads/2010/09/Screen-shot-2010-09-28-at-5.08.44-PM1.png"><img src="/wp-content/uploads/2010/09/Screen-shot-2010-09-28-at-5.08.44-PM1.png" alt="" title="Final" width="416" height="735" class="alignnone size-full wp-image-2417" /></a></p>
<p>And you&#8217;re done!</p>
<p>You can see the complete class below.  I&#8217;ve also posted a <a href="http://github.com/elc/ICB_LocalWeather">demo project</a> to github.</p>
<p>My name is Matt Tuzzolo (<a href="http://twitter.com/matt_tuzzolo">@matt_tuzzolo</a>).  I hope you found this post helpful.</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;">//  LocalWeatherViewController.h</span>
<span style="color: #11740a; font-style: italic;">//  LocalWeather</span>
<span style="color: #11740a; font-style: italic;">//</span>
<span style="color: #11740a; font-style: italic;">//  Created by Matt Tuzzolo on 8/30/10.</span>
<span style="color: #11740a; font-style: italic;">//  Copyright iCodeBlog LLC 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;MapKit/MapKit.h&quot;</span>
&nbsp;
<span style="color: #a61390;">@interface</span> LocalWeatherViewController <span style="color: #002200;">:</span> UIViewController &lt;mkreverseGeocoderDelegate&gt; <span style="color: #002200;">&#123;</span>
    IBOutlet UILabel <span style="color: #002200;">*</span>currentTempLabel, <span style="color: #002200;">*</span>highTempLabel, <span style="color: #002200;">*</span>lowTempLabel, <span style="color: #002200;">*</span>conditionsLabel, <span style="color: #002200;">*</span>cityLabel;
    IBOutlet UIImageView <span style="color: #002200;">*</span>conditionsImageView;
    UIImage <span style="color: #002200;">*</span>conditionsImage;
<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 UILabel <span style="color: #002200;">*</span>currentTempLabel, <span style="color: #002200;">*</span>highTempLabel, <span style="color: #002200;">*</span>lowTempLabel, <span style="color: #002200;">*</span>conditionsLabel, <span style="color: #002200;">*</span>cityLabel;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic,retain<span style="color: #002200;">&#41;</span> IBOutlet UIImageView <span style="color: #002200;">*</span>conditionsImageView;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic,retain<span style="color: #002200;">&#41;</span> UIImage <span style="color: #002200;">*</span>conditionsImage;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>updateUI<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>ICB_WeatherConditions <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>weather;
&nbsp;
<span style="color: #a61390;">@end</span></pre></td></tr></table></div>

<p>And the .m:</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;">//  LocalWeatherViewController.m</span>
<span style="color: #11740a; font-style: italic;">//  LocalWeather</span>
<span style="color: #11740a; font-style: italic;">//</span>
<span style="color: #11740a; font-style: italic;">//  Created by Matt Tuzzolo on 8/30/10.</span>
<span style="color: #11740a; font-style: italic;">//  Copyright iCodeBlog LLC 2010. All rights reserved.</span>
<span style="color: #11740a; font-style: italic;">//</span>
&nbsp;
<span style="color: #6e371a;">#import &quot;LocalWeatherViewController.h&quot;</span>
<span style="color: #6e371a;">#import &quot;ICB_WeatherConditions.h&quot;</span>
<span style="color: #6e371a;">#import &quot;MapKit/MapKit.h&quot;</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> LocalWeatherViewController
&nbsp;
<span style="color: #a61390;">@synthesize</span> currentTempLabel, highTempLabel, lowTempLabel, conditionsLabel, cityLabel;
<span style="color: #a61390;">@synthesize</span> conditionsImageView;
<span style="color: #a61390;">@synthesize</span> conditionsImage;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>viewDidLoad <span style="color: #002200;">&#123;</span>
    <span style="color: #002200;">&#91;</span>super viewDidLoad<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #2400d9;">1</span><span style="color: #002200;">&#41;</span> <span style="color: #11740a; font-style: italic;">//you have coordinates but need a city</span>
    <span style="color: #002200;">&#123;</span>
        <span style="color: #11740a; font-style: italic;">// Check out Part 1 of the tutorial to see how to find your Location with CoreLocation</span>
        CLLocationCoordinate2D coord;
        coord.latitude <span style="color: #002200;">=</span> <span style="color: #2400d9;">45.574779</span>;
        coord.longitude <span style="color: #002200;">=</span> <span style="color: #002200;">-</span><span style="color: #2400d9;">122.685366</span>;
&nbsp;
        <span style="color: #11740a; font-style: italic;">// Geocode coordinate (normally we'd use location.coordinate here instead of coord).</span>
        <span style="color: #11740a; font-style: italic;">// This will get us something we can query Google's Weather API with</span>
        MKReverseGeocoder <span style="color: #002200;">*</span>geocoder <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>MKReverseGeocoder alloc<span style="color: #002200;">&#93;</span> initWithCoordinate<span style="color: #002200;">:</span>coord<span style="color: #002200;">&#93;</span>;
        geocoder.delegate <span style="color: #002200;">=</span> self;
        <span style="color: #002200;">&#91;</span>geocoder start<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
    <span style="color: #a61390;">else</span> <span style="color: #11740a; font-style: italic;">// You already know your users zipcode, city, or otherwise.</span>
    <span style="color: #002200;">&#123;</span>
        <span style="color: #11740a; font-style: italic;">// Do this in the background so we don't lock up the UI.</span>
        <span style="color: #002200;">&#91;</span>self performSelectorInBackground<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>showWeatherFor<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span> withObject<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;97217&quot;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>showWeatherFor<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>query
<span style="color: #002200;">&#123;</span>
    <span style="color: #400080;">NSAutoreleasePool</span> <span style="color: #002200;">*</span>pool <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSAutoreleasePool</span> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
&nbsp;
    ICB_WeatherConditions <span style="color: #002200;">*</span>weather <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>ICB_WeatherConditions alloc<span style="color: #002200;">&#93;</span> initWithQuery<span style="color: #002200;">:</span>query<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #002200;">&#91;</span>self.currentTempLabel setText<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;%d&quot;</span>, weather.currentTemp<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>self.highTempLabel setText<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;%d&quot;</span>, weather.highTemp<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>self.lowTempLabel setText<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;%d&quot;</span>, weather.lowTemp<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>self.conditionsLabel setText<span style="color: #002200;">:</span>weather.condition<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>self.cityLabel setText<span style="color: #002200;">:</span>weather.location<span style="color: #002200;">&#93;</span>;
&nbsp;
    self.conditionsImageView.image <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIImage imageWithData<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSData</span> dataWithContentsOfURL<span style="color: #002200;">:</span>weather.conditionImageURL<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #002200;">&#91;</span>weather release<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #002200;">&#91;</span>pool release<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #6e371a;">#pragma mark MKReverseGeocoder Delegate Methods</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>reverseGeocoder<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>MKReverseGeocoder <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>geocoder didFindPlacemark<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>MKPlacemark <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>placemark
<span style="color: #002200;">&#123;</span>
    <span style="color: #002200;">&#91;</span>geocoder release<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>self performSelectorInBackground<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>showWeatherFor<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span> withObject<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>placemark.addressDictionary objectForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;ZIP&quot;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>reverseGeocoder<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>MKReverseGeocoder <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>geocoder 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>
    NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;reverseGeocoder:%@ didFailWithError:%@&quot;</span>, geocoder, error<span style="color: #002200;">&#41;</span>;
    <span style="color: #002200;">&#91;</span>geocoder release<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>didReceiveMemoryWarning <span style="color: #002200;">&#123;</span>
     <span style="color: #002200;">&#91;</span>super didReceiveMemoryWarning<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>viewDidUnload <span style="color: #002200;">&#123;</span>
	<span style="color: #11740a; font-style: italic;">// Release any retained subviews of the main view.</span>
	<span style="color: #11740a; font-style: italic;">// e.g. self.myOutlet = nil;</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>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>

]]></content:encoded>
			<wfw:commentRss>http://icodeblog.com/2010/09/29/adding-local-weather-conditions-to-your-app-part-22-accessing-googles-xml-weather-api/feed/</wfw:commentRss>
		<slash:comments>40</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>
	</channel>
</rss>
