<?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; iphone development</title>
	<atom:link href="/tag/iphone-development/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>How to Add GPS to Your iOS App &#8211; Part 1</title>
		<link>http://icodeblog.com/2012/06/04/how-to-add-gps-to-your-ios-app-part-1/</link>
		<comments>http://icodeblog.com/2012/06/04/how-to-add-gps-to-your-ios-app-part-1/#comments</comments>
		<pubDate>Mon, 04 Jun 2012 21:33:11 +0000</pubDate>
		<dc:creator><![CDATA[Shawn Grimes]]></dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[beginner]]></category>
		<category><![CDATA[CoreLocation]]></category>
		<category><![CDATA[GPS]]></category>
		<category><![CDATA[iOS]]></category>
		<category><![CDATA[iPhone Coding]]></category>
		<category><![CDATA[iphone dev]]></category>
		<category><![CDATA[iphone development]]></category>
		<category><![CDATA[iphone programming]]></category>

		<guid isPermaLink="false">http://icodeblog.com/?p=3690</guid>
		<description><![CDATA[
In Part 1 of this series, I will introduce you to the very basics of CoreLocation services and getting the location of the device currently being used.
About CoreLocation
The CoreLocation framework provides your app with the ability to get a device&#8217;s current location, provided the user of the device has granted your app access to that information.
Location services are provided in two major ways using this framework:

Standard Location Services- This service provides the most accurate location information using a variety of  ...]]></description>
				<content:encoded><![CDATA[<p><center><img src="http://img.skitch.com/20120604-drip3tee432eccbfhsdkrpwhbr.jpg" alt="CoreLocation Graphic" /></center></p>
<blockquote><p>In Part 1 of this series, I will introduce you to the very basics of CoreLocation services and getting the location of the device currently being used.</p></blockquote>
<h2>About CoreLocation</h2>
<p>The CoreLocation framework provides your app with the ability to get a device&#8217;s current location, provided the user of the device has granted your app access to that information.</p>
<p>Location services are provided in two major ways using this framework:</p>
<ol>
<li><strong>Standard Location Services</strong>- This service provides the most accurate location information using a variety of methods including celluar, wifi, and GPS.
<ul>
<li>Most accurate.</li>
<li>Consumes the most power and can take a longer time to acquire.</li>
<li>Can only be used when your application is in the foreground.</li>
</ul>
</li>
<li><strong>Significant Change Location Services</strong>- This service uses cellular data to determine a general idea of where the device is located.
<ul>
<li>Fastest and uses the least amount of power to determine the location.</li>
<li>Can also be used to notify your app when a significant change in the location has occurred even if your app is not in focus.</li>
</ul>
</li>
</ol>
<h2>Project Files:</h2>
<p>You can download the project files at this github repository: <a href="http://github.com/shawngrimes/Location-and-Map-Sample">http://github.com/shawngrimes/Location-and-Map-Sample</a></p>
<h2>Adding Location Frameworks</h2>
<p>The first step to adding location services to your project is to add the CoreLocation framework.</p>
<div class="thumbnail" align="center"><a href="http://skitch.com/shawnegrimes/86jyp/link-binary-with-libraries"><img src="http://img.skitch.com/20120603-d7w4x5fyueprskt33g5xet7a1n.preview.jpg" alt="Link Binary With Libraries" /></a></div>
<ol>
<li>Select your target and go to the &#8220;Build Phases&#8221; tab.</li>
<li>Click the &#8220;+&#8221; button under &#8220;Link Binary With Libraries&#8221;</li>
<li>Select the CoreLocation framework and click &#8220;Add&#8221;</li>
</ol>
<div class="thumbnail" align="center"><a href="http://skitch.com/shawnegrimes/86je5/add-core-location-framework"><img src="http://img.skitch.com/20120603-gby2hyh3mtsfax86u5ht39491r.preview.jpg" alt="Add Core Location Framework" /></a></div>
<h2>Location Manager</h2>
<p>With location services, you create an instance of CLLocationManager. There is usually one instance of this per app so I feel a good place for this is in the App Delegate and then each view can access the current location from the App Delegate.</p>
<p>To start, open your App Delegate header file:</p>
<ol>
<li>Add <code>#import &lt;CoreLocation/CoreLocation.h&gt;</code> to the top</li>
<li>Specify that your app delegate complies with the Core Location Delegate protocol by changing your <code>@interface</code> line with <code>@interface CFAAppDelegate : UIResponder &lt;UIApplicationDelegate, CLLocationManagerDelegate&gt;</code></li>
<li>Specify a CLLocationManager property by adding <code>@property (strong, nonatomic) CLLocationManager *locationManager</code> right before the <code>@end</code> statement.</li>
</ol>
<p>Your App Delegate header file should now look similar to the following:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &amp;lt;UIKit/UIKit.h&amp;gt;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">//Add Location Framework</span>
<span style="color: #6e371a;">#import &amp;lt;CoreLocation/CoreLocation.h&amp;gt;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">//Specify this app delegate complies with the location manager delegate</span>
<span style="color: #a61390;">@interface</span> CFAAppDelegate <span style="color: #002200;">:</span> UIResponder <span style="color: #002200;">&amp;</span>lt;UIApplicationDelegate, CLLocationManagerDelegate<span style="color: #002200;">&amp;</span>gt;
&nbsp;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>strong, nonatomic<span style="color: #002200;">&#41;</span> UIWindow <span style="color: #002200;">*</span>window;
&nbsp;
<span style="color: #11740a; font-style: italic;">//Add a location manager property to this app delegate</span>
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>strong, nonatomic<span style="color: #002200;">&#41;</span> CLLocationManager <span style="color: #002200;">*</span>locationManager;
&nbsp;
<span style="color: #a61390;">@end</span></pre></td></tr></table></div>

<h2>Creating The Location Manager Object</h2>
<p>Switch over to the App Delegate implementation file (.m), and we are going to create our location manager object. The first thing we should do, since we created it as a property of our app delegate, is synthesize the property so add <code>@synthesize locationManager=_locationManager;</code> under the line that reads <code>@synthesize window = _window;</code>.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &quot;CFAAppDelegate.h&quot;</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> CFAAppDelegate
&nbsp;
<span style="color: #a61390;">@synthesize</span> window <span style="color: #002200;">=</span> _window;
<span style="color: #a61390;">@synthesize</span> locationManager<span style="color: #002200;">=</span>_locationManager;</pre></td></tr></table></div>

<p>Now that our property is synthesized, we can create the object. I usually create it in the method <code>application didFinishLaunchingWithOptions:</code>. A Location Manager object is created similar to any other object but there are three important properties you should set after you have alloc&#8217;d/init&#8217;d.</p>
<ol>
<li><strong>.purpose</strong> &#8211; The purpose property is displayed to the user of your app when they are prompted to allow your app to use their location. It gives you a chance to explain what your app is going to do with their location information.</li>
<li><strong>.desiredAccuracy</strong> &#8211; The desired accuracy property allows you to tell the device how accurate you would like the location information to be. This should be set based on your application&#8217;s needs. Don&#8217;t set this property to <code>kCLLocationAccuracyBest</code> if you only need to know what city they are in. <em>NOTE: This is the &#8220;desired&#8221; accuracy, it is not guaranteed. The device will determine the best available information and provide it to you but you are not, however, guaranteed any level of accuracy.</em></li>
<li><strong>.distanceFilter</strong> &#8211; The distance filter property tells the location manager how far a device needs to move (horizontally vs. an altitude change) before triggering a new location event. It is measured in meters. You can set the property to <code>kCLDistanceFilterNone</code> to be notified of all events (this is also the default value).</li>
</ol>
<p>The completed creation of our location manage looks like this:</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>
    self.window <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIWindow alloc<span style="color: #002200;">&#93;</span> initWithFrame<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIScreen mainScreen<span style="color: #002200;">&#93;</span> bounds<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #11740a; font-style: italic;">// Override point for customization after application launch.</span>
    self.window.backgroundColor <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIColor whiteColor<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>self.window makeKeyAndVisible<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>self.locationManager<span style="color: #002200;">==</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#123;</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>;
        <span style="color: #11740a; font-style: italic;">//I'm using ARC with this project so no need to release</span>
&nbsp;
        _locationManager.delegate<span style="color: #002200;">=</span>self;
        _locationManager.purpose <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;We will try to tell you where you are if you get lost&quot;</span>;
        _locationManager.desiredAccuracy<span style="color: #002200;">=</span>kCLLocationAccuracyBest;
        _locationManager.distanceFilter<span style="color: #002200;">=</span><span style="color: #2400d9;">500</span>;
        self.locationManager<span style="color: #002200;">=</span>_locationManager;
    <span style="color: #002200;">&#125;</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>If you run this now, you should not notice any change in the way your application runs, you also won&#8217;t start receiving location information just yet. That comes next&#8230;</p>
<h2>Starting Location Services</h2>
<p>Now that we have our location manager configured, we want to start getting the location. To start the standard location service, you should first check to make sure that location services are enabled. This is done with a simple call to <code>[CLLocationManager locationServicesEnabled]</code> so after we set up our location manager above and before the <code>return YES;</code>, add the following code:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;">    <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>CLLocationManager locationServicesEnabled<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#123;</span>
        <span style="color: #002200;">&#91;</span>self.locationManager startUpdatingLocation<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<blockquote><p>NOTE: If location services are not enabled and you start updating location services, the user will be prompted to enable location services. This could be annoying to the end user if they have to answer &#8220;No&#8221; every time your app launches.</p></blockquote>
<p>If you run your app now, you will notice that the user is prompted to enable location services (and it will include the message from the <code>.prompt</code> property of our location manager object).</p>
<div class="thumbnail" align="center"><a href="http://skitch.com/shawnegrimes/86idb/enablelocationservicesprompt"><img src="http://img.skitch.com/20120605-ke3m4ipx1cj2p9pprstkcdcrdn.preview.jpg" alt="EnableLocationServicesPrompt" /></a></div>
<h2>Receiving Location Changes</h2>
<p>We&#8217;ve started location services, but now we need to start receiving updates to location changes. This is all done through the <code>CLLocationManagerDelegate</code> protocol. There are a number of available methods to implement for this delegate but the three most important are probably:</p>
<ol>
<li>– locationManager:didUpdateToLocation:fromLocation:</li>
<li>– locationManager:didFailWithError:</li>
<li>– locationManager:didChangeAuthorizationStatus:</li>
</ol>
<h2>– locationManager:didUpdateToLocation:fromLocation:</h2>
<p>Let&#8217;s start with receiving an update in location and how to handle that. In our App Delegate implementation file (.m), add the following method placeholder below the <code>@synthesize …</code> statements and above <code>- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions</code> method:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>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 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>
&nbsp;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>One of the first things I like to do is check the timestamp of the newLocation detected. When you first start updating location services, the location manager will receive the last known location of the device, this could be hours or days old depending on the last time location services were used. In my apps, I like to make sure that I have a recent location. This following code will only use a location that has been found in the last 15.0 seconds:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;">    <span style="color: #400080;">NSDate</span><span style="color: #002200;">*</span> eventDate <span style="color: #002200;">=</span> newLocation.timestamp;
    NSTimeInterval howRecent <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>eventDate timeIntervalSinceNow<span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">abs</span><span style="color: #002200;">&#40;</span>howRecent<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&amp;</span>lt; <span style="color: #2400d9;">15.0</span><span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#123;</span>
        <span style="color: #11740a; font-style: italic;">//Location timestamp is within the last 15.0 seconds, let's use it!</span>
    <span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>The next check that I like to perform is to see how accurate the newLocation is. This can be done by looking at the <code>.horizontalAccuracy</code> property. This property tells you how accurate your location information is. As I stated above, you can request to have very accurate locations but you are not guaranteed it. In the illustration below, the newLocation.coordinate is the location the device thinks it is, but in truth the orange circle of the .horizontalAccuracy says that it could be anywhere in that circle.</p>
<div class="thumbnail" align="center"><a href="https://skitch.com/shawnegrimes/86kp3/horizontalaccuracy"><img src="https://img.skitch.com/20120603-enfpceaw8jgsqw1hieenx48jmj.preview.jpg" alt="horizontalAccuracy" /></a></div>
<p>Your application&#8217;s need will determine how accurate of a location you need. Again, if you just need the city the user is in, you won&#8217;t need it to be as accurate as if you were trying to find something within walking distance. In the code below, I have set my accuracy threshold to 35.0 meters. This is pretty broad but I have found that it works well indoors as well as out. Insert the following code right after the comment <code>//Location timestamp is within the last 15.0 seconds, let's use it!</code></p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;">        <span style="color: #11740a; font-style: italic;">//Location timestamp is within the last 15.0 seconds, let's use it!</span>
        <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>newLocation.horizontalAccuracy<span style="color: #002200;">&amp;</span>lt;<span style="color: #2400d9;">35.0</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#123;</span>
            <span style="color: #11740a; font-style: italic;">//Location seems pretty accurate, let's use it!</span>
            NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;latitude %+.6f, longitude %+.6f<span style="color: #2400d9;">\n</span>&quot;</span>,
                  newLocation.coordinate.latitude,
                  newLocation.coordinate.longitude<span style="color: #002200;">&#41;</span>;
            NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Horizontal Accuracy:%f&quot;</span>, newLocation.horizontalAccuracy<span style="color: #002200;">&#41;</span>;
&nbsp;
            <span style="color: #11740a; font-style: italic;">//Optional: turn off location services once we've gotten a good location</span>
            <span style="color: #002200;">&#91;</span>manager stopUpdatingLocation<span style="color: #002200;">&#93;</span>;
        <span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>The code above will print the new location to the console and then turn off location services. This is an optional step but if you don&#8217;t need location services anymore, it&#8217;s a smart thing to turn them off to conserve your user&#8217;s battery.</p>
<p>This is what our complete delegate method looks like:</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>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 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: #400080;">NSDate</span><span style="color: #002200;">*</span> eventDate <span style="color: #002200;">=</span> newLocation.timestamp;
    NSTimeInterval howRecent <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>eventDate timeIntervalSinceNow<span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">abs</span><span style="color: #002200;">&#40;</span>howRecent<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&amp;</span>lt; <span style="color: #2400d9;">15.0</span><span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#123;</span>
        <span style="color: #11740a; font-style: italic;">//Location timestamp is within the last 15.0 seconds, let's use it!</span>
        <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>newLocation.horizontalAccuracy<span style="color: #002200;">&amp;</span>lt;<span style="color: #2400d9;">35.0</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#123;</span>
            <span style="color: #11740a; font-style: italic;">//Location seems pretty accurate, let's use it!</span>
            NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;latitude %+.6f, longitude %+.6f<span style="color: #2400d9;">\n</span>&quot;</span>,
                  newLocation.coordinate.latitude,
                  newLocation.coordinate.longitude<span style="color: #002200;">&#41;</span>;
            NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Horizontal Accuracy:%f&quot;</span>, newLocation.horizontalAccuracy<span style="color: #002200;">&#41;</span>;
&nbsp;
            <span style="color: #11740a; font-style: italic;">//Optional: turn off location services once we've gotten a good location</span>
            <span style="color: #002200;">&#91;</span>manager stopUpdatingLocation<span style="color: #002200;">&#93;</span>;
        <span style="color: #002200;">&#125;</span>
    <span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>Now we can run the project again. If you didn&#8217;t authorize the app to use location services before, go ahead and do that now. We haven&#8217;t done anything with the location at this point except print it out to the console. If you don&#8217;t know how to see the console, make sure Xcode is the active window (NOT the simulator) and press ⌘⇧C</p>
<p>If you are running the project on the simulator and not a device, you may not see any results in the console window. This is because the simulator does not have location services turned on by default. You can simulate a location by selecting the location services simulation in the console window:</p>
<div class="thumbnail" align="center"><a href="http://skitch.com/shawnegrimes/86typ/simulate-location"><img src="http://img.skitch.com/20120604-b359j7crjba8wctd1gje5kk112.preview.jpg" alt="Simulate Location" /></a></div>
<p>You can also simulate a location in the Simulator by using the Debug menu.</p>
<div class="thumbnail" align="center"><a href="https://skitch.com/shawnegrimes/86tn4/simulator-location-services"><img src="https://img.skitch.com/20120604-qxs23ikjy6qufjpwpg72fuewk9.preview.jpg" alt="Simulator Location Services" /></a></div>
<p>Once a location has been selected, you may need to restart the app to get the new location to show in the console window:</p>
<div class="thumbnail" align="center"><a href="https://skitch.com/shawnegrimes/86trw/location-in-console"><img src="https://img.skitch.com/20120604-ddt5unstdmea3i58ddxsgfgih8.preview.jpg" alt="Location In Console" /></a></div>
<p>Because we built the location manager into the App Delegate, you can access the device&#8217;s current location anywhere in your code by including the AppDelegate header file (.h) in your view controller&#8217;s implementation file (.m):</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &quot;CFAAppDelegate.h&quot;</span></pre></td></tr></table></div>

<p>Then whenever you need to access the location, you can use the following bit of code:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">//Make sure location services are enabled before requesting the location</span>
<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>CLLocationManager locationServicesEnabled<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#123;</span>
&nbsp;
    CFAAppDelegate <span style="color: #002200;">*</span>appDelegate<span style="color: #002200;">=</span><span style="color: #002200;">&#40;</span>CFAAppDelegate <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#91;</span>UIApplication sharedApplication<span style="color: #002200;">&#93;</span>.delegate;
    CLLocation <span style="color: #002200;">*</span>currentLocation<span style="color: #002200;">=</span>appDelegate.locationManager.location;
&nbsp;
    <span style="color: #11740a; font-style: italic;">//Do what you want with the location...</span>
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<blockquote><p><strong>About: Shawn Grimes</strong><br />
Shawn is a mobile app developer for <a href="http://elctech.com/">ELC Technologies</a>. He is a co-author of the book, <a href="http://www.amazon.com/gp/product/1430240059/ref=as_li_ss_tl?ie=UTF8&amp;tag=charmtrave-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=1430240059">iOS 5 Recipes: A Problem-Solution Approach (Recipes Apress)</a>. You can follow him on twitter <a href="https://twitter.com/#!/shawng">@shawng</a>. Shawn also works with high school students to teach them app development skills through the <a href="http://www.appliedclub.org">APPlied Club</a> Program.</p></blockquote>
<p>UIKit/UIKit.hp</p>
]]></content:encoded>
			<wfw:commentRss>http://icodeblog.com/2012/06/04/how-to-add-gps-to-your-ios-app-part-1/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>iPhone Game Programming Tutorial Part 4 &#8211; Basic Game Audio</title>
		<link>http://icodeblog.com/2009/05/04/iphone-game-programming-tutorial-part-4-basic-game-audio/</link>
		<comments>http://icodeblog.com/2009/05/04/iphone-game-programming-tutorial-part-4-basic-game-audio/#comments</comments>
		<pubDate>Mon, 04 May 2009 23:47:30 +0000</pubDate>
		<dc:creator><![CDATA[brandontreb]]></dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Intermediate]]></category>
		<category><![CDATA[iphone development]]></category>
		<category><![CDATA[iphone game programming tutorial]]></category>

		<guid isPermaLink="false">http://icodeblog.com/?p=719</guid>
		<description><![CDATA[Ok, I will be finishing up this series by showing you guys how to add basic audio to your game.  Since this is a beginner tutorial, I won&#8217;t go into complex audio using OpenAL or Audio Queues.  Today, we will just be playing short sounds (less than 30 seconds).  This will be the easiest way to do audio if you don&#8217;t require any background music (I guess you could do a nasty hack and loop a 30  ...]]></description>
				<content:encoded><![CDATA[<p>Ok, I will be finishing up this series by showing you guys how to add basic audio to your game.  Since this is a beginner tutorial, I won&#8217;t go into complex audio using OpenAL or Audio Queues.  Today, we will just be playing short sounds (less than 30 seconds).  This will be the easiest way to do audio if you don&#8217;t require any background music (I guess you could do a nasty hack and loop a 30 sec clip but that would be shameful <img src="/wp-includes/images/smilies/icon_smile.gif" alt=":)" class="wp-smiley" /> ).  The downfall of doing audio this way is only one audio file can be playing at a time.  Ok, let&#8217;s get started&#8230; Make sure you are starting with the base code from the <a href="/2009/03/18/iphone-game-programming-tutorial-part-3-splash-screen/">previous tutorial</a> (either use yours or download a fresh copy).  We will be using 2 sounds today, 1 for when the ball is hit and another for when someone scores a point.</p>
<p>Here are the sound files you will need to download for this tutorial:</p>
<ul>
<li><a style="text-decoration: none;" href="/wp-content/uploads/2009/05/clapping-crowd-studio-01.caf">clapping-crowd-studio-01</a></li>
<li><a href="/wp-content/uploads/2009/05/tennis-volley-01.caf">tennis-volley-01</a></li>
</ul>
<p>Notice that all of the audio files have a .caf extension.  This is the audio type all of your sound files must be in to be played by the iPhone.  Luckily Apple has provided us a utility for converting audio to this format. You can read up on it <a href="http://developer.apple.com/iphone/prerelease/library/codinghowtos/AudioAndVideo/index.html#PROCESSING-CONVERT_AN_AUDIO_FILE_TO_THE_PREFERRED_FORMAT_FOR_IPHONE_OS">here</a>.  It&#8217;s pretty straight forward, just open up a Terminal and type</p>
<p><strong>/usr/bin/afconvert -f caff -d LEI16 {INPUT} {OUTPUT}</strong></p>
<p>Where {INPUT} is the path to your input audio file and {OUTPUT} is the file you want it to save it to with a .caf extension.</p>
<h2>Adding the Audio Files To You Project</h2>
<p>Start by downloading these files.  Then drag them to your Resources Folder inside of your project.  You may also want to add a subfolder called Audio to keep organized. Make sure you check the box to copy the files into your project&#8217;s directory.</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2009/05/screenshot_011.png"><img class="alignnone size-full wp-image-724" title="screenshot_01" src="/wp-content/uploads/2009/05/screenshot_011.png" alt="screenshot_01" width="232" height="240" /></a></p>
<h2>Add The AudioToolbox.framework to Your Project</h2>
<p>Since we need to call functions within this framework, we will need to add it to our project. The best way I have found to locate a framework on my mac is to search for it.<span style="white-space:pre"> </span> Right Click on the <strong>Frameworks</strong> folder in your project and select <strong>Add -&gt; Existing Frameworks</strong>.  In the search box type <strong>AudioToolbox.framework </strong> and select the framework among the search results. (Make sure you have <strong>Computer</strong> selected to search your entire computer)</p>
<p style="text-align: center; "><a href="/wp-content/uploads/2009/05/screenshot_021.png"><img class="alignnone size-full wp-image-725" title="screenshot_02" src="/wp-content/uploads/2009/05/screenshot_021.png" alt="screenshot_02" width="532" height="427" /></a></p>
<p style="text-align: left; ">When you find it, click <strong>Add.</strong> We are now ready to start coding&#8230;</p>
<h2>Add the Header Declarations</h2>
<p>Open up <strong>iTennisViewController.h</strong> and add the following code.</p>
<p style="text-align: center; "><a href="/wp-content/uploads/2009/05/screenshot_111.png"><img class="alignnone size-full wp-image-729" title="screenshot_11" src="/wp-content/uploads/2009/05/screenshot_111.png" alt="screenshot_11" width="325" height="356" /></a></p>
<p style="text-align: left; ">First we see an import for the AudioServices.h. This is needed to call the functions required for us to play audio.  Next, we declare 2 SystemSoundID&#8217;s.  These are basically just Integers (could actually use an int instead but you&#8217;d get a warning).  Next, open up <strong>iTennishViewController.m</strong> and add the following code to synthesize these variables.</p>
<p style="text-align: center; "><a href="/wp-content/uploads/2009/05/screenshot_091.png"><img class="alignnone size-full wp-image-728" title="screenshot_09" src="/wp-content/uploads/2009/05/screenshot_091.png" alt="screenshot_09" width="258" height="27" /></a></p>
<p style="text-align: left; ">Now let&#8217;s initialize our sounds objects.  Go to the viewDidLoad method and add the following code.</p>
<p style="text-align: center; "><a href="/wp-content/uploads/2009/05/screenshot_121.png"><img class="size-full wp-image-730 aligncenter" title="screenshot_12" src="/wp-content/uploads/2009/05/screenshot_121.png" alt="screenshot_12" width="709" height="223" /></a></p>
<p style="text-align: left; ">
<p style="text-align: left; ">
<p style="text-align: left; ">
<p style="text-align: left; ">Pretty straight forward.  The first line gets the path of the audio file.  The next line converts the path to a CFURLRef.  Finally, we load that audio file and associate it with the given sound ID. Notice we pass &amp;clappingFileID.  This is a pass by reference and allows the AudioServicesCreateSystemSoundID method to modify this variable.  We do the same thing to load the volley sound.  Now, let&#8217;s play the sounds.</p>
<h2>Playing The Sounds</h2>
<p>It&#8217;s actually quite simple to play the sounds we have loaded.  Simply call the AudioServicesPlaySystemSound method and pass it the soundID of the file you want to play.  So to play the clapping sound when someone scores, add this code to your reset method.</p>
<p style="text-align: center; "><a href="/wp-content/uploads/2009/05/screenshot_141.png"><img class="alignnone size-full wp-image-731" title="screenshot_14" src="/wp-content/uploads/2009/05/screenshot_141.png" alt="screenshot_14" width="322" height="52" /></a></p>
<p style="text-align: left; ">Notice, we are passing the clappingFileID to indicate we want to play the clapping sound.  Now, add the following code to gameLoop to play the racquet sound when someone hits the ball.</p>
<p style="text-align: center; "><a href="/wp-content/uploads/2009/05/screenshot_151.png"><img class="alignnone size-full wp-image-732" title="screenshot_15" src="/wp-content/uploads/2009/05/screenshot_151.png" alt="screenshot_15" width="381" height="209" /></a></p>
<p style="text-align: left; ">Pretty straight forward Right? Now click Build and Go and check out the game audio.  That concludes this tutorial.  If you have any questions or comments, feel free to leave them in the comments section of this post or write them to me on <a href="http://twitter.com/brandontreb">Twitter</a>.  You can also download the sample code <a href="/wp-content/uploads/2009/05/itennis-41.zip">here</a></p>
<p>Happy iCoding!</p>
<p style="text-align: left; ">
<p style="text-align: left; ">
<p style="text-align: left;">
]]></content:encoded>
			<wfw:commentRss>http://icodeblog.com/2009/05/04/iphone-game-programming-tutorial-part-4-basic-game-audio/feed/</wfw:commentRss>
		<slash:comments>70</slash:comments>
		</item>
		<item>
		<title>A Great iPhone Coding Resource</title>
		<link>http://icodeblog.com/2008/11/05/a-great-iphone-coding-resource/</link>
		<comments>http://icodeblog.com/2008/11/05/a-great-iphone-coding-resource/#comments</comments>
		<pubDate>Wed, 05 Nov 2008 18:42:03 +0000</pubDate>
		<dc:creator><![CDATA[brandontreb]]></dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[iPhone apps]]></category>
		<category><![CDATA[iPhone Coding]]></category>
		<category><![CDATA[iphone development]]></category>

		<guid isPermaLink="false">http://icodeblog.com/?p=544</guid>
		<description><![CDATA[I&#8217;m sure many of you are aware that <a href="http://www.tuaw.com/2008/07/23/stanford-to-offer-iphone-programming-course/">Stanford is offering an iPhone development class</a> this semester.   On my journeys through the interwebs, I discovered a link to the site where the lectures for this class were posted (via delicious).  
So for those of you who already didn&#8217;t know this, here is a link to the lecture slides (with sample code).
<a href="http://www.stanford.edu/class/cs193p/cgi-bin/index.php">http://www.stanford.edu/class/cs193p/cgi-bin/index.php</a>
I hope you find the examples and instruction here very useful. Have a great day  ...]]></description>
				<content:encoded><![CDATA[<p>I&#8217;m sure many of you are aware that <a href="http://www.tuaw.com/2008/07/23/stanford-to-offer-iphone-programming-course/">Stanford is offering an iPhone development class</a> this semester.   On my journeys through the interwebs, I discovered a link to the site where the lectures for this class were posted (via delicious).  </p>
<p>So for those of you who already didn&#8217;t know this, here is a link to the lecture slides (with sample code).</p>
<p><a href="http://www.stanford.edu/class/cs193p/cgi-bin/index.php">http://www.stanford.edu/class/cs193p/cgi-bin/index.php</a></p>
<p>I hope you find the examples and instruction here very useful. Have a great day and happy iCoding!</p>
]]></content:encoded>
			<wfw:commentRss>http://icodeblog.com/2008/11/05/a-great-iphone-coding-resource/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>No More iPhone Developer NDA</title>
		<link>http://icodeblog.com/2008/10/01/no-more-iphone-developer-nda/</link>
		<comments>http://icodeblog.com/2008/10/01/no-more-iphone-developer-nda/#comments</comments>
		<pubDate>Wed, 01 Oct 2008 16:02:58 +0000</pubDate>
		<dc:creator><![CDATA[brandontreb]]></dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[iphone development]]></category>
		<category><![CDATA[iphone NDA]]></category>

		<guid isPermaLink="false">http://icodeblog.com/?p=456</guid>
		<description><![CDATA[<a href="/wp-content/uploads/2008/10/iphonenda1.jpg"></a>
It looks like iCodeBlog is no longer illegal! Lol.  Apple has just announced that they are doing away with their &#8220;F-ing&#8221; NDA (as it has been coined).  This is great news for developers as we can now talk safely in the open about development for the iPhone  without fear.
This pesky NDA has been annoying developers since the beginning of (iPhone Development) time.  Maybe now, we will start seeing some great books being published for iPhone  ...]]></description>
				<content:encoded><![CDATA[<p style="text-align: center;"><a href="/wp-content/uploads/2008/10/iphonenda1.jpg"><img class="size-full wp-image-457 aligncenter" title="No Mas NDA!!1!" src="/wp-content/uploads/2008/10/iphonenda1.jpg" alt="" width="500" height="250" /></a></p>
<p style="text-align: left;">It looks like iCodeBlog is no longer illegal! Lol.  Apple has just announced that they are doing away with their &#8220;F-ing&#8221; NDA (as it has been coined).  This is great news for developers as we can now talk safely in the open about development for the iPhone  without fear.</p>
<p style="text-align: left;">This pesky NDA has been annoying developers since the beginning of (iPhone Development) time.  Maybe now, we will start seeing some great books being published for iPhone development.</p>
<p style="text-align: left;">Source: <a href="http://www.engadget.com/2008/10/01/apple-drops-iphone-nda/">Engaget</a></p>
]]></content:encoded>
			<wfw:commentRss>http://icodeblog.com/2008/10/01/no-more-iphone-developer-nda/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
