<?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>Chris Danielson&#039;s Blog &#187; SCNetworkReachability</title>
	<atom:link href="http://www.chrisdanielson.com/tag/scnetworkreachability/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.chrisdanielson.com</link>
	<description>Trials and Tribulations of a Software Developer</description>
	<lastBuildDate>Wed, 14 Sep 2011 00:00:51 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>iPhone Network Connectivity Test Example</title>
		<link>http://www.chrisdanielson.com/2009/07/22/iphone-network-connectivity-test-example/</link>
		<comments>http://www.chrisdanielson.com/2009/07/22/iphone-network-connectivity-test-example/#comments</comments>
		<pubDate>Thu, 23 Jul 2009 03:09:05 +0000</pubDate>
		<dc:creator>Chris Danielson</dc:creator>
				<category><![CDATA[iPhone Development]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Network Test]]></category>
		<category><![CDATA[Objective-C]]></category>
		<category><![CDATA[SCNetworkReachability]]></category>

		<guid isPermaLink="false">http://www.chrisdanielson.com/?p=70</guid>
		<description><![CDATA[Every iPhone developer that has integrated a network connection based application has had to follow the Apple HID (Human Interface Design) rules. This means, that in order to get the Apple reviewers to sign-off on your application, you have to pass the network connectivity test. Basically, in the case of a catastrophic network disconnect issue [...]]]></description>
			<content:encoded><![CDATA[<p>Every iPhone developer that has integrated a network connection based application has had to follow the Apple <a href="http://developer.apple.com/iphone/library/documentation/UserExperience/Conceptual/MobileHIG/Introduction/Introduction.html">HID (Human Interface Design)</a> rules.  This means, that in order to get the Apple reviewers to sign-off on your application, you have to pass the network connectivity test.  Basically, in the case of a catastrophic network disconnect issue with 3G and WiFi does your application properly alert the user that it will not perform properly without the usage of the network?  I&#8217;ve seen a lot of workaround techniques where users attempt to do an HTTP GET on say Google or some other website.  Clearly, this kind of technique will work sporadically in an unknown environment common to the mobile device.  I mean, imagine causing your application users to sit through a 5-30 second web request timeout.  The end-user will probably believe that the application is hung.  The ideal solution happens to be handed to us in the API Framework <a href="http://developer.apple.com/iphone/library/documentation/SystemConfiguration/Reference/SCNetworkReachabilityRef/Reference/reference.html">SCNetworkReachability</a>.  (<i>Make sure your codebase is linked properly with the &#8220;SystemConfiguration&#8221; Framework</i>).<br />
It&#8217;s worked for me and I recommend it for doing a simple network connectivity test.</p>

<div class="wp_codebox_msgheader"><span class="right"></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p70code2'); return false;">View Code</a> OBJC</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p702"><td class="code" id="p70code2"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &lt;sys/socket.h&gt;</span>
<span style="color: #6e371a;">#import &lt;netinet/in.h&gt;</span>
<span style="color: #6e371a;">#import &lt;arpa/inet.h&gt;</span>
<span style="color: #6e371a;">#import &lt;netdb.h&gt;</span>
<span style="color: #6e371a;">#import &lt;SystemConfiguration/SCNetworkReachability.h&gt;</span>
<span style="color: #11740a; font-style: italic;">//Snip, you know we're in the implementation...</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span> connectedToNetwork
<span style="color: #002200;">&#123;</span>
	<span style="color: #11740a; font-style: italic;">// Create zero addy</span>
	<span style="color: #a61390;">struct</span> sockaddr_in zeroAddress;
	bzero<span style="color: #002200;">&#40;</span><span style="color: #002200;">&amp;</span>zeroAddress, <a href="http://www.opengroup.org/onlinepubs/009695399/functions/sizeof.html"><span style="color: #a61390;">sizeof</span></a><span style="color: #002200;">&#40;</span>zeroAddress<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
	zeroAddress.sin_len <span style="color: #002200;">=</span> <a href="http://www.opengroup.org/onlinepubs/009695399/functions/sizeof.html"><span style="color: #a61390;">sizeof</span></a><span style="color: #002200;">&#40;</span>zeroAddress<span style="color: #002200;">&#41;</span>;
	zeroAddress.sin_family <span style="color: #002200;">=</span> AF_INET;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Recover reachability flags</span>
	SCNetworkReachabilityRef defaultRouteReachability <span style="color: #002200;">=</span> SCNetworkReachabilityCreateWithAddress<span style="color: #002200;">&#40;</span><span style="color: #a61390;">NULL</span>, <span style="color: #002200;">&#40;</span><span style="color: #a61390;">struct</span> sockaddr <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&amp;</span>zeroAddress<span style="color: #002200;">&#41;</span>;
	SCNetworkReachabilityFlags flags;
&nbsp;
	<span style="color: #a61390;">BOOL</span> didRetrieveFlags <span style="color: #002200;">=</span> SCNetworkReachabilityGetFlags<span style="color: #002200;">&#40;</span>defaultRouteReachability, <span style="color: #002200;">&amp;</span>flags<span style="color: #002200;">&#41;</span>;
	CFRelease<span style="color: #002200;">&#40;</span>defaultRouteReachability<span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span>didRetrieveFlags<span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #a61390;">return</span> <span style="color: #a61390;">NO</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #a61390;">BOOL</span> isReachable <span style="color: #002200;">=</span> flags <span style="color: #002200;">&amp;</span> kSCNetworkFlagsReachable;
	<span style="color: #a61390;">BOOL</span> needsConnection <span style="color: #002200;">=</span> flags <span style="color: #002200;">&amp;</span> kSCNetworkFlagsConnectionRequired;
	<span style="color: #a61390;">return</span> <span style="color: #002200;">&#40;</span>isReachable <span style="color: #002200;">&amp;&amp;</span> <span style="color: #002200;">!</span>needsConnection<span style="color: #002200;">&#41;</span> ? <span style="color: #a61390;">YES</span> <span style="color: #002200;">:</span> <span style="color: #a61390;">NO</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">//call like:</span>
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> start <span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span><span style="color: #002200;">&#91;</span>self connectedToNetwork<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <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;Network Connection Error&quot;</span> 
                         message<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;You need to be connected to the internet to use this feature.&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> <span style="color: #a61390;">else</span> <span style="color: #002200;">&#123;</span>
             <span style="color: #11740a; font-style: italic;">//do something </span>
        <span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>This code can also be found on this <a href="http://www.iphonedevsdk.com/forum/iphone-sdk-development/7300-test-if-internet-connection-available.html">forum</a> post.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.chrisdanielson.com/2009/07/22/iphone-network-connectivity-test-example/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
	</channel>
</rss>

