<?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</title>
	<atom:link href="/tag/iphone/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>Disable the iPhone&#8217;s Front Camera</title>
		<link>http://icodeblog.com/2011/08/23/disable-the-iphones-front-camera/</link>
		<comments>http://icodeblog.com/2011/08/23/disable-the-iphones-front-camera/#comments</comments>
		<pubDate>Tue, 23 Aug 2011 20:44:00 +0000</pubDate>
		<dc:creator><![CDATA[Marc Charbonneau]]></dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Camera]]></category>
		<category><![CDATA[Intermediate]]></category>
		<category><![CDATA[iOS]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[iPhone Coding]]></category>
		<category><![CDATA[iphone programming]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://icodeblog.com/?p=3334</guid>
		<description><![CDATA[The iPhone 4&#8217;s front camera is limited to 640&#215;480 resolution. Although handy for video conferencing, for some apps that&#8217;s to small to yield a usable photo. Unfortunately the UIImagePickerController class does not have an option to restrict the user from using the front camera. Although you can check the size of the photo after the user is finished, it&#8217;s not great user experience to reject it after they go through the entire process of taking a photo.
One option is to  ...]]></description>
				<content:encoded><![CDATA[<p>The iPhone 4&#8217;s front camera is limited to 640&#215;480 resolution. Although handy for video conferencing, for some apps that&#8217;s to small to yield a usable photo. Unfortunately the UIImagePickerController class does not have an option to restrict the user from using the front camera. Although you can check the size of the photo after the user is finished, it&#8217;s not great user experience to reject it after they go through the entire process of taking a photo.</p>
<p>One option is to replace the standard camera controls with a custom interface, but that&#8217;s a whole lot of work if you just want to prevent the user from taking a photo with the front camera. Fortunately there&#8217;s another option: put a transparent button over the &#8220;switch camera&#8221; button, which will intercept touch events and show an alert dialog. It sounds simple, but as you&#8217;ll see there are a few tricks to actually getting this to work.</p>
<p>We&#8217;re going to use UIImagePickerController&#8217;s <strong>cameraOverlayView</strong> property to add our button to the view hierarchy. We can&#8217;t simply provide a UIButton object though. In the latest iOS SDK, cameraOverlayView is automatically resized to fill the entire screen, while we only want to cover a small corner of it. Instead, we&#8217;re going to put the button inside a UIView subclass that will be used for layout and a few other tasks. Go ahead and create this UIView subclass in your project, call it <strong>ELCCameraOverlayView</strong>, and delete any methods the Xcode template includes by default.</p>
<p>We&#8217;ll need a button, so let&#8217;s start by giving our new subclass a UIButton instance variable named <strong>_button</strong> with a corresponding <strong>button</strong> property. Declare this as you&#8217;d declare any synthesized property, but let&#8217;s override the default setter method to also add it to the view hierarchy.</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>setButton<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIButton <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>button;
<span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> _button <span style="color: #002200;">!=</span> button <span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#123;</span>
        <span style="color: #002200;">&#91;</span>_button removeFromSuperview<span style="color: #002200;">&#93;</span>;
        <span style="color: #002200;">&#91;</span>_button release<span style="color: #002200;">&#93;</span>;
        _button <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>button retain<span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> button <span style="color: #002200;">!=</span> <span style="color: #a61390;">nil</span> <span style="color: #002200;">&#41;</span>
        	<span style="color: #002200;">&#91;</span>self addSubview<span style="color: #002200;">:</span>button<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>You might create the button in another class (such as a view controller) and assign it to our custom class, but you can also create a standard button in your initWithFrame: method. Note that we still have to call <strong>addSubview:</strong> here as we&#8217;re not using the property accessor method (which is discouraged in init methods).</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>initWithFrame<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CGRect<span style="color: #002200;">&#41;</span>frame;
<span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#40;</span> self <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>super initWithFrame<span style="color: #002200;">:</span>frame<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#123;</span>
        _button <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIButton alloc<span style="color: #002200;">&#93;</span> initWithFrame<span style="color: #002200;">:</span>CGRectMake<span style="color: #002200;">&#40;</span> 240.0f, 0.0f, 80.0f, 80.0f <span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
        <span style="color: #002200;">&#91;</span>self addSubview<span style="color: #002200;">:</span>_button<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></pre></td></tr></table></div>

<p>With the button in place, let&#8217;s move on to intercepting touch events which would normally go to the &#8220;switch cameras&#8221; button. We can do this by overriding the UIView methods which are called to determine if an event occurred within a view&#8217;s frame or not. Just check to see if the event took place within the button&#8217;s frame rectangle.</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>UIView <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>hitTest<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CGPoint<span style="color: #002200;">&#41;</span>point withEvent<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIEvent <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>event;
<span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span>super hitTest<span style="color: #002200;">:</span>point withEvent<span style="color: #002200;">:</span>event<span style="color: #002200;">&#93;</span> <span style="color: #002200;">==</span> self.button <span style="color: #002200;">&#41;</span>
        <span style="color: #a61390;">return</span> self.button;
&nbsp;
    <span style="color: #a61390;">return</span> <span style="color: #a61390;">nil</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span>pointInside<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CGPoint<span style="color: #002200;">&#41;</span>point withEvent<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIEvent <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>event;
<span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> CGRectContainsPoint<span style="color: #002200;">&#40;</span> self.button.frame, point <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#41;</span>
        <span style="color: #a61390;">return</span> <span style="color: #a61390;">YES</span>;
&nbsp;
    <span style="color: #a61390;">return</span> <span style="color: #a61390;">NO</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>It may seem like we&#8217;re almost done at this point. If you set the camera overlay view to an instance of this class you&#8217;ll see the &#8220;switch camera&#8221; button should no longer work, and if you give the button an action method you can pop up an alert message telling users not to use the front camera. However, if you rotate the phone to landscape mode, you&#8217;ll notice that while the &#8220;switch camera&#8221; button is re-positioned, our custom button is not! To see what I mean, try setting a custom background color on our button so you can see it on screen.</p>
<p>This is a tricky problem. Unlike UIViewController, our UIView subclass does not have any way of telling when the interface orientation is changed. This doesn&#8217;t even matter though, since UIImagePickerController doesn&#8217;t actually change its interface orientation, it simply re-arranges the camera buttons while remaining in UIInterfaceOrientationPortrait! </p>
<p>The way I&#8217;ve solved this problem is to use the accelerometer to determine the device orientation. It might sound complicated, but it&#8217;s actually not a lot of work. Start by adding a new instance variable <strong>_interfaceOrientation</strong> and property <strong>interfaceOrientation</strong> to your class, of type <strong>UIInterfaceOrientation</strong>. We&#8217;ll also override its setter method to call <strong>setNeedsLayout</strong> whenever its changed.</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>setInterfaceOrientation<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIInterfaceOrientation<span style="color: #002200;">&#41;</span>interfaceOrientation;
<span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> _interfaceOrientation <span style="color: #002200;">!=</span> interfaceOrientation <span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#123;</span>
        _interfaceOrientation <span style="color: #002200;">=</span> interfaceOrientation;
        <span style="color: #002200;">&#91;</span>self setNeedsLayout<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>Now let&#8217;s move the button whenever the orientation changes, and implement the required <strong>UIAccelerometerDelegate</strong> 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>layoutSubviews;
<span style="color: #002200;">&#123;</span>
    CGFloat width <span style="color: #002200;">=</span> CGRectGetWidth<span style="color: #002200;">&#40;</span> self.button.frame <span style="color: #002200;">&#41;</span>;
    CGFloat height <span style="color: #002200;">=</span> CGRectGetHeight<span style="color: #002200;">&#40;</span> self.button.frame <span style="color: #002200;">&#41;</span>;
&nbsp;
    <span style="color: #a61390;">switch</span> <span style="color: #002200;">&#40;</span> self.interfaceOrientation <span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#123;</span>
        <span style="color: #a61390;">case</span> UIInterfaceOrientationPortrait<span style="color: #002200;">:</span>
        <span style="color: #a61390;">case</span> UIInterfaceOrientationPortraitUpsideDown<span style="color: #002200;">:</span>
            self.button.frame <span style="color: #002200;">=</span> CGRectMake<span style="color: #002200;">&#40;</span> CGRectGetMaxX<span style="color: #002200;">&#40;</span> self.bounds <span style="color: #002200;">&#41;</span> <span style="color: #002200;">-</span> width, 0.0f, width, height <span style="color: #002200;">&#41;</span>;
            <span style="color: #a61390;">break</span>;
        <span style="color: #a61390;">case</span> UIInterfaceOrientationLandscapeRight<span style="color: #002200;">:</span>
            self.button.frame <span style="color: #002200;">=</span> CGRectMake<span style="color: #002200;">&#40;</span> CGRectGetMaxX<span style="color: #002200;">&#40;</span> self.bounds <span style="color: #002200;">&#41;</span> <span style="color: #002200;">-</span> width, CGRectGetMaxY<span style="color: #002200;">&#40;</span> self.bounds <span style="color: #002200;">&#41;</span> <span style="color: #002200;">-</span> height <span style="color: #002200;">-</span> 50.0f, width, height <span style="color: #002200;">&#41;</span>;
            <span style="color: #a61390;">break</span>;
        <span style="color: #a61390;">case</span> UIInterfaceOrientationLandscapeLeft<span style="color: #002200;">:</span>
            self.button.frame <span style="color: #002200;">=</span> CGRectMake<span style="color: #002200;">&#40;</span> 0.0f, 0.0f, width, height <span style="color: #002200;">&#41;</span>;
            <span style="color: #a61390;">break</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>accelerometer<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIAccelerometer <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>accelerometer didAccelerate<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIAcceleration <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>acceleration;
<span style="color: #002200;">&#123;</span> 
	CGFloat x <span style="color: #002200;">=</span> <span style="color: #002200;">-</span><span style="color: #002200;">&#91;</span>acceleration x<span style="color: #002200;">&#93;</span>; 
	CGFloat y <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>acceleration y<span style="color: #002200;">&#93;</span>; 
	CGFloat angle <span style="color: #002200;">=</span> <span style="color: #a61390;">atan2</span><span style="color: #002200;">&#40;</span>y, x<span style="color: #002200;">&#41;</span>; 
&nbsp;
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> angle &gt;<span style="color: #002200;">=</span> <span style="color: #002200;">-</span>2.25f <span style="color: #002200;">&amp;&amp;</span> angle &lt;<span style="color: #002200;">=</span> <span style="color: #002200;">-</span>0.25f <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
        self.interfaceOrientation <span style="color: #002200;">=</span> UIInterfaceOrientationPortrait;
	<span style="color: #002200;">&#125;</span>
	<span style="color: #a61390;">else</span> <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> angle &gt;<span style="color: #002200;">=</span> <span style="color: #002200;">-</span>1.75f <span style="color: #002200;">&amp;&amp;</span> angle &lt;<span style="color: #002200;">=</span> 0.75f <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
        self.interfaceOrientation <span style="color: #002200;">=</span> UIInterfaceOrientationLandscapeRight;
	<span style="color: #002200;">&#125;</span>
	<span style="color: #a61390;">else</span> <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> angle &gt;<span style="color: #002200;">=</span> 0.75f <span style="color: #002200;">&amp;&amp;</span> angle &lt;<span style="color: #002200;">=</span> 2.25f <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
        self.interfaceOrientation <span style="color: #002200;">=</span> UIInterfaceOrientationPortraitUpsideDown;
	<span style="color: #002200;">&#125;</span>
	<span style="color: #a61390;">else</span> <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> angle &lt;<span style="color: #002200;">=</span> <span style="color: #002200;">-</span>2.25f || angle &gt;<span style="color: #002200;">=</span> 2.25f <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
        self.interfaceOrientation <span style="color: #002200;">=</span> UIInterfaceOrientationLandscapeLeft;
	<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>We&#8217;re almost done! Just make sure to start and stop the accelerometer in your init and dealloc methods.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>initWithFrame<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CGRect<span style="color: #002200;">&#41;</span>frame;
<span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#40;</span> self <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>super initWithFrame<span style="color: #002200;">:</span>frame<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#123;</span>
        _interfaceOrientation <span style="color: #002200;">=</span> UIInterfaceOrientationPortrait;
        _button <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIButton alloc<span style="color: #002200;">&#93;</span> initWithFrame<span style="color: #002200;">:</span>CGRectMake<span style="color: #002200;">&#40;</span> 240.0f, 0.0f, 80.0f, 80.0f <span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
        <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIAccelerometer sharedAccelerometer<span style="color: #002200;">&#93;</span> setDelegate<span style="color: #002200;">:</span>self<span style="color: #002200;">&#93;</span>;
        <span style="color: #002200;">&#91;</span>self addSubview<span style="color: #002200;">:</span>_button<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><span style="color: #002200;">&#91;</span>UIAccelerometer sharedAccelerometer<span style="color: #002200;">&#93;</span> setDelegate<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>_button release<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>super dealloc<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>Finished! If you haven&#8217;t tested your overlay yet, you can add it to your UIImagePickerController as so.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;">UIImagePickerController <span style="color: #002200;">*</span>controller <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIImagePickerController alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
ELCCameraOverlayView <span style="color: #002200;">*</span>view <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>ELCCameraOverlayView alloc<span style="color: #002200;">&#93;</span> initWithFrame<span style="color: #002200;">:</span>controller.view.frame<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #002200;">&#91;</span>view.button addTarget<span style="color: #002200;">:</span>self action<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>selectFrontCamera<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span> forControlEvents<span style="color: #002200;">:</span>UIControlEventTouchUpInside<span style="color: #002200;">&#93;</span>;
&nbsp;
controller.sourceType <span style="color: #002200;">=</span> UIImagePickerControllerSourceTypeCamera;
controller.cameraCaptureMode <span style="color: #002200;">=</span> UIImagePickerControllerCameraCaptureModePhoto;
controller.cameraDevice <span style="color: #002200;">=</span> UIImagePickerControllerCameraDeviceRear;
controller.delegate <span style="color: #002200;">=</span> self;
controller.cameraOverlayView <span style="color: #002200;">=</span> view;
&nbsp;
<span style="color: #002200;">&#91;</span>self.navigationController presentModalViewController<span style="color: #002200;">:</span>controller animated<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>controller release<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>view release<span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

<p>Don&#8217;t forget to provide the button&#8217;s action method. You could show an alert, for instance.</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>selectFrontCamera<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>sender;
<span style="color: #002200;">&#123;</span>
    <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>title <span style="color: #002200;">=</span> NSLocalizedString<span style="color: #002200;">&#40;</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Front Camera Disabled&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;&quot;</span> <span style="color: #002200;">&#41;</span>;
    <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>message <span style="color: #002200;">=</span> NSLocalizedString<span style="color: #002200;">&#40;</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;The front camera does not have sufficient resolution.&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;&quot;</span> <span style="color: #002200;">&#41;</span>;
    <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>button <span style="color: #002200;">=</span> NSLocalizedString<span style="color: #002200;">&#40;</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Okay&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;&quot;</span> <span style="color: #002200;">&#41;</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>title message<span style="color: #002200;">:</span>message delegate<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span> cancelButtonTitle<span style="color: #002200;">:</span>button otherButtonTitles<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
&nbsp;
    <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></pre></td></tr></table></div>

<p>I&#8217;ve uploaded the <a href="https://gist.github.com/1166388">complete ELCCameraOverlayView class files here</a>. Leave a comment if you have any questions!</p>
]]></content:encoded>
			<wfw:commentRss>http://icodeblog.com/2011/08/23/disable-the-iphones-front-camera/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Back To Basics: Hello iPhone</title>
		<link>http://icodeblog.com/2011/08/17/back-to-basics-hello-iphone/</link>
		<comments>http://icodeblog.com/2011/08/17/back-to-basics-hello-iphone/#comments</comments>
		<pubDate>Wed, 17 Aug 2011 21:01:43 +0000</pubDate>
		<dc:creator><![CDATA[brandontreb]]></dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[beginner]]></category>
		<category><![CDATA[hello world]]></category>
		<category><![CDATA[iOS]]></category>
		<category><![CDATA[iphone]]></category>

		<guid isPermaLink="false">http://icodeblog.com/?p=3302</guid>
		<description><![CDATA[Today I&#8217;m going to show you how to make a simple &#8220;Hello World&#8221; project on the iPhone. There are quite a few ways that this can be accomplished, however I am going to show you the way that I feel will be the most beneficial.
While I know many of iCodeBlog&#8217;s readers are veteran developers, this tutorial (and many in this series) are going to be geared towards the more novice developer. This group is often overlooked when it comes to  ...]]></description>
				<content:encoded><![CDATA[<p>Today I&#8217;m going to show you how to make a simple &#8220;Hello World&#8221; project on the iPhone. There are quite a few ways that this can be accomplished, however I am going to show you the way that I feel will be the most beneficial.</p>
<p>While I know many of iCodeBlog&#8217;s readers are veteran developers, this tutorial (and many in this series) are going to be geared towards the more novice developer. This group is often overlooked when it comes to tutorials, and my hope is to help lay a solid foundation for them (in addition to honing the skills of the veteran developers). So, let&#8217;s dig in.</p>
<h4>Introduction</h4>
<p>Generally, when you make a &#8220;Hello World&#8221; project, the goal is to get the text &#8220;Hello World&#8221; to display on the screen or the console. On the iPhone/iPad, there are quite a few ways to display text on the screen (labels, text areas, text views, core text, etc&#8230;). Today we are just going to focus on drawing the text using a <strong>UILabel</strong>.</p>
<p>There are two ways to get a UILabel to display on the screen. The first is to use Apple&#8217;s <strong>Interface Builder</strong> tool. Interface Builder is an extremely handy tool for creating iOS interfaces visually. It supports drag and drop and allows you to configure all of the properties of each of the elements. While it is a great tool, I want to show you a bit what&#8217;s happening under the hood in code. It is important not to lean on Interface Builder as a crutch when creating interfaces as there are times when you will have to add UI elements manually. That being said, I will have an intro to Interface Builder tutorial up later in this series.</p>
<h4>Creating The Project</h4>
<p>Before we begin make sure you have read and understand the following tutorial:</p>
<p><a href="/2011/07/18/back-to-basics-getting-set-up-for-ios-development/">Back To Basics &#8211; Getting Set Up For iOS Development</a></p>
<p>This tutorial will ensure that you have the proper environment set up for iOS development.</p>
<p>When you first launch XCode, you are prompted with a screen similar to this one:</p>
<p><a href="/wp-content/uploads/2011/08/Screen-shot-2011-08-17-at-12.51.31-PM.png"><img class="size-full wp-image-3325 alignnone" title="Screen shot 2011-08-17 at 12.51.31 PM" src="/wp-content/uploads/2011/08/Screen-shot-2011-08-17-at-12.51.31-PM.png" alt="" width="600" height="393" /></a></p>
<p>To create a new project, simply click on <strong>Create a new XCode project</strong>. This will bring up a wizard guiding you through the initial project set up.</p>
<p>Built into XCode are several different project templates to be used as a base for the most common projects that you might want to create. Each one comes with some specific &#8220;boiler plate&#8221; code to quickly get you up and running.</p>
<p><a href="/wp-content/uploads/2011/08/Screen-shot-2011-08-17-at-12.56.43-PM.png"><img class="size-full wp-image-3326 alignnone" title="Screen shot 2011-08-17 at 12.56.43 PM" src="/wp-content/uploads/2011/08/Screen-shot-2011-08-17-at-12.56.43-PM.png" alt="" width="600" height="408" /></a></p>
<p>We are going to start by clicking on <strong>View-based Application</strong>. This will give us enough code to have a simple view on the screen. A UIView is the simplest of display objects. Every other UI element that you draw on the screen will inherit from UIView. In the case of a view-based application, we are given a full screen blank view to start with. We can then add display objects as subviews.</p>
<p>After selecting View-based Application, click next. This screen will show some simple project options such as name, company identifier, and Device family.</p>
<p><a href="/wp-content/uploads/2011/08/Screen-shot-2011-08-17-at-1.01.18-PM.png"><img class="size-full wp-image-3327 alignnone" title="Screen shot 2011-08-17 at 1.01.18 PM" src="/wp-content/uploads/2011/08/Screen-shot-2011-08-17-at-1.01.18-PM.png" alt="" width="600" height="408" /></a></p>
<p>Here&#8217;s a bit about each:</p>
<ul>
<li><strong>Name:</strong> This is simply the name of the application. Don&#8217;t worry you are not bound to this and you can easily change the display name.</li>
<li><strong>Company Identifier</strong> This is unique id used to identify your application. It will be prepended to your project name to form the unique id that the app store uses. Identifiers are usually of the form com.companyName.projectName.</li>
<li><strong>Device Family:</strong> This will be either iPhone or iPad. You can always upgrade to universal later (tutorial to come).</li>
</ul>
<h4>The Files</h4>
<p>Once your project has been created, you will have 6 files of interest (for this tutorial&#8217;s sake):</p>
<p><strong>HelloiPhoneAppDelegate.h/m</strong></p>
<p>The app delegate is the point of entry for your application. It&#8217;s a singleton class (meaning there is only one), and it is responsible for displaying the main window/view of the application. You generally want to keep this class pretty lean and offload the work somewhere else. It&#8217;s often a n00b mistake to want to work on your UI/logic in the app delegate. For this project, we are not going to touch it at all. Just note that it is what&#8217;s launching your main view.</p>
<p><strong>MainWindow.xib</strong></p>
<p>Xib files are to be used with Interface Builders. They are basically xml files that Interface Builder uses to represent a user interface. In this case the MainWindow.xib (pronounced nib), is responsible for loading up our Window and Main ViewController (HelloiPhoneViewController). The View Controller could have been loaded programmatically in the App Delegate, however Apple has chosen to load in using a nib in Interface Builder for this template. I&#8217;m not going to go into detail about that in this tutorial, but you can see for yourself by opening up MainWindow.xib and look under <strong>objects</strong>.</p>
<p><strong>HelloiPhoneViewController</strong></p>
<p>iOS development tends to really enforce <a href="http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller">Model-View-Controller</a> design patterns. This is basically the idea of separating the view code from the model code (using the controller code as the glue). Generally, in iOS when you have a view controller, it has a corresponding nib file(the view) to go along with it. HelloiPhoneViewcontroller.xib is where you would drag and drop UI elements to customize the view using the Interface Builder tool. As I mentioned in the beginning, we are going to ignore this file and add our text manually.</p>
<h4>The Code</h4>
<p>Well, this has been quite a bit of talk just to get us to the simple code below. Open up the file HelloiPhoneViewController.m and find the method called <strong>- (void) viewDidLoad </strong>. This method fires automatically when the view is first loaded. Make sure to uncomment it if it&#8217;s commented out. Inside of this method, add the following code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>viewDidLoad <span style="color: #002200;">&#123;</span>
    <span style="color: #002200;">&#91;</span>super viewDidLoad<span style="color: #002200;">&#93;</span>;
&nbsp;
    CGRect frame <span style="color: #002200;">=</span> CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">10</span>, <span style="color: #2400d9;">10</span>, <span style="color: #2400d9;">300</span>, <span style="color: #2400d9;">25</span><span style="color: #002200;">&#41;</span>;
    UILabel <span style="color: #002200;">*</span>helloLabel <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UILabel alloc<span style="color: #002200;">&#93;</span> initWithFrame<span style="color: #002200;">:</span>frame<span style="color: #002200;">&#93;</span>;
    helloLabel.text <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Hello iPhone!&quot;</span>;
    helloLabel.textColor <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIColor blueColor<span style="color: #002200;">&#93;</span>;
    helloLabel.textAlignment <span style="color: #002200;">=</span> UITextAlignmentCenter;
    <span style="color: #002200;">&#91;</span>self.view addSubview<span style="color: #002200;">:</span>helloLabel<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>helloLabel release<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>Let&#8217;s break down this code:</p>
<p><strong>Line 4:</strong> This creates a frame. Think of a frame as a box. The first two parameters are the starting x and y locations of the box, and the second two are the width and height of the box. Every display object you will create has a frame. It tells the device how big and where to draw a view.</p>
<p><strong>Line 5:</strong> On this line we create a new label object. The alloc says &#8220;give me the memory large enough for a UIlabel&#8221; and the init initializes all of the view properties. You will never really see these two methods called apart from each other. We call one of the copy constructors of UILabel called initWithFrame. This allows us to pass in the view&#8217;s frame on initialization.</p>
<p><strong>Lines 6-8:</strong> In these 3 lines, we configure various properties of a UILabel. The first is the actual text to be displayed, the second is the color of the text, and the third tells the text to center inside of it&#8217;s frame. There are quite a few options you can set with regards to any UI element and they can be found in the documentation for that particular object.</p>
<p><strong>Line 9:</strong> This is where we actually get the label to display. All views are subviews of some other view (even your main view is a subview of the window). We are basically attaching our label to the main view to be drawn. Without this line, your label would never be seen.</p>
<p><strong>Line 10:</strong> I don&#8217;t want to go <em>too</em> much into this right now, but The iOS platform (as of iOS versions &lt; 5) doesn&#8217;t have garbage collection. What this means is, we need to handle our memory management ourselves. That being said, as a general rule, whenever you do an &#8220;alloc init&#8221; on an object, you must have a corresponding release call to give that memory back to the system. Otherwise, you have a memory leak. More to come on this.</p>
<h4>Running The Application</h4>
<p>OK, now that we have the code in place it&#8217;s time to run it in the simulator. To do this, select iPhone X.X Simulator in the dropdown at the very top of XCode (currently mine says 4.3). Then click the <strong>Run</strong> button. Your code will be compiled and the simulator will start. You should now see your _beautiful Interface_.</p>
<p><a href="/wp-content/uploads/2011/08/Screen-shot-2011-08-17-at-2.22.21-PM.png"><img class="size-full wp-image-3328" title="Screen shot 2011-08-17 at 2.22.21 PM" src="/wp-content/uploads/2011/08/Screen-shot-2011-08-17-at-2.22.21-PM.png" alt="" width="387" height="735" /></a></p>
<div style="clear:both">&nbsp;</div>
<p>Well, it&#8217;s not much to look at, but try reading the documentation and adding some other interface elements to the view. Or, read the documentation for UILabel and change some of the properties.</p>
<p>If you have any questions or comments, feel free to leave them in the comments section of this post or send them to me on <a href="http://twitter.com/brandontreb">Twitter</a>.</p>
<p>You can download the source code for this tutorial <a href="/wp-content/uploads/2011/08/HelloiPhone.zip">here</a>.</p>
<p>Happy iCoding!</p>
<p><span style="font-family: 'Lucida Grande';"><strong><span style="font-weight: normal;"><span style="font-family: arial, verdana, tahoma, sans-serif; font-size: 13px; line-height: 20px;"><em>This post is part of an iOS development series called Back To Basics.  You can keep up with this series through the <a style="text-decoration: none; color: #004199; padding: 0px; margin: 0px;" href="/2011/07/12/iphone-development-back-to-basics/">table of contents</a>, <a style="text-decoration: none; color: #004199; padding: 0px; margin: 0px;" href="/tag/b2b/feed/">RSS feed</a>, or <a style="text-decoration: none; color: #004199; padding: 0px; margin: 0px;" href="https://twitter.com/#!/brandontreb">Twitter</a>.</em></span></span></strong></span></p>
]]></content:encoded>
			<wfw:commentRss>http://icodeblog.com/2011/08/17/back-to-basics-hello-iphone/feed/</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
		<item>
		<title>Update: ELCImagePickerController</title>
		<link>http://icodeblog.com/2011/03/03/update-elcimagepickercontroller/</link>
		<comments>http://icodeblog.com/2011/03/03/update-elcimagepickercontroller/#comments</comments>
		<pubDate>Thu, 03 Mar 2011 00:23:14 +0000</pubDate>
		<dc:creator><![CDATA[Matt Tuzzolo]]></dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Featured]]></category>
		<category><![CDATA[Advanced]]></category>
		<category><![CDATA[ELCImagePickerController]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[UIImagePickerController]]></category>

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

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

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

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

<p>Then when the block is finished enumerating, it&#8217;ll call reloadTable as well.  On an album with roughly 1500 photos, it was pretty tough to reach the bottom of the tableview before the block finished enumerating.  So this is a pretty decent solution to the issue.  Originally, I experimented with lazy loading the assets as the user scrolled through the table, but ultimately wasn&#8217;t able to get the performance I wanted out of it.</p>
<p>This release of ELCImagePickerController should perform faster, and with a smaller memory footprint than before.  I hope you enjoy it.</p>
<p>You can follow me on twitter <a href="http://twitter.com/matt_tuzzolo">@matt_tuzzolo</a> or get in touch with us at <a href="http://www.elctech.com">http://www.elctech.com</a></p>
<p>ps. for an extra bonus, check out [ELCAsset toggleSelection];</p>
]]></content:encoded>
			<wfw:commentRss>http://icodeblog.com/2011/03/03/update-elcimagepickercontroller/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Join Me At The Voices That Matter iOS Dev Conference **Coupon Code**</title>
		<link>http://icodeblog.com/2010/09/13/join-me-at-the-voices-that-matter-ios-dev-conference-coupon-code/</link>
		<comments>http://icodeblog.com/2010/09/13/join-me-at-the-voices-that-matter-ios-dev-conference-coupon-code/#comments</comments>
		<pubDate>Mon, 13 Sep 2010 17:25:53 +0000</pubDate>
		<dc:creator><![CDATA[brandontreb]]></dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[coupon]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[voices that matter discount]]></category>
		<category><![CDATA[vtm]]></category>

		<guid isPermaLink="false">http://icodeblog.com/?p=2358</guid>
		<description><![CDATA[Learn from Industry Leaders Who Literally "Wrote the Books" on iOS Development


Coming to you direct from Addison-Wesley Professional, which has published some of the leading books in the field, the Voices That Matter: iPhone Developers Conference is taking place October 16-17 in Philadelphia!

Take a look around at all the tech books you have on your physical and digital bookshelves. If you've been developing for the Mac, the iPhone and the iPad - chances are good that you rely on books by Steve Kochan, Erica Sadun, Aaron Hillegass and Jeff LaMarche. Even more, you probably follow the Tweets and blogs of folks like Graham Lee, Mike Lee, Matt Long and Chris Adamson. Wouldn't it be great to meet and learn from these thought-leaders in person? Now you can at the Voices That Matter: iPhone Developers Conference!]]></description>
				<content:encoded><![CDATA[<p><a href="http://www.voicesthatmatter.com/iphonefall2010/register.aspx"><img class="alignright size-full wp-image-2359" title="150x150DontMissiPhoneVTM" src="/wp-content/uploads/2010/09/150x150DontMissiPhoneVTM1.jpg" alt="" width="150" height="150" /></a><strong>Learn from Industry Leaders Who Literally &#8220;Wrote the Books&#8221; on iOS Development</strong></p>
<p><strong> </strong></p>
<p>Coming to you direct from Addison-Wesley Professional, which has published some of the leading books in the field, the <strong><a href="http://www.voicesthatmatter.com/iphonefall2010/">Voices That Matter: iPhone Developers Conference</a></strong> is taking place October 16-17 in Philadelphia!</p>
<p>Take a look around at all the tech books you have on your physical and digital bookshelves. If you&#8217;ve been developing for the Mac, the iPhone and the iPad &#8211; chances are good that you rely on books by <strong>Steve Kochan, Erica Sadun, Aaron Hillegass</strong> and <strong>Jeff LaMarche</strong>. Even more, you probably follow the Tweets and blogs of folks like <strong>Graham Lee, Mike Lee, Matt Long</strong> and <strong>Chris Adamson</strong>. Wouldn&#8217;t it be great to meet and learn from these thought-leaders in person? Now you can at the Voices That Matter: iPhone Developers Conference!</p>
<p>You&#8217;ll acquire skills for mastering iOS development during a weekend of robust educational sessions. Our speakers are eager to share their knowledge, answer your questions and address your application needs. You&#8217;ll participate in interactive discussions that provide the perfect environment for an unbiased and effective learning experience.</p>
<p><strong><span style="text-decoration: underline;"> </span></strong></p>
<p><strong><span style="text-decoration: underline;">SPECIAL SAVINGS!</span> </strong>As someone that reads this blog, you can save $100 off the conference fee by providing priority code PHBLOGS when registering. <strong><a href="http://www.voicesthatmatter.com/iphonefall2010/register.aspx">Register Now!</a></strong></p>
<h2><strong>Coupon Code: PHBLOGS</strong></h2>
<p><strong><br />
</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://icodeblog.com/2010/09/13/join-me-at-the-voices-that-matter-ios-dev-conference-coupon-code/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Adding Local Weather Conditions To Your App (Part 1/2: Implementing CoreLocation)</title>
		<link>http://icodeblog.com/2010/09/03/adding-local-weather-conditions-to-your-app-part-12-implementing-corelocation/</link>
		<comments>http://icodeblog.com/2010/09/03/adding-local-weather-conditions-to-your-app-part-12-implementing-corelocation/#comments</comments>
		<pubDate>Fri, 03 Sep 2010 22:45:12 +0000</pubDate>
		<dc:creator><![CDATA[Matt Tuzzolo]]></dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Coordinates]]></category>
		<category><![CDATA[CoreLocation]]></category>
		<category><![CDATA[GPS]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[Latitude]]></category>
		<category><![CDATA[Longitude]]></category>
		<category><![CDATA[objective-c]]></category>
		<category><![CDATA[xcode]]></category>

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

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

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

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


<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">//  LocationGetter.m</span>
<span style="color: #11740a; font-style: italic;">//  CoreLocationExample</span>
<span style="color: #11740a; font-style: italic;">//</span>
<span style="color: #11740a; font-style: italic;">//  Created by Matt on 9/3/10.</span>
<span style="color: #11740a; font-style: italic;">//  Copyright 2009 iCodeBlog. All rights reserved.</span>
<span style="color: #11740a; font-style: italic;">//</span>
&nbsp;
<span style="color: #6e371a;">#import &quot;LocationGetter.h&quot;</span>
<span style="color: #6e371a;">#import &lt;coreLocation/CoreLocation.h&gt;</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> LocationGetter
&nbsp;
<span style="color: #a61390;">@synthesize</span> locationManager, delegate;
<span style="color: #a61390;">BOOL</span> didUpdate <span style="color: #002200;">=</span> <span style="color: #a61390;">NO</span>;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>startUpdates
<span style="color: #002200;">&#123;</span>
    NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Starting Location Updates&quot;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>locationManager <span style="color: #002200;">==</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span>
        locationManager <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>CLLocationManager alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
&nbsp;
    locationManager.delegate <span style="color: #002200;">=</span> self;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// locationManager.distanceFilter = 1000;  // update is triggered after device travels this far (meters)</span>
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Alternatively you can use kCLLocationAccuracyHundredMeters or kCLLocationAccuracyHundredMeters, though higher accuracy takes longer to resolve</span>
    locationManager.desiredAccuracy <span style="color: #002200;">=</span> kCLLocationAccuracyKilometer;
    <span style="color: #002200;">&#91;</span>locationManager startUpdatingLocation<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>locationManager<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CLLocationManager <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>manager didFailWithError<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSError</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>error
<span style="color: #002200;">&#123;</span>
    UIAlertView <span style="color: #002200;">*</span>alert <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIAlertView alloc<span style="color: #002200;">&#93;</span> initWithTitle<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Error&quot;</span> message<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Your location could not be determined.&quot;</span> delegate<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span> cancelButtonTitle<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;OK&quot;</span> otherButtonTitles<span style="color: #002200;">:</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>alert show<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>alert release<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// Delegate method from the CLLocationManagerDelegate protocol.</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>locationManager<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CLLocationManager <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>manage didUpdateToLocation<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CLLocation <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>newLocation fromLocation<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CLLocation <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>oldLocation
<span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>didUpdate<span style="color: #002200;">&#41;</span>
        <span style="color: #a61390;">return</span>;
&nbsp;
    didUpdate <span style="color: #002200;">=</span> <span style="color: #a61390;">YES</span>;
    <span style="color: #11740a; font-style: italic;">// Disable future updates to save power.</span>
    <span style="color: #002200;">&#91;</span>locationManager stopUpdatingLocation<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// let our delegate know we're done</span>
    <span style="color: #002200;">&#91;</span>delegate newPhysicalLocation<span style="color: #002200;">:</span>newLocation<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>dealloc
<span style="color: #002200;">&#123;</span>
    <span style="color: #002200;">&#91;</span>locationManager release<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #002200;">&#91;</span>super dealloc<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></td></tr></table></div>

<p>CoreLocation gives you a few options for the accuracy of the user&#8217;s location.  The more accurate the measurement, typically the longer it takes LocationManager to call it&#8217;s delegate method didUpdateToLocation.  It&#8217;s just something to keep in mind when deciding what level of accuracy to use.</p>
<p>Next we need to actually invoke this code and start getting location updates.  I usually do this in my AppDelegate&#8217;s didFinishLaunchingWithOptions, though you could also do it in viewDidLoad somewhere if you didn&#8217;t need to know the user&#8217;s location on app startup.</p>

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

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

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

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

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

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

		<guid isPermaLink="false">http://icodeblog.com/?p=2165</guid>
		<description><![CDATA[Way back when, when everyone was still complaining about Apple's lack of support for (3rd party) multitasking, there was a simple solution put in place. This solution was known as push notifications.

Push notifications solved many of the issues associated with background processing.  For example, when quitting the AIM application, the server could keep you logged in and send you a push notification when a new message arrived.  You could then tap on a View button that would launch the app.

This solution is great and all, but it still requires that you have an active internet connection.  As of iOS4, Apple has introduced a new type of notification that can be scheduled to fire within the device itself.  It requires no complicated server programming, or additional configuration with iTunes.  I am talking about Local Notifications.]]></description>
				<content:encoded><![CDATA[<p><a href="/wp-content/uploads/2010/07/Screen-shot-2010-07-29-at-11.31.32-AM1.png"><img class="size-full wp-image-2181 alignright" title="Screen shot 2010-07-29 at 11.31.32 AM" src="/wp-content/uploads/2010/07/Screen-shot-2010-07-29-at-11.31.32-AM1.png" alt="" width="289" height="398" /></a>Way back when, when everyone was still complaining about Apple&#8217;s lack of support for (3rd party) multitasking, there was a simple solution put in place. This solution was known as push notifications.</p>
<p>Push notifications solved many of the issues associated with background processing.  For example, when quitting the AIM application, the server could keep you logged in and send you a push notification when a new message arrived.  You could then tap on a View button that would launch the app.</p>
<p>This solution is great and all, but it still requires that you have an active internet connection.  As of iOS4, Apple has introduced a new type of notification that can be scheduled to fire within the device itself.  It requires no complicated server programming, or additional configuration with iTunes.  I am talking about <strong>Local Notifications</strong>.</p>
<p>Local notifications can be scheduled on the user&#8217;s iDevice to fire at any given time; you can even set them to be recurring.  Today, we will explore these notifications and I will provide you with a simple example of how to schedule, view, and handle local notifications.  Here is a quick screenshot of the project that we are going to create (note I am using the iPhone 4  simulator).</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2010/07/Screen-shot-2010-07-29-at-11.28.46-AM1.png"><img class="alignnone size-full wp-image-2168" title="Screen shot 2010-07-29 at 11.28.46 AM" src="/wp-content/uploads/2010/07/Screen-shot-2010-07-29-at-11.28.46-AM1.png" alt="" width="482" height="664" /></a></p>
<p style="text-align: left;">The project will allow a user to schedule a location notification to fire off at a given date.  Using the text field, they are also able to specify some text for the notification.  The table view below displays a list of all of the currently scheduled location notifications within the application.</p>
<p style="text-align: left;">So, by now (if you are an avid iCodeBlog reader), you are already a killer iPhone dev and I can rush through the stuff that is not related to the notifications.  I will try to provide links to tutorials about sections that I rush through as well.</p>
<h2>1. Create a View-Based Application</h2>
<p style="text-align: left;">We will be using this as our starting point.  Check out <a href="/2008/07/29/iphone-programming-tutorial-beginner-interface-builder-hello-world/">this tutorial</a> if you are unfamiliar with doing this.  Name the project <strong>Notifier</strong>.</p>
<h2>2. Create All Properties and Dummy IBActions</h2>
<p style="text-align: left;">This is usually a good first step when tackling an application like this.  Let&#8217;s get everything set up in the .h and .m files so we only have to visit Interface Builder Once.  Here is what our NotifierViewController.h file looks like.</p>
<p style="text-align: left;">

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> NotifierViewController <span style="color: #002200;">:</span> UIViewController<span style="color: #002200;">&amp;</span>lt;UITableViewDelegate,UITableViewDataSource<span style="color: #002200;">&amp;</span>gt; <span style="color: #002200;">&#123;</span>
	IBOutlet UITableView <span style="color: #002200;">*</span>tableview;
	IBOutlet UIDatePicker <span style="color: #002200;">*</span>datePicker;
	IBOutlet UITextField <span style="color: #002200;">*</span>eventText;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, retain<span style="color: #002200;">&#41;</span> IBOutlet UITableView <span style="color: #002200;">*</span>tableview;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, retain<span style="color: #002200;">&#41;</span> IBOutlet UIDatePicker <span style="color: #002200;">*</span>datePicker;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, retain<span style="color: #002200;">&#41;</span> IBOutlet UITextField <span style="color: #002200;">*</span>eventText;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>IBAction<span style="color: #002200;">&#41;</span> scheduleAlarm<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span> sender;
&nbsp;
<span style="color: #a61390;">@end</span></pre></td></tr></table></div>

<p style="text-align: left;">Seems clear enough.  We have 3 UI elements that we care about and one action.  One thing to note is, your class should implement the UITableViewDelegate and UITableViewDataSource protocols.  This is because we will be displaying a tableview containing all of the scheduled alarms.</p>
<p style="text-align: left;">Now, do all of the necessary steps in your .m file.  This includes memory management for the IBOutlets and creating a dummy method for the scheduleAlarm IBAction.  Your .m file should look something like this.  Note: I have omitted import statements because my syntax highlighter wasn&#8217;t digging them.</p>
<p style="text-align: left;">

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@implementation</span> NotifierViewController
&nbsp;
<span style="color: #a61390;">@synthesize</span> datePicker,tableview, eventText;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>IBAction<span style="color: #002200;">&#41;</span> scheduleAlarm<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span> sender <span style="color: #002200;">&#123;</span>
&nbsp;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><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>
	datePicker <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
	tableview <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
	eventText <span style="color: #002200;">=</span> <span style="color: #a61390;">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>

<p style="text-align: left;">Now it&#8217;s time to build our interface.  Open Interface builder and construct an interface like this.</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2010/07/Screen-shot-2010-07-29-at-10.52.35-AM1.png"><img class="alignnone size-full wp-image-2174" title="Screen shot 2010-07-29 at 10.52.35 AM" src="/wp-content/uploads/2010/07/Screen-shot-2010-07-29-at-10.52.35-AM1.png" alt="" width="400" height="582" /></a></p>
<p style="text-align: left;">If you want my super sweet green button image, here it is:</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2010/07/button-green1.png"><img class="size-full wp-image-2175 aligncenter" title="button-green" src="/wp-content/uploads/2010/07/button-green1.png" alt="" width="292" height="47" /></a></p>
<p style="text-align: left;">After creating the interface, make sure you hook up all of the UI components to their corresponding IBOutlets and hook up the touchUpInside: method of the button the your scheduleAlarm: IBAction.  For more info on hooking up IBOutlets, check out <a href="/2008/07/30/iphone-programming-tutorial-connecting-code-to-an-interface-builder-view/">this tutorial</a>.</p>
<h2>3. Implement UITableViewDelegate and UITableViewDataSource Delegate methods to List Currently Scheduled Local Notifications</h2>
<p style="text-align: left;">It may seem weird to implement the code to display the notifications before the code that creates them, however I like this approach better.  This way, once we schedule the notifications, they automagically appear in our table.  Add the following code to your .m file.</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>NSInteger<span style="color: #002200;">&#41;</span>numberOfSectionsInTableView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UITableView <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>tableView <span style="color: #002200;">&#123;</span>
    <span style="color: #11740a; font-style: italic;">// We only have one section</span>
    <span style="color: #a61390;">return</span> <span style="color: #2400d9;">1</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span>tableView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UITableView <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>tableView numberOfRowsInSection<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span>section <span style="color: #002200;">&#123;</span>
    <span style="color: #11740a; font-style: italic;">// Return the number of notifications</span>
    <span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIApplication sharedApplication<span style="color: #002200;">&#93;</span> scheduledLocalNotifications<span style="color: #002200;">&#93;</span> count<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>UITableViewCell <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>tableView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UITableView <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>tableView cellForRowAtIndexPath<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSIndexPath</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>indexPath <span style="color: #002200;">&#123;</span>
&nbsp;
    <span style="color: #a61390;">static</span> <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>CellIdentifier <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Cell&quot;</span>;
&nbsp;
    UITableViewCell <span style="color: #002200;">*</span>cell <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>tableView dequeueReusableCellWithIdentifier<span style="color: #002200;">:</span>CellIdentifier<span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>cell <span style="color: #002200;">==</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        cell <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UITableViewCell alloc<span style="color: #002200;">&#93;</span> initWithStyle<span style="color: #002200;">:</span>UITableViewCellStyleSubtitle reuseIdentifier<span style="color: #002200;">:</span>CellIdentifier<span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Get list of local notifications</span>
    <span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>notificationArray <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIApplication sharedApplication<span style="color: #002200;">&#93;</span> scheduledLocalNotifications<span style="color: #002200;">&#93;</span>;
    UILocalNotification <span style="color: #002200;">*</span>notif <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>notificationArray objectAtIndex<span style="color: #002200;">:</span>indexPath.row<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Display notification info</span>
    <span style="color: #002200;">&#91;</span>cell.textLabel setText<span style="color: #002200;">:</span>notif.alertBody<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>cell.detailTextLabel setText<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>notif.fireDate description<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #a61390;">return</span> cell;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p style="text-align: left;">OK, finally some &#8220;real&#8221; code.  Most of this code should seem pretty straight forward.  If not, <a href="/2008/08/08/iphone-programming-tutorial-populating-uitableview-with-an-nsarray/">check out this tutorial</a> on UITableViews.</p>
<p style="text-align: left;">So, the new code here is dealing with retrieving a list of scheduled notifications.  Calling the scheduledLocalNotifications method of UIApplication will return an NSArray of all notifications scheduled by the current app.  We just index into this array and grab each notification.</p>
<p style="text-align: left;">Finally, we are displaying the alertBody (text that displays when the notification fires) and the fireDate (date and time when the notification will display) in the tableview cell.</p>
<h2>4. Scheduling Notifications</h2>
<p>And now for the moment you&#8217;ve been waiting for&#8230; OK, probably not, but definitely the most exciting (least boring) part of this tutorial.  Let&#8217;s implement that scheduleAlarm: IBAction that you framed out earlier.  Update your .m file to contain the following code.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>IBAction<span style="color: #002200;">&#41;</span> scheduleAlarm<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span> sender <span style="color: #002200;">&#123;</span>
    <span style="color: #002200;">&#91;</span>eventText resignFirstResponder<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #400080;">NSCalendar</span> <span style="color: #002200;">*</span>calendar <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSCalendar</span> autoupdatingCurrentCalendar<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Get the current date</span>
    <span style="color: #400080;">NSDate</span> <span style="color: #002200;">*</span>pickerDate <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self.datePicker date<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Break the date up into components</span>
    <span style="color: #400080;">NSDateComponents</span> <span style="color: #002200;">*</span>dateComponents <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>calendar components<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span> NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit <span style="color: #002200;">&#41;</span>
												   fromDate<span style="color: #002200;">:</span>pickerDate<span style="color: #002200;">&#93;</span>;
    <span style="color: #400080;">NSDateComponents</span> <span style="color: #002200;">*</span>timeComponents <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>calendar components<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span> NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit <span style="color: #002200;">&#41;</span>
												   fromDate<span style="color: #002200;">:</span>pickerDate<span style="color: #002200;">&#93;</span>;
    <span style="color: #11740a; font-style: italic;">// Set up the fire time</span>
    <span style="color: #400080;">NSDateComponents</span> <span style="color: #002200;">*</span>dateComps <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDateComponents</span> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>dateComps setDay<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>dateComponents day<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>dateComps setMonth<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>dateComponents month<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>dateComps setYear<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>dateComponents year<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>dateComps setHour<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>timeComponents hour<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #11740a; font-style: italic;">// Notification will fire in one minute</span>
    <span style="color: #002200;">&#91;</span>dateComps setMinute<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>timeComponents minute<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>dateComps setSecond<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>timeComponents second<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #400080;">NSDate</span> <span style="color: #002200;">*</span>itemDate <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>calendar dateFromComponents<span style="color: #002200;">:</span>dateComps<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>dateComps release<span style="color: #002200;">&#93;</span>;
&nbsp;
    UILocalNotification <span style="color: #002200;">*</span>localNotif <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UILocalNotification alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>localNotif <span style="color: #002200;">==</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span>
        <span style="color: #a61390;">return</span>;
    localNotif.fireDate <span style="color: #002200;">=</span> itemDate;
    localNotif.timeZone <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSTimeZone</span> defaultTimeZone<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Notification details</span>
    localNotif.alertBody <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>eventText text<span style="color: #002200;">&#93;</span>;
	<span style="color: #11740a; font-style: italic;">// Set the action button</span>
    localNotif.alertAction <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;View&quot;</span>;
&nbsp;
    localNotif.soundName <span style="color: #002200;">=</span> UILocalNotificationDefaultSoundName;
    localNotif.applicationIconBadgeNumber <span style="color: #002200;">=</span> <span style="color: #2400d9;">1</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Specify custom data for the notification</span>
    <span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span>infoDict <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDictionary</span> dictionaryWithObject<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;someValue&quot;</span> forKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;someKey&quot;</span><span style="color: #002200;">&#93;</span>;
    localNotif.userInfo <span style="color: #002200;">=</span> infoDict;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Schedule the notification</span>
    <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIApplication sharedApplication<span style="color: #002200;">&#93;</span> scheduleLocalNotification<span style="color: #002200;">:</span>localNotif<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>localNotif release<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #002200;">&#91;</span>self.tableview reloadData<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>So, most of the explanation is in the comments.  I&#8217;ll talk you through some of the less obvious stuff.  The first tricky area is dealing with the NSCalendar.  We just use the NSCalendar object to break up the date into components.  <strong>Note</strong>: This demo does not require that we break the date up into components.  You could have just as easily fed the date from the date picker into the notification fireDate.  The reason that I&#8217;m showing you how to break it down is, you may have some sort of custom date logic to work with and this makes things much easier in the future.</p>
<p>Another important bit of code is where we set the alertBody or the notification.  In this example we set it to the text that the user entered into the text field.  You can set this to whatever you like.</p>
<p>The other thing I want to mention is the infoDict in the code.  This dictionary is your chance to associate some additional information with the alert.  For example, if you are using this alert in a game like We Rule to notify you when a crop is ready.  You might want to set a key and value that contains the id of the crop that has completed.  For now, we just set some arbitrary values and you can ignore them if you like.</p>
<p>After actually scheduling the notification, we just reload the tableview to get it to display immediately.</p>
<h2>5. Handling Notifications After They Fire</h2>
<p>The last piece of this puzzle is determining what to do when a notification fires.  Fortunately, this step is very easy and handled inside of the appDelegate.  When a notification fires, there are one of two situations. 1. The app is running and 2. The app is not running (or running in the &#8220;background&#8221;) .</p>
<p>Open up your app delegate .m file and add the following code.</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;
    <span style="color: #11740a; font-style: italic;">// Override point for customization after application launch.</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;
    application.applicationIconBadgeNumber <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Handle launching from a notification</span>
    UILocalNotification <span style="color: #002200;">*</span>localNotif <span style="color: #002200;">=</span>
    <span style="color: #002200;">&#91;</span>launchOptions objectForKey<span style="color: #002200;">:</span>UIApplicationLaunchOptionsLocalNotificationKey<span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>localNotif<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Recieved Notification %@&quot;</span>,localNotif<span style="color: #002200;">&#41;</span>;
    <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>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</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>app didReceiveLocalNotification<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UILocalNotification <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>notif <span style="color: #002200;">&#123;</span>
    <span style="color: #11740a; font-style: italic;">// Handle the notificaton when the app is running</span>
    NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Recieved Notification %@&quot;</span>,notif<span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>The first thing we see here is the application badge number is getting set to 0.  Whenever a notification fires, it will increase the badge count on the application.  Next, we handle the case when the application launches from a notification.   This happens when the user presses the view button on the notification.  For now, we just NSLog the data, but you should handle the notification how you see fit for your app.</p>
<p>Finally, we implement the didReceiveLocalNotification method.  This method is required if you want to handle notifications at all in your app.  You will see this method fire when the app is running and you receive a local notification.  When the app is running, you will not see the UIAlertView show up with the notification data.</p>
<p>And there you have it!  The complete lifecycle of a local notification.  You may download the source for this tutorial below.  If you have any questions, feel free to post them in the comments section or <a href="http://twitter.com/brandontreb">write them to me on Twitter</a>.</p>
<p><a href="/wp-content/uploads/2010/07/Notifier1.zip">Notifier Source Code</a></p>
<p style="text-align: left;">
]]></content:encoded>
			<wfw:commentRss>http://icodeblog.com/2010/07/29/iphone-programming-tutorial-local-notifications/feed/</wfw:commentRss>
		<slash:comments>84</slash:comments>
		</item>
		<item>
		<title>How To Integrate Google Analytics Tracking Into Your Apps In 7 Minutes</title>
		<link>http://icodeblog.com/2010/04/22/how-to-integrate-google-analytics-tracking-into-your-apps-in-7-minutes/</link>
		<comments>http://icodeblog.com/2010/04/22/how-to-integrate-google-analytics-tracking-into-your-apps-in-7-minutes/#comments</comments>
		<pubDate>Thu, 22 Apr 2010 15:02:06 +0000</pubDate>
		<dc:creator><![CDATA[brandontreb]]></dc:creator>
				<category><![CDATA[Snippets]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[google analytics]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[SQLite]]></category>
		<category><![CDATA[stats]]></category>
		<category><![CDATA[tracking]]></category>

		<guid isPermaLink="false">http://icodeblog.com/?p=1906</guid>
		<description><![CDATA[So, why would you want to integrate Google Analytics into your iPhone application.  Duh, for the same reasons you would integrate it into your site.  Google has extended their killer analytics platform to include mobile devices including the iPhone and Android devices.

The analytics API gives you some very powerful options to get as nitty gritty as you would like in your application tracking.]]></description>
				<content:encoded><![CDATA[<p>Ok, maybe that title is +- 3 minutes depending on how efficient you are ;).</p>
<h4>What?</h4>
<p>So, why would you want to integrate Google Analytics into your iPhone application.  Duh, for the same reasons you would integrate it into your site.  Google has extended their killer analytics platform to include mobile devices including the iPhone and Android devices.</p>
<p>The analytics API gives you some very powerful options to get as nitty gritty as you would like in your application tracking.</p>
<h4>Why?</h4>
<p>Uhh, because stats are freakin sweet.  Even if you don&#8217;t have a million+ downloads of your app, it is still interesting to know how your users are using your application.  Being able to track every single action made by a user is priceless when thinking about the direction of your app including what features to add, improve, or scrap.</p>
<p>Imaging spending all of your time on some complex feature to find out that only 1% of your users even care about that feature.  How could you know that only 1% of your users actually care about that feature unless you are doing some sort of tracking.</p>
<p>So before you add that super duper all in one end all be all 1337 feature to your app, consider reading the rest of this tutorial and add stat tracking first.  It is quick and painless and I promise that it will be totally worth it.</p>
<h4>Configuring your Google Analytics Account</h4>
<p>Google Analytics is a free server provided by Google.  If you dont already have an account, you can sign up for one with your existing Gmail account.  <a href="http://google.com/analytics">http://google.com/analytics</a>. Sign up and log in.</p>
<p>The first thing you will want to do is Add a new <strong class="Apple-style-span" style="font-weight: bold;">Website Profile.</strong></p>
<p><strong class="Apple-style-span" style="font-weight: bold;"><a href="/wp-content/uploads/2010/04/Screen-shot-2010-04-06-at-10.45.36-AM1.png"><img class="alignnone size-full wp-image-1907" title="Screen shot 2010-04-06 at 10.45.36 AM" src="/wp-content/uploads/2010/04/Screen-shot-2010-04-06-at-10.45.36-AM1.png" alt="" width="386" height="71" /></a></strong></p>
<p>On the next page:</p>
<ul>
<li>Select Add a Profile for a new domain</li>
<li>Enter in <em class="Apple-style-span" style="font-style: italic;">some</em> URL &#8211; This doesn&#8217;t really matter since google doesn&#8217;t use it to identify your app.  A good convention is iphone.yoursite.com or appname.yoursite.com so that you know these stats are for your iPhone app.</li>
<li>Select your country and timezone</li>
<li>Click Finish</li>
</ul>
<p><a href="/wp-content/uploads/2010/04/Screen-shot-2010-04-06-at-10.47.20-AM1.png"><img class="alignnone size-full wp-image-1908" title="Screen shot 2010-04-06 at 10.47.20 AM" src="/wp-content/uploads/2010/04/Screen-shot-2010-04-06-at-10.47.20-AM1.png" alt="" width="689" height="395" /></a></p>
<p>Pretty simple&#8230; On the next screen, make sure you copy the <strong class="Apple-style-span" style="font-weight: bold;">Web Property ID</strong>. This number (begins with UA) is what we will be using to uniquely identify your application.</p>
<p><a href="/wp-content/uploads/2010/04/Screen-shot-2010-04-06-at-10.50.25-AM1.png"><img class="alignnone size-full wp-image-1909" title="Screen shot 2010-04-06 at 10.50.25 AM" src="/wp-content/uploads/2010/04/Screen-shot-2010-04-06-at-10.50.25-AM1.png" alt="" width="266" height="25" /></a></p>
<p>That&#8217;s all for the web side of things!  Now on to the iPhone part.</p>
<h4>Download the Google Analytics Library and Add It To Your Application</h4>
<p>This could not be easier.  Head on Over to <a href="http://code.google.com/apis/analytics/docs/tracking/mobileAppsTracking.html">http://code.google.com/apis/analytics/docs/tracking/mobileAppsTracking.html</a> . They explain much of the steps  so you could read the rest there, but I will lay them out in detail here with screesnhots.  Download the <a href="http://dl.google.com/gaformobileapps/GoogleAnalyticsIphone_0.7.tar.gz">Analytics SDK for iPhone</a> and extract it.</p>
<p>There are 2 files that are important.  Drag both files from the Library folder into your XCode project.  These files are GANTracker.h and libGoogleAnalytics.a.</p>
<p><a href="/wp-content/uploads/2010/04/Screen-shot-2010-04-22-at-7.29.49-AM1.png"><img class="alignnone size-full wp-image-1971" title="Screen shot 2010-04-22 at 7.29.49 AM" src="/wp-content/uploads/2010/04/Screen-shot-2010-04-22-at-7.29.49-AM1.png" alt="" width="397" height="70" /></a></p>
<p>When XCode pops up the confirmation box, make sure that you check the box that says &#8220;Copy items into destination group&#8217;s folder (if needed)&#8221;.  This will ensure that the code gets place in your project directory.</p>
<h4><strong class="Apple-style-span" style="font-weight: bold;">Include CFNetwork and Link Against the sqlite framework</strong></h4>
<p>The google analytics framework requires the use of CFNetwork (for interent connection detection) and the sqlite framework (most likely to store events when the user does not have internet).</p>
<p>Let&#8217;s start by adding CFNetwork to the project. Right click on the frameworks folder and select Add -&gt; Existing Frameworks.  Next, select CFNetwork.framework from the list and click Add.</p>
<p><a href="/wp-content/uploads/2010/04/Screen-shot-2010-04-22-at-8.07.14-AM1.png"><img class="alignnone size-full wp-image-1972" title="Screen shot 2010-04-22 at 8.07.14 AM" src="/wp-content/uploads/2010/04/Screen-shot-2010-04-22-at-8.07.14-AM1.png" alt="" width="352" height="548" /></a></p>
<p>Now we need to link the project against libsqlite3.0.dylib.  To do this Click Project -&gt; Edit Active Target &#8220;&lt;project name&gt;&#8221; where &lt;project name&gt; is the name of your XCode project.</p>
<p>Next, click the + button at the bottom to bring up the library picker. Select libsqlite3.0.dylib and click Add</p>
<p><a href="/wp-content/uploads/2010/04/Screen-shot-2010-04-22-at-8.11.23-AM1.png"><img class="alignnone size-full wp-image-1974" title="Screen shot 2010-04-22 at 8.11.23 AM" src="/wp-content/uploads/2010/04/Screen-shot-2010-04-22-at-8.11.23-AM1.png" alt="" width="510" height="442" /></a></p>
<h4>Including and Initializing the Tracker</h4>
<p>Since the Analytics tracker is a singleton class, we only have to initialize it once and the rest of the application can use the same instance.</p>
<p>Let&#8217;s start by opening up the app delegate and adding the code to import the Google Analytics framework.</p>

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

<p>Now you need to initialize the tracker&#8230; Make sure you have the UA number handy that you received from Google earlier.  Add the following code to you applicationDidFinishLaunching method.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>GANTracker sharedTracker<span style="color: #002200;">&#93;</span> startTrackerWithAccountID<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;UA-15609865-3&quot;</span>
				dispatchPeriod<span style="color: #002200;">:</span><span style="color: #2400d9;">10</span>
				delegate<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

<p>Make sure you replace my UA number with yours.  The dispatchPeriod variable tells the tracker how often to report back to Google.  Google suggests using 10 seconds in their example so we&#8217;ll stick to that.</p>
<h4>Tracking Pageviews and Custom Events</h4>
<p>Google gives you 2 different ways to track how your users use the application.  The first way is via pageviews.  Pageviews work just like they do in the browser as if the user navigated to a new page in your website.  Some examples of when to use pageview are when you have a tab interface or a flipside view controller.  I would not suggest using a pageview every time the user performs an action such as click a button.</p>
<p>The second way is via custom events.  These are really cool.  With custom events, you can easily see HOW your users are using your application.  Examples of events are button clicks, typing in a textbox, submitting high scores, etc&#8230;</p>
<p>We will see an example of implementing each of these methods and take a look at how they appear inside of the Google Analytics website.</p>
<p><strong>Pageview method</strong></p>
<p>Here is an example of a pageview when the user first launches the app.  Put this code inside of your applicationDidFinishLaunching method.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSError</span> <span style="color: #002200;">*</span>error;
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>GANTracker sharedTracker<span style="color: #002200;">&#93;</span> trackPageview<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;/app_launched&quot;</span>
                                        withError<span style="color: #002200;">:&amp;</span>amp;error<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
     <span style="color: #11740a; font-style: italic;">// Handle error here</span>
   <span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>Note that the &#8220;app_launched&#8221; is essentially our page name similar to index.html or aboutus.php.  You can add this code anywhere in you app that you want to add a page view.  Make sure you include the GANTrack.h file and change the pagename for each new location.</p>
<p><strong>Event method</strong><br />
Here is an example of an event that gets fired when the user presses a Save button.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>IBAction<span style="color: #002200;">&#41;</span> saveTheWorld<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span> sender
<span style="color: #002200;">&#123;</span>
	<span style="color: #400080;">NSError</span> <span style="color: #002200;">*</span>error;
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>GANTracker sharedTracker<span style="color: #002200;">&#93;</span> trackEvent<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;button_click&quot;</span>
				             action<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;save_the_world&quot;</span>
					      label<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;my_label&quot;</span>
					      value<span style="color: #002200;">:-</span><span style="color: #2400d9;">1</span>
					  withError<span style="color: #002200;">:&amp;</span>amp;error<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
		<span style="color: #11740a; font-style: italic;">// Handle error here</span>
	<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>What&#8217;s really cool about event tracking is you have 3 levels of customization.  You have the category, the action, and the label.  How you organize it is up to you, but make sure the grouping makes sense.  In the example above, I will use the category &#8220;button_click&#8221; every time a user clicks any button.  The action will determine which button was clicked (in this case, it&#8217;s &#8220;save_the_world&#8221;).  Finally, the label is just another level.  I&#8217;d suggest using the UDID of the users device.</p>
<p>Now that we have implemented our code, let&#8217;s take a look inside of our Google Analytics account and see what the stats look like.</p>
<p><a href="/wp-content/uploads/2010/04/Screen-shot-2010-04-22-at-8.48.41-AM1.png"><img class="alignnone size-full wp-image-1977" title="Screen shot 2010-04-22 at 8.48.41 AM" src="/wp-content/uploads/2010/04/Screen-shot-2010-04-22-at-8.48.41-AM1.png" alt="" width="639" height="299" /></a></p>
<p>This is just some of our sample data from another app.  Notice the interface is exactly the same as it is when you are tracking website statistics.  It shows you unique visits as well as pageviews.  You get all of the advanced reporting too including time spent on each &#8220;page&#8221;.</p>
<p>Now we take a look at events.  To find the actions click Content -&gt; Event Tracking inside of your Google Analytics Page.</p>
<p><a href="/wp-content/uploads/2010/04/Screen-shot-2010-04-22-at-8.53.30-AM1.png"><img class="alignnone size-full wp-image-1978" title="Screen shot 2010-04-22 at 8.53.30 AM" src="/wp-content/uploads/2010/04/Screen-shot-2010-04-22-at-8.53.30-AM1.png" alt="" width="608" height="339" /></a></p>
<p>In the above screenshot, we see tracking for an event called &#8220;clap&#8221;.  It shows us the number of times the users &#8220;clapped&#8221; within a given day.</p>
<h4>Conclusion</h4>
<p>One last thing I want to mention before concluding this tutorial. <strong>Google Analytics stats are 1 day behind</strong>.  What this means is, you won&#8217;t see your stats appear immediately.  So, before you comment here because you don&#8217;t see the stats appearing, please please please wait one day and check your stats again.  That is all <img src="/wp-includes/images/smilies/icon_smile.gif" alt=":)" class="wp-smiley" /></p>
<p>Be one of the cool kids, <a href="http://twitter.com/brandontreb">follow me on Twitter</a>.</p>
<p>Happy iCoding</p>
]]></content:encoded>
			<wfw:commentRss>http://icodeblog.com/2010/04/22/how-to-integrate-google-analytics-tracking-into-your-apps-in-7-minutes/feed/</wfw:commentRss>
		<slash:comments>38</slash:comments>
		</item>
		<item>
		<title>Creating a document centric iPhone/iPad application with own file format using ZipArchive</title>
		<link>http://icodeblog.com/2010/04/12/creating-a-document-centric-iphoneipad-application-with-own-file-format-using-ziparchive/</link>
		<comments>http://icodeblog.com/2010/04/12/creating-a-document-centric-iphoneipad-application-with-own-file-format-using-ziparchive/#comments</comments>
		<pubDate>Mon, 12 Apr 2010 13:00:48 +0000</pubDate>
		<dc:creator><![CDATA[Marin Todorov]]></dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[custom]]></category>
		<category><![CDATA[file format]]></category>
		<category><![CDATA[how to]]></category>
		<category><![CDATA[ipad]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[NSFileManager]]></category>
		<category><![CDATA[ZipArchive]]></category>

		<guid isPermaLink="false">http://icodeblog.com/?p=1941</guid>
		<description><![CDATA[If I need to predict one thing about where the App Store is heading to, now that the iPad has been released, I would say "It's going to be less about farting apps and much more about productivity apps" Yes - the iPad has already changed the game drastically - with an almost real life size keyboard, large beautiful screen and file sharing direct in iTunes you can achieve much more than before. But hey, iPhone OS 4.0 is just around the corner, and I bet one of those new features will be the same file sharing you get on the iPad.]]></description>
				<content:encoded><![CDATA[<p>If I need to predict one thing about where the App Store is heading to, now that the iPad has been released, I would say &#8220;It&#8217;s going to be less about farting apps and much more about productivity apps&#8221; Yes &#8211; the iPad has already changed the game drastically &#8211; with an almost real life size keyboard, large beautiful screen and file sharing direct in iTunes you can achieve much more than before. But hey, iPhone OS 4.0 is just around the corner, and I bet one of those new features will be the same file sharing you get on the iPad.</p>
<h3>The problem</h3>
<p>Assuming you are already familiar with Objective-C and Cocoa Touch, today I&#8217;ll be discussing how to create a productivity application which can read and write multimedia files &#8211; custom format files which will be a mixture of image and text data. If you look at the most of the Apple&#8217;s productivity applications you&#8217;ll notice they all use bundles as their output format. For those not familiar with the concept &#8211; the bundle is an actual file directory, which then holds different files, but to the user it&#8217;s presented as a single file &#8211; easier to copy around and in general to work with. I am going to be doing the same today by making my example application create different text and image files and then zip them together in a single file &#8211; this way the user could then copy this single file over trough iTunes via the file sharing feature.</p>
<h3>ZipArchive overview</h3>
<p>The star of today&#8217;s article is ZipArchive &#8211; the Objective-C library I&#8217;m going to use for compressing and uncompressing my custom zip files. It is completely free and you can download it from : <a title="ZipArchive download" href="http://code.google.com/p/ziparchive/" target="_blank">http://code.google.com/p/ziparchive/</a></p>
<p>To use ZipArchive I download the library and add it to my Xcode project.</p>
<p><a href="/wp-content/uploads/2010/04/zipFilelist1.png"><img class="alignnone size-full wp-image-1945" src="/wp-content/uploads/2010/04/zipFilelist1.png" alt="" width="248" height="221" /></a></p>
<p>Now that I added the ZipArchive sources I am almost ready to create and extract zip archives. In fact ZipArchive uses the libz framework, so I need to add this framework to my XCode project too. Add-&gt;Existing Frameworks and I choose &#8220;libz.1.2.3.dylib&#8221;</p>
<p><a href="/wp-content/uploads/2010/04/addZlyb1.jpeg"><img class="alignnone size-medium wp-image-1942" src="/wp-content/uploads/2010/04/addZlyb-300x250.jpg" alt="" width="300" height="250" /></a></p>
<p>ZipArchive is very straightforward to use, also because it offers just a handful of methods, let&#8217;s have a look together at what&#8217;s inside:</p>
<p>Creating a ZipArchive instance is super simple.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &quot;ZipArchive.h&quot;</span>
&nbsp;
ZipArchive<span style="color: #002200;">*</span> zip <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>ZipArchive alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

<h3>Create zip archives</h3>
<p>Call CreateZipFile2: to create an empty zip archive file, or call CreateZipFile:Password: to create an empty zip archive which is password protected (the latter makes creating encrypted files really easy). <strong>NB:</strong> <em>If your application is creating password protected zip files in general it uses encryption, so you would need to tick the encryption checkbox when you submit it to the App Store.</em></p>
<p>Once you create a zip archive you can add as many files as you like by calling addFileToZip:newname: and when you are done call CloseZipFile2. Or as they say &#8220;a line of code is worth thousand words&#8221;:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;">ZipArchive <span style="color: #002200;">*</span>zip <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>ZipArchive alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #002200;">&#91;</span>zip CreateZipFile2<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;archive.zip&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #002200;">&#91;</span>zip addFileToZip<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;photo1.png&quot;</span> newname<span style="color: #002200;">:</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;photo1.png&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #002200;">&#91;</span>zip addFileToZip<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;photo2.png&quot;</span> newname<span style="color: #002200;">:</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;photo2.png&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #002200;">&#91;</span>zip addFileToZip<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;../../test/IMG_0001.png&quot;</span> newname<span style="color: #002200;">:</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;photo3.png&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #002200;">&#91;</span>zip CloseZipFile2<span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

<p>Now I really don&#8217;t think creating a zip archive gets any simpler than that. All of those methods return a boolean result, which you can check to see if the operation was successful, so you can implement also your error handling code along. To create a password protected file call:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">&#91;</span>zip CreateZipFile2<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;archive.zip&quot;</span> Password<span style="color: #002200;">:</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;plaintextpassword&quot;</span><span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

<p>(<em>I should really admit that ZipArchive&#8217;s the naming convention is strange: some names are CamelCase: , some camelCase:, some camel:Case: , some camelCase:case:, and sometimes a capital in the middle of the word &#8220;overWrite&#8221; &#8211; and this is a class with only 8 methods :</em>)</p>
<h3>Extracting zip archives</h3>
<p>Extracting files from a zip archive is as simple as creating one. You open an archive using UnzipOpenFile: or UnzipOpenFile:Password: Now pay attention to the result of that operation- if the file exists, but UnzipOpenFile: returns NO, it might mean the archive is password protected. If the archive file has been successfully opened it means ZipArchive can read the contents and you can proceed to extracting the files to a destination of your choice by using UnzipFileTo:overWrite: If you pass YES as the second argument, the extracting operation will overwrite files at the target location.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;">ZipArchive <span style="color: #002200;">*</span>zip <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>ZipArchive alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span>zip UnzipOpenFile<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;archive.zip&quot;</span><span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
&nbsp;
<span style="color: #002200;">&#91;</span>zip UnzipFileTo<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;tempFolder&quot;</span> overWrite<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">&#91;</span>zip UnzipCloseFile<span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

<p>To gain a bit more control over what ZipArchive does you can set a class of yours as a delegate, here is the ZipArchiveDelegate protocol:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@protocol</span> ZipArchiveDelegate <span style="color: #002200;">&amp;</span>lt;NSObject<span style="color: #002200;">&amp;</span>gt;
&nbsp;
@optional
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> ErrorMessage<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> msg;
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span> OverWriteOperation<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> file;
&nbsp;
<span style="color: #a61390;">@end</span></pre></td></tr></table></div>

<div>So as the method names suggest if you want to get more information about the errors happening let your delegate implement ErrorMessage: and if you want to be more flexible which files gets overwritten during archive extraction implement OverWriteOperation: (return YES to overwrite the given file)</div>
<p>Now that you know everything there is to know about ZipArchive, I can start with my super duper productivity application &#8230;</p>
<h3>Creating simple custom file format with ZipArchive</h3>
<p>I&#8217;ll first create a class called CustomFile, which will be my data model &#8211; it will be responsible for reading and writing data to the file system. I&#8217;ll need few ivars and properties for them:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span> filePath;
&nbsp;
<span style="color: #11740a; font-style: italic;">//the file contents</span>
&nbsp;
<span style="color: #400080;">NSString</span><span style="color: #002200;">*</span> title;
&nbsp;
<span style="color: #400080;">NSString</span><span style="color: #002200;">*</span> text;
&nbsp;
UIImage<span style="color: #002200;">*</span> photo;</pre></td></tr></table></div>

<p>In filePath I&#8217;ll keep the absolute path to the location the file will be written to or red from. The rest of the ivars will be my file contents &#8211; 2 texts and 1 image. Now let me explain what this custom file format will be about &#8211; I will want to store in my files short articles &#8211; much similar to blog posts &#8211; my files will hold the title, the full text and a photo (if attached) of an article.</p>
<p>Some of you will ask <strong>why</strong> do I need a custom format for my files &#8230; can&#8217;t I just save a text file with the text and the image somewhere on my disk and then save their names in a sqlite database and done ? Nope, think trends, think iPad / iPhone 4.0 and think file sharing: as I said things are moving beyond apps which sole purpose is to amuse their users for about 5 to 10 minutes. If my app saves separate files of text and images, when the user wants to copy them from his iPad to his wife&#8217;s iPad he might miss one of those files and that will ruin the integrity of his document&#8230; So let&#8217;s look further what I have in mind:</p>
<h3>My model&#8217;s initializer:</h3>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>initWithFilePath<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>initPath
&nbsp;
<span style="color: #002200;">&#123;</span>
&nbsp;
self <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>super init<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>self <span style="color: #002200;">!=</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">//set the file path</span>
&nbsp;
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>initPath <span style="color: #002200;">!=</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
&nbsp;
self.filePath <span style="color: #002200;">=</span> initPath;
&nbsp;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">return</span> self;
&nbsp;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>Nothing special, just making sure every instance of the model is related to a file path. So since all the file contents are also class properties, I can use them to  fill in my file&#8217;s instance with content and then I will need a save method to save to the file system. And what I&#8217;d need is create a temp folder, save all my data as files there, and them zip&#8217;em! If you have a look in the code below, now that you know how to use ZipArchive the code is actually very straightforward:</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>saveFile
&nbsp;
<span style="color: #002200;">&#123;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">//create a temp directory</span>
&nbsp;
<span style="color: #400080;">NSFileManager</span><span style="color: #002200;">*</span> fileManager <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSFileManager</span> defaultManager<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #400080;">NSString</span><span style="color: #002200;">*</span> documentsDir <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>filePath stringByDeletingLastPathComponent<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>tmpPath <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>documentsDir stringByAppendingPathComponent<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;tmp&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #002200;">&#91;</span>fileManager createDirectoryAtPath<span style="color: #002200;">:</span>tmpPath attributes<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
&nbsp;
ZipArchive <span style="color: #002200;">*</span>zip <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>ZipArchive alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #002200;">&#91;</span>zip CreateZipFile2<span style="color: #002200;">:</span>self.filePath<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">//save the texts</span>
&nbsp;
<span style="color: #400080;">NSDictionary</span><span style="color: #002200;">*</span> texts <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDictionary</span> dictionaryWithObjectsAndKeys<span style="color: #002200;">:</span>self.title,<span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;title&quot;</span>,self.text,<span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;text&quot;</span>,<span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #400080;">NSString</span><span style="color: #002200;">*</span> textsFile <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>tmpPath stringByAppendingPathComponent<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;texts.plist&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">//save the image and add them to the zip file</span>
&nbsp;
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>self.photo<span style="color: #002200;">!=</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
&nbsp;
<span style="color: #400080;">NSString</span><span style="color: #002200;">*</span> photoFile <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>tmpPath stringByAppendingPathComponent<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;photo0.png&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #400080;">NSData</span> <span style="color: #002200;">*</span>imageData <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSData</span> dataWithData<span style="color: #002200;">:</span>UIImagePNGRepresentation<span style="color: #002200;">&#40;</span>self.photo<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #002200;">&#91;</span>imageData writeToFile<span style="color: #002200;">:</span>photoFile atomically<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #002200;">&#91;</span>zip addFileToZip<span style="color: #002200;">:</span>photoFile newname<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;photo0.png&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #002200;">&#91;</span>fileManager removeItemAtPath<span style="color: #002200;">:</span>photoFile error<span style="color: #002200;">:</span><span style="color: #a61390;">NULL</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">&#91;</span>texts writeToFile<span style="color: #002200;">:</span>textsFile atomically<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #002200;">&#91;</span>zip addFileToZip<span style="color: #002200;">:</span>textsFile newname<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;texts.plist&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #002200;">&#91;</span>fileManager removeItemAtPath<span style="color: #002200;">:</span>textsFile error<span style="color: #002200;">:</span><span style="color: #a61390;">NULL</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> <span style="color: #002200;">!</span><span style="color: #002200;">&#91;</span>zip CloseZipFile2<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span>
&nbsp;
<span style="color: #002200;">&#123;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// error handler here</span>
&nbsp;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">&#91;</span>fileManager removeItemAtPath<span style="color: #002200;">:</span>tmpPath error<span style="color: #002200;">:</span><span style="color: #a61390;">NULL</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #002200;">&#91;</span>zip release<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>I am just putting the texts in a NSDictionary and saving it as a plist file, and then saving the image as a separate PNG file.  Then I zip everything and remove all traces. Note how I create my temporary folder in the same location where the file is going to be saved (assuming it is indeed writable) Your own document centric application is probably much greater than putting together some texts and an image, but I&#8217;m sure this example already gives you the right direction.</p>
<p>Now that I have already my saving method, it&#8217;s very easy to put together also the one that reads the data from the file system.</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>loadFile
&nbsp;
<span style="color: #002200;">&#123;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">//create a temp directory</span>
&nbsp;
<span style="color: #400080;">NSFileManager</span><span style="color: #002200;">*</span> fileManager <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSFileManager</span> defaultManager<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #400080;">NSString</span><span style="color: #002200;">*</span> documentsDir <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>filePath stringByDeletingLastPathComponent<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>tmpPath <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>documentsDir stringByAppendingPathComponent<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;tmp&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #002200;">&#91;</span>fileManager createDirectoryAtPath<span style="color: #002200;">:</span>tmpPath attributes<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
&nbsp;
ZipArchive <span style="color: #002200;">*</span>zip <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>ZipArchive alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #a61390;">BOOL</span> result <span style="color: #002200;">=</span> <span style="color: #a61390;">NO</span>;
&nbsp;
<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>zip UnzipOpenFile<span style="color: #002200;">:</span>filePath<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">//zip file is there</span>
&nbsp;
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>zip UnzipFileTo<span style="color: #002200;">:</span>tmpPath overWrite<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">//unzipped successfully</span>
&nbsp;
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Archive unzip Success&quot;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
result<span style="color: #002200;">=</span> <span style="color: #a61390;">YES</span>;
&nbsp;
<span style="color: #002200;">&#125;</span> <span style="color: #a61390;">else</span> <span style="color: #002200;">&#123;</span>
&nbsp;
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Failure To Extract Archive, maybe password?&quot;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">&#125;</span> <span style="color: #a61390;">else</span>  <span style="color: #002200;">&#123;</span>
&nbsp;
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Failure To Open Archive&quot;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>To load the file contents I have very similar approach: I get the directory of the file and create a temp folder in the same place and then I unzip it inside. Spoiler alert: All my documents are going to be saved in the Documents folder which is always writable, so no worries whether this little temp folder of mine can be crated or not. I chose for the Documents folder as this is the folder which you application can share with your computer via iTunes (in OS 3.2+)</p>
<p>Then onwards is also very simple, now the contents are extracted just load them back into the class instance:</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>result<span style="color: #002200;">==</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
&nbsp;
<span style="color: #400080;">NSString</span><span style="color: #002200;">*</span> textFile <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>tmpPath stringByAppendingPathComponent<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;texts.plist&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #400080;">NSDictionary</span><span style="color: #002200;">*</span> texts <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDictionary</span> dictionaryWithContentsOfFile<span style="color: #002200;">:</span>textFile<span style="color: #002200;">&#93;</span>;
&nbsp;
self.title <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><span style="color: #002200;">&#91;</span>texts objectForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;title&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
self.text  <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><span style="color: #002200;">&#91;</span>texts objectForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;text&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #400080;">NSString</span><span style="color: #002200;">*</span> tmpPhotoPath <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>tmpPath stringByAppendingPathComponent<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;photo0.png&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span>fileManager fileExistsAtPath<span style="color: #002200;">:</span>tmpPhotoPath<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
&nbsp;
self.photo <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> dataWithContentsOfFile<span style="color: #002200;">:</span>tmpPhotoPath<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">//do cleanup</span>
&nbsp;
<span style="color: #002200;">&#91;</span>fileManager removeItemAtPath<span style="color: #002200;">:</span>tmpPath error<span style="color: #002200;">:</span><span style="color: #a61390;">NULL</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #002200;">&#91;</span>zip release<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #a61390;">return</span> result;
&nbsp;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>Now that I have my model working (a simple class to read and write my custom file format) I&#8217;ll create also the GUI of my example application &#8211; I assume you are already familiar with creating UITableControllers and binding GUI elements to your classes so I won&#8217;t go into details about those. I&#8217;ll just quickly make a resume of the app&#8217;s idea and let you dig into the source code, which is available for download at the end of the article.</p>
<p>I&#8217;ll create my main view controller to load the names of all files in my Documents directory which have extension &#8220;.mtt&#8221; &#8211; those are the files my application create and show them in a table:</p>
<p><a href="/wp-content/uploads/2010/04/DocumentsList1.png"><img class="alignnone size-medium wp-image-1949" src="/wp-content/uploads/2010/04/DocumentsList-170x300.png" alt="" width="170" height="300" /></a></p>
<p>My second view controller will be the application work area &#8211; a screen where you can load an image from your photo gallery and enter some texts (again if you are not familiar with the techniques to do all these, read a more introductiory article about iPhone programming)</p>
<p><a href="/wp-content/uploads/2010/04/EditorScreen1.png"><img class="alignnone size-medium wp-image-1950" src="/wp-content/uploads/2010/04/EditorScreen-166x300.png" alt="" width="166" height="300" /></a></p>
<p>So the idea of the demo app is simple, initially shows you a list of the available documents, if you click on one of them, the CustomFile class unarchives it and loads its contents into the second view controller, there you can change the contents and hit Save Document. If you want to create  a new document you just choose the &#8220;New Document &#8230;&#8221; item from the list and this will just load the details view controller empty, so you can edit and save. Just download the source and run the app to get a feeling of the app and make sure to check the source to see how ZipArchive is being used.</p>
<h3>Wrap up</h3>
<p>I hope this article has been useful &#8211; I showcased ZipArchive and also did put some ideas together on how to create more elaborate document centric applications. The custom file format can be developed a lot further and the interface for editing multimedia contents could be improved dramatically on the iPad&#8217;s big screen.</p>
<p>The full source code of the demo application you can <a href="/wp-content/uploads/2010/04/iPhoneCustomFileFormat1.zip">download here</a>.</p>
<p>If you have comments or questions, please write in the comments below or <a title="Icanzil on Twitter" href="http://www.twitter.com/icanzilb">ping me in twitter</a>!</p>
<p>I wish you happy iCoding !</p>
<p>Marin Todorov</p>
]]></content:encoded>
			<wfw:commentRss>http://icodeblog.com/2010/04/12/creating-a-document-centric-iphoneipad-application-with-own-file-format-using-ziparchive/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>iPhone Coding &#8211; Turbo Charging Your Apps With NSOperation</title>
		<link>http://icodeblog.com/2010/03/04/iphone-coding-turbo-charging-your-apps-with-nsoperation/</link>
		<comments>http://icodeblog.com/2010/03/04/iphone-coding-turbo-charging-your-apps-with-nsoperation/#comments</comments>
		<pubDate>Thu, 04 Mar 2010 19:28:18 +0000</pubDate>
		<dc:creator><![CDATA[brandontreb]]></dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Advanced]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[iPhone Coding]]></category>
		<category><![CDATA[nosoperationqueue]]></category>
		<category><![CDATA[nsinvocationoperation]]></category>
		<category><![CDATA[nsoperation]]></category>

		<guid isPermaLink="false">http://icodeblog.com/?p=1792</guid>
		<description><![CDATA[<p>So, let's face it, MANY applications in the app store are "Clunky".  They have jittery interfaces, poor scrolling performance, and the UI tends to lock up at times.  The reason? DOING ANYTHING OTHER THAN INTERFACE MANIPULATION IN THE MAIN APPLICATION THREAD!</p>

<p>What do I mean by this? Well, I am essentially talking about multithreading your application.  If you don't know what is meant by multithreading, I suggest you read up on it and return to this post.  Let's dig in and I'll give you an example of the problem.</p>]]></description>
				<content:encoded><![CDATA[<h4>Introduction</h4>
<p>So, let&#8217;s face it, MANY applications in the app store are &#8220;Clunky&#8221;.  They have jittery interfaces, poor scrolling performance, and the UI tends to lock up at times.  The reason? DOING ANYTHING OTHER THAN INTERFACE MANIPULATION IN THE MAIN APPLICATION THREAD!</p>
<p>What do I mean by this? Well, I am essentially talking about multithreading your application.  If you don&#8217;t know what is meant by multithreading, I suggest you <a href="http://en.wikipedia.org/wiki/Multithreading">read up on it</a> and return to this post OR don&#8217;t worry about it because you don&#8217;t need much threading knowledge for this tutorial.  Let&#8217;s dig in and I&#8217;ll give you an example of the problem.</p>
<h4>The Problem</h4>
<p>When you create an application, the iPhone spawns a new process containing the main thread of your application.  All of interface components are run inside of this thread (table views, tab bars, alerts, etc&#8230;).  At some point in your application, you will want to populate these views with data.  This data can be retrieved from the disk, the web, a database, etc&#8230; The problem is: How do you efficiently load this data into your interface while still allowing the user to have control of the application.</p>
<p>Many applications in the store simply &#8216;freeze&#8217; while their application data is being loaded.  This could be anywhere from a tenth of a second to much longer. Even the smallest amount of time is noticeable to the user.</p>
<p>Now, don&#8217;t get me wrong, I am not talking about applications that display  a loading message on the screen while the data populates.  In most cases, this is acceptable, but can not be done effectively unless the data is loaded in another thread besides the main one.</p>
<p>Here is a look at the application we will be creating today:</p>
<p><img class="alignnone size-full wp-image-1806" title="Screen shot 2010-03-04 at 12.15.47 PM" src="/wp-content/uploads/2010/03/Screen-shot-2010-03-04-at-12.15.47-PM1.png" alt="" width="414" height="770" /></p>
<p>Let&#8217;s take a look at the incorrect way to load data into a UITableView from data loaded from the web.   The example below reads a plist file from icodeblog.com containing 10,000 entries and populates a UITableView with those entries.  This happens when the user presses the &#8220;Load&#8221; button.</p>
<h3><span style="color: #ff0000;">Wrong (<a href="/wp-content/uploads/2010/03/NSOperationTableWRONG1.zip"><span style="color: #ff0000;">download this code here to see for yourself</span></a>)</span></h3>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@implementation</span> RootViewController
<span style="color: #a61390;">@synthesize</span> array;
&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: #11740a; font-style: italic;">/* Adding the button */</span>
    self.navigationItem.rightBarButtonItem <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIBarButtonItem alloc<span style="color: #002200;">&#93;</span> initWithTitle<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Load&quot;</span>
        style<span style="color: #002200;">:</span>UIBarButtonItemStyleDone
        target<span style="color: #002200;">:</span>self
        action<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>loadData<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">/* Initialize our array */</span>
    <span style="color: #400080;">NSMutableArray</span> <span style="color: #002200;">*</span>_array <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableArray</span> alloc<span style="color: #002200;">&#93;</span> initWithCapacity<span style="color: #002200;">:</span><span style="color: #2400d9;">10000</span><span style="color: #002200;">&#93;</span>;
    self.array <span style="color: #002200;">=</span> _array;
    <span style="color: #002200;">&#91;</span>_array release<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// Fires when the user presses the load button</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> loadData <span style="color: #002200;">&#123;</span>
&nbsp;
    <span style="color: #11740a; font-style: italic;">/* Grab web data */</span>
    <span style="color: #400080;">NSURL</span> <span style="color: #002200;">*</span>dataURL <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSURL</span> URLWithString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;http://icodeblog.com/samples/nsoperation/data.plist&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>tmp_array <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSArray</span> arrayWithContentsOfURL<span style="color: #002200;">:</span>dataURL<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">/* Populate our array with the web data */</span>
    <span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>str <span style="color: #a61390;">in</span> tmp_array<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #002200;">&#91;</span>self.array addObject<span style="color: #002200;">:</span>str<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
&nbsp;
    <span style="color: #11740a; font-style: italic;">/* reload the table */</span>
    <span style="color: #002200;">&#91;</span>self.tableView reloadData<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #6e371a;">#pragma mark Table view methods</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span>numberOfSectionsInTableView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UITableView <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>tableView <span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">return</span> <span style="color: #2400d9;">1</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span>tableView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UITableView <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>tableView numberOfRowsInSection<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span>section <span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span>self.array count<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>UITableViewCell <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>tableView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UITableView <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>tableView cellForRowAtIndexPath<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSIndexPath</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>indexPath <span style="color: #002200;">&#123;</span>
&nbsp;
    <span style="color: #a61390;">static</span> <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>CellIdentifier <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Cell&quot;</span>;
&nbsp;
    UITableViewCell <span style="color: #002200;">*</span>cell <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>tableView dequeueReusableCellWithIdentifier<span style="color: #002200;">:</span>CellIdentifier<span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>cell <span style="color: #002200;">==</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        cell <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UITableViewCell alloc<span style="color: #002200;">&#93;</span> initWithStyle<span style="color: #002200;">:</span>UITableViewCellStyleDefault
                reuseIdentifier<span style="color: #002200;">:</span>CellIdentifier<span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
&nbsp;
    <span style="color: #11740a; font-style: italic;">/* Display the text of the array */</span>
    <span style="color: #002200;">&#91;</span>cell.textLabel setText<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>self.array objectAtIndex<span style="color: #002200;">:</span>indexPath.row<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #a61390;">return</span> cell;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><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;">&#91;</span>array release<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>&#8220;Looks good to me&#8221;, you may say.  But that is incorrect.  If you run the code above, pressing the &#8220;Load&#8221; button will result in the interface &#8216;freezing&#8217; while the data is being retrieved from the web.  During that time, the user is unable to scroll or do anything since the main thread is off downloading data.</p>
<h4>About NSOperationQueue And NSOperation</h4>
<p>Before I show you the solution, I though I would bring you up to speed on NSOperation.</p>
<p>According to Apple&#8230;</p>
<blockquote><p>The <code>NSOperation</code> and <code>NSOperationQueue</code> classes alleviate much of the pain of multi-threading, allowing you to simply define your tasks, set any dependencies that exist, and fire them off. Each task, or <strong>operation</strong>, is represented by an instance of an <code>NSOperation</code> class; the <code>NSOperationQueue</code> class takes care of starting the operations, ensuring that they are run in the appropriate order, and accounting for any priorities that have been set.</p></blockquote>
<p>The way it works is, you create a new NSOperationQueue and add NSOperations to it.  The NSOperationQueue creates a new thread for each operation and runs them in the order they are added (or a specified order (advanced)).  It takes care of all of the autorelease pools and other garbage that gets confusing when doing multithreading and greatly simplifies the process.</p>
<p>Here is the process for using the NSOperationQueue.</p>
<ol>
<li>Instantiate a new NSOperationQueue object</li>
<li>Create an instance of your NSOperation</li>
<li>Add your operation to the queue</li>
<li>Release your operation</li>
</ol>
<p>There are a few ways to work with NSOperations.  Today, I will show you the simplest one: NSInvocationOperation.  NSInvocationOperation is a subclass of NSOperation which allows you to specify a target and selector that will run as an operation.</p>
<p>Here is an example of how to execute an NSInvocationOperation:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSOperationQueue</span> <span style="color: #002200;">*</span>queue <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSOperationQueue</span> new<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #400080;">NSInvocationOperation</span> <span style="color: #002200;">*</span>operation <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSInvocationOperation</span> alloc<span style="color: #002200;">&#93;</span> initWithTarget<span style="color: #002200;">:</span>self
    selector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>methodToCall<span style="color: #002200;">&#41;</span>
    object<span style="color: #002200;">:</span>objectToPassToMethod<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #002200;">&#91;</span>queue addOperation<span style="color: #002200;">:</span>operation<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>operation release<span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

<p>This will call the method &#8220;methodToCall&#8221; passing in the object &#8220;objectToPassToMethod&#8221; in a separate thread.  Let&#8217;s see how this can be added to our code above to make it run smoother.</p>
<h4>The Solution</h4>
<p>Here we still have a method being fired when the user presses the &#8220;Load&#8221; button, but instead of fetching the data, this method fires off an NSOperation to fetch the data.  Check out the updated code.</p>
<h3><span style="color: #00ff00;">Correct (<span style="color: #00ff00;"><a href="/wp-content/uploads/2010/03/NSOperationTable11.zip"><span style="color: #00ff00;">Download the source code here</span></a></span>)</span></h3>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@implementation</span> RootViewController
<span style="color: #a61390;">@synthesize</span> array;
&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;
    self.navigationItem.rightBarButtonItem <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIBarButtonItem alloc<span style="color: #002200;">&#93;</span> initWithTitle<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Load&quot;</span>
       style<span style="color: #002200;">:</span>UIBarButtonItemStyleDone
       target<span style="color: #002200;">:</span>self
       action<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>loadData<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #400080;">NSMutableArray</span> <span style="color: #002200;">*</span>_array <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableArray</span> alloc<span style="color: #002200;">&#93;</span> initWithCapacity<span style="color: #002200;">:</span><span style="color: #2400d9;">10000</span><span style="color: #002200;">&#93;</span>;
    self.array <span style="color: #002200;">=</span> _array;
    <span style="color: #002200;">&#91;</span>_array 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> loadData <span style="color: #002200;">&#123;</span>
&nbsp;
    <span style="color: #11740a; font-style: italic;">/* Operation Queue init (autorelease) */</span>
    <span style="color: #400080;">NSOperationQueue</span> <span style="color: #002200;">*</span>queue <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSOperationQueue</span> new<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">/* Create our NSInvocationOperation to call loadDataWithOperation, passing in nil */</span>
    <span style="color: #400080;">NSInvocationOperation</span> <span style="color: #002200;">*</span>operation <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSInvocationOperation</span> alloc<span style="color: #002200;">&#93;</span> initWithTarget<span style="color: #002200;">:</span>self
        selector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>loadDataWithOperation<span style="color: #002200;">&#41;</span>
        object<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">/* Add the operation to the queue */</span>
    <span style="color: #002200;">&#91;</span>queue addOperation<span style="color: #002200;">:</span>operation<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>operation 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> loadDataWithOperation <span style="color: #002200;">&#123;</span>
    <span style="color: #400080;">NSURL</span> <span style="color: #002200;">*</span>dataURL <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSURL</span> URLWithString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;http://icodeblog.com/samples/nsoperation/data.plist&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>tmp_array <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSArray</span> arrayWithContentsOfURL<span style="color: #002200;">:</span>dataURL<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>str <span style="color: #a61390;">in</span> tmp_array<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #002200;">&#91;</span>self.array addObject<span style="color: #002200;">:</span>str<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
&nbsp;
    <span style="color: #002200;">&#91;</span>self.tableView performSelectorOnMainThread<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>reloadData<span style="color: #002200;">&#41;</span> withObject<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span> waitUntilDone<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #6e371a;">#pragma mark Table view methods</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span>numberOfSectionsInTableView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UITableView <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>tableView <span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">return</span> <span style="color: #2400d9;">1</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span>tableView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UITableView <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>tableView numberOfRowsInSection<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span>section <span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span>self.array count<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>UITableViewCell <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>tableView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UITableView <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>tableView cellForRowAtIndexPath<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSIndexPath</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>indexPath <span style="color: #002200;">&#123;</span>
&nbsp;
    <span style="color: #a61390;">static</span> <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>CellIdentifier <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Cell&quot;</span>;
&nbsp;
    UITableViewCell <span style="color: #002200;">*</span>cell <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>tableView dequeueReusableCellWithIdentifier<span style="color: #002200;">:</span>CellIdentifier<span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>cell <span style="color: #002200;">==</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        cell <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UITableViewCell alloc<span style="color: #002200;">&#93;</span> initWithStyle<span style="color: #002200;">:</span>UITableViewCellStyleDefault reuseIdentifier<span style="color: #002200;">:</span>CellIdentifier<span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
&nbsp;
    <span style="color: #002200;">&#91;</span>cell.textLabel setText<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>self.array objectAtIndex<span style="color: #002200;">:</span>indexPath.row<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #a61390;">return</span> cell;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><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;">&#91;</span>array release<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>As you can see, we haven&#8217;t added much code here, but we have GREATLY improved the overall user experience.  So, what did I do exactly?</p>
<ol>
<li>Moved all of the processing (downloading) code from the loadData method to another method that could be run asynchronously</li>
<li>Created a new instance of NSOperationQueue by calling [NSOperationQueue new]</li>
<li>Created an NSInvocationOperation to call our method loadDataWithOperation</li>
<li>Added the operation to the queue</li>
<li>Released the operation</li>
<li>When the Data has been downloaded, we reload the table data in the main thread since it&#8217;s a UI manipulation</li>
</ol>
<p>One thing to note here is we never actually tell the operation to run.  This is handled automatically in the queue.   The queue will figure out the optimal time run the operation and do it for you.</p>
<p>Now that you have your downloading and processing in a separate thread, you are now free to add things such as a loading view.</p>
<p>I will be expanding on this tutorial in the coming week and showing you how to cache data and display old data to the user while the new is loading.  This is a popular technique used in many Twitter and News applications.</p>
<p>That concludes today&#8217;s tutorial.</p>
<p>Post questions in the comments or <a href="http://twitter.com/brandontreb">Ping me on Twitter</a>.</p>
<p>Happy iCoding!</p>
]]></content:encoded>
			<wfw:commentRss>http://icodeblog.com/2010/03/04/iphone-coding-turbo-charging-your-apps-with-nsoperation/feed/</wfw:commentRss>
		<slash:comments>65</slash:comments>
		</item>
		<item>
		<title>Introduction to MapKit in iPhone OS 3.0 Part 2</title>
		<link>http://icodeblog.com/2009/12/22/introduction-to-mapkit-in-iphone-os-3-0-part-2/</link>
		<comments>http://icodeblog.com/2009/12/22/introduction-to-mapkit-in-iphone-os-3-0-part-2/#comments</comments>
		<pubDate>Tue, 22 Dec 2009 18:53:56 +0000</pubDate>
		<dc:creator><![CDATA[Collin]]></dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Google Maps]]></category>
		<category><![CDATA[Intermediate]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[MapKit]]></category>
		<category><![CDATA[MKMapView]]></category>
		<category><![CDATA[MKReverseGeocoder]]></category>
		<category><![CDATA[MKReverseGeocoderDelegate]]></category>

		<guid isPermaLink="false">http://icodeblog.com/?p=1558</guid>
		<description><![CDATA[Introduction
Back in September I <a title="Introduction to MapKit in iPhone OS 3.0" href="/2009/12/21/introduction-to-mapkit-in-iphone-os-3-0/" target="_blank">posted a large post</a> going over all the components required to implement the MapKit in your application. The MapKit is a framework introduced in iPhone OS 3.0 and allows developers to easily take advantage of Google&#8217;s mapping technology. In my first post I go over how to present a map as well as annotate the map with custom badges to highlight points of interest. The MapKit also  ...]]></description>
				<content:encoded><![CDATA[<h1><span style="color: #ff6600;">Introduction</span></h1>
<p><span style="color: #000000;">Back in September I <a title="Introduction to MapKit in iPhone OS 3.0" href="/2009/12/21/introduction-to-mapkit-in-iphone-os-3-0/" target="_blank">posted a large post</a> going over all the components required to implement the MapKit in your application. The MapKit is a framework introduced in iPhone OS 3.0 and allows developers to easily take advantage of Google&#8217;s mapping technology. In my first post I go over how to present a map as well as annotate the map with custom badges to highlight points of interest. The MapKit also gives developers access to reverse geocoding services from Google which I will cover in this post.</span></p>
<h1><span style="color: #ff6600;">Reverse Geocoding</span></h1>
<p><span style="color: #000000;">A users location is defined by coordinates. When using the Core Location services, or specifying where a map should center its view, coordinates will be the units developers will be working with. This is all well and good for presenting mapping information visually but there is a whole other set of information that can be derived from a set of coordinates.</span></p>
<ul>
<li>Country</li>
<li>State</li>
<li>Zip Code</li>
<li>Address</li>
</ul>
<p>Google provides the service to translate any coordinate set to an MKPlacemark object. An MKPlacemark object contains properties to access all this information. Let&#8217;s look into the process of getting this object.</p>
<h2>Step 1</h2>
<p>The first thing you need to do is make a &#8220;View Based Application&#8221;. Now we need to is pick a set of coordinates we want to use. I have decided to use the address of Arizona State University.</p>
<p><strong>Longitude: -111.936619;</strong></p>
<p><strong>Latitude: 33.416199;</strong></p>
<p>Make your AppDelegates .m file look 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;">void</span><span style="color: #002200;">&#41;</span>applicationDidFinishLaunching<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIApplication <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>application <span style="color: #002200;">&#123;</span>
    <span style="color: #11740a; font-style: italic;">// Override point for customization after app launch</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;
	CLLocationCoordinate2D coord;
	coord.longitude <span style="color: #002200;">=</span> <span style="color: #002200;">-</span><span style="color: #2400d9;">111.936619</span>;
	coord.latitude <span style="color: #002200;">=</span> <span style="color: #2400d9;">33.416199</span>;
&nbsp;
	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>;
	<span style="color: #002200;">&#91;</span>geocoder setDelegate<span style="color: #002200;">:</span>self<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>geocoder start<span style="color: #002200;">&#93;</span>
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<h2>Step 2</h2>
<p>Now we need to implement the MKReverseGeocoderDelegate methods. There are only 2 delegate methods:</p>
<p>Called when an error occurs when fetching the reverse geocoder object</p>

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

<p>Called when the ReverseGeocode object is successfully returned.</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>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</pre></td></tr></table></div>

<h2>Step 3</h2>
<p>Now we have a MKPlacemark object to use. An MKPlacemark object conforms to the MKAnnotation protocol. So these objects could be added as annotations to an MKMapView. For more information on MKAnnotations reference <a title="Introduction to MapKit in iPhone OS 3.0" href="/2009/12/21/introduction-to-mapkit-in-iphone-os-3-0/">part 1</a>. The MKPlacemark object contains the following properties that you can access to use in your application.</p>
<p><a href="/wp-content/uploads/2009/12/Screen-shot-2009-12-22-at-11.48.05-AM1.png"><img class="aligncenter size-full wp-image-1563" title="Screen shot 2009-12-22 at 11.48.05 AM" src="/wp-content/uploads/2009/12/Screen-shot-2009-12-22-at-11.48.05-AM1.png" alt="" width="597" height="400" /></a></p>
<p>To see all of these values make the success delegate method look 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;">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>
	NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;The geocoder has returned: %@&quot;</span>, <span style="color: #002200;">&#91;</span>placemark addressDictionary<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>You should see this output:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #2400d9;">2009</span><span style="color: #002200;">-</span><span style="color: #2400d9;">12</span><span style="color: #002200;">-</span><span style="color: #2400d9;">22</span> <span style="color: #2400d9;">11</span><span style="color: #002200;">:</span><span style="color: #2400d9;">23</span><span style="color: #002200;">:</span><span style="color: #2400d9;">05.492</span> ReverseGeocoder<span style="color: #002200;">&#91;</span><span style="color: #2400d9;">2591</span><span style="color: #002200;">:</span><span style="color: #2400d9;">207</span><span style="color: #002200;">&#93;</span> The geocoder has returned<span style="color: #002200;">:</span> <span style="color: #002200;">&#123;</span>
    City <span style="color: #002200;">=</span> Tempe;
    Country <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">&quot;United States&quot;</span>;
    CountryCode <span style="color: #002200;">=</span> US;
    FormattedAddressLines <span style="color: #002200;">=</span>     <span style="color: #002200;">&#40;</span>
        <span style="color: #bf1d1a;">&quot;200-206 E Lemon St&quot;</span>,
        <span style="color: #bf1d1a;">&quot;Tempe, AZ 85281&quot;</span>,
        USA
    <span style="color: #002200;">&#41;</span>;
    State <span style="color: #002200;">=</span> Arizona;
    Street <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">&quot;200-206 E Lemon St&quot;</span>;
    SubAdministrativeArea <span style="color: #002200;">=</span> Maricopa;
    SubThoroughfare <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">&quot;200-206&quot;</span>;
    Thoroughfare <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">&quot;E Lemon St&quot;</span>;
    ZIP <span style="color: #002200;">=</span> <span style="color: #2400d9;">85281</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://icodeblog.com/2009/12/22/introduction-to-mapkit-in-iphone-os-3-0-part-2/feed/</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
	</channel>
</rss>
