<?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>Mac / iPhone App Development &#187; tutorials</title>
	<atom:link href="http://benreeves.co.uk/tag/tutorials/feed/" rel="self" type="application/rss+xml" />
	<link>http://benreeves.co.uk</link>
	<description>Home of a Small Time Developer</description>
	<lastBuildDate>Wed, 26 May 2010 14:24:12 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Adding Reachability Code to your app</title>
		<link>http://benreeves.co.uk/adding-reachability-code-to-your-app/</link>
		<comments>http://benreeves.co.uk/adding-reachability-code-to-your-app/#comments</comments>
		<pubDate>Tue, 26 Jan 2010 19:37:41 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[objective c]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[tutorials]]></category>

		<guid isPermaLink="false">http://www.benreeves.co.uk/?p=130</guid>
		<description><![CDATA[Ok so iVersion 1.4 was declined due to not loading correctly when their is no network connectivity.
When the device is connected to a cellular network, iVersion does not load its contents.  After the user enters the URL (http://svn.collab.net/repos/svn/trunk/) and taps &#8220;connect,&#8221; an error message is received.
Apparently the error message is not clear enough or something, anyway this [...]]]></description>
			<content:encoded><![CDATA[<p>Ok so iVersion 1.4 was declined due to not loading correctly when their is no network connectivity.</p>
<blockquote><p>When the device is connected to a cellular network, iVersion does not load its contents.  After the user enters the URL (<a href="http://svn.collab.net/repos/svn/trunk/" target="_blank" class="broken_link">http://svn.collab.net/repos/svn/trunk/</a>) and taps &#8220;connect,&#8221; an error message is received.</p></blockquote>
<p>Apparently the error message is not clear enough or something, anyway this is not a rant, this is what apple recommends.</p>
<blockquote><p>Please take a look at the Reachability iPhone program sample which demonstrates the use of the System Configuration Reachability API to detect the absence of WiFi and Wireless Wide Area Network (WWAN) services. Your application can then take appropriate action at the first point where network services are required.</p></blockquote>
<p>The sample code can be found on Apple developer site, however the two file we are interested in are:</p>
<p><strong>Download the Reachability Code:</strong><br />
<a href="http://www.benreeves.co.uk/wp-content/uploads/2010/01/Reachability.h">Reachability.h</a></p>
<p><a href="http://www.benreeves.co.uk/wp-content/uploads/2010/01/Reachability.m">Reachability.m</a></p>
<p><strong>How to use:</strong><br />
Lets say you have a simple UILabel which you want to update with either &#8220;(Offline)&#8221; or &#8220;(Online)&#8221; depending on whether the network is reachable. Your view controller would be defined something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> MyStatusView <span style="color: #002200;">:</span> UIViewController
<span style="color: #002200;">&#123;</span>
	UILabel <span style="color: #002200;">*</span> statusLabel;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@property</span><span style="color: #002200;">&#40;</span>nonatomic, assign<span style="color: #002200;">&#41;</span> IBOutlet UILabel <span style="color: #002200;">*</span> statusLabel;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>reachabilityChanged<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSNotification</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>note;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>updateStatus;
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<p>Then we add the following code to the view controller .m file:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>viewWillAppear<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span>animated
<span style="color: #002200;">&#123;</span>
	self.reachability <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>Reachability reachabilityWithHostName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;google.com&quot;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>reachability startNotifer<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNotificationCenter</span> defaultCenter<span style="color: #002200;">&#93;</span> addObserver<span style="color: #002200;">:</span>self selector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>reachabilityChanged<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span> name<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;kNetworkReachabilityChangedNotification&quot;</span> object<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #002200;">&#91;</span>self updateStatus<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>viewWillDisappear<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span>animated
<span style="color: #002200;">&#123;</span>
	self.reachability <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
	<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNotificationCenter</span> defaultCenter<span style="color: #002200;">&#93;</span> removeObserver<span style="color: #002200;">:</span>self<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>reachabilityChanged<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSNotification</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>notification
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>self updateStatus<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>updateStatus
<span style="color: #002200;">&#123;</span>
	NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Reachability changed&quot;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>reachability currentReachabilityStatus<span style="color: #002200;">&#93;</span> <span style="color: #002200;">==</span> NotReachable<span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		statusLabel.text <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;(Offline)&quot;</span>;
	<span style="color: #002200;">&#125;</span>
	<span style="color: #a61390;">else</span>
	<span style="color: #002200;">&#123;</span>
		statusLabel.text <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;(Online)&quot;</span>;
	<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>I think it&#8217;s fairly self explanatory. Basically when the view appears the reachability class is created and started using startNotifier. Then we hook in our reachabilityChanged: method into the notification centre, so it is automatically called when the network status changes. reachabilityChanged: calls updateStatus which simply changes our label depending on the connectivity.</p>
]]></content:encoded>
			<wfw:commentRss>http://benreeves.co.uk/adding-reachability-code-to-your-app/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
