{"id":1458,"date":"2009-02-23T15:50:13","date_gmt":"2009-02-23T18:50:13","guid":{"rendered":"http:\/\/brockerhoff.net\/bb\/viewtopic.php?p=2666"},"modified":"2010-05-08T11:21:35","modified_gmt":"2010-05-08T14:21:35","slug":"re-cocoa-musings-pt-4","status":"publish","type":"post","link":"https:\/\/brockerhoff.net\/blog\/2009\/02\/23\/re-cocoa-musings-pt-4\/","title":{"rendered":"Re: Cocoa musings pt.4"},"content":{"rendered":"<p><a href=\"http:\/\/www.zathras.de\/angelweb\/home.htm\">M. Uli Kusterer<\/a> emailed me to say I&#8217;d forgotten that some people still have non-broadband connections. I had supposed that NSURLConnection would simply return an immediate error if the internet link is down &#8211; it turns out that it may autoconnect if the user has a dialup connection, for instance.<\/p>\n<p>The way around that is to use the network reachability APIs. Here&#8217;s one way to insert that into the <a href=\"\/bb\/viewtopic.php?p=2665#2665\">example below<\/a>:<\/p>\n<pre><code>#include &lt;SystemConfiguration\/SCNetworkReachability.h&gt;\r\n...\r\n- (id)initWithURL:(NSURL*)theURL delegate:(id)theDelegate {\r\n\u00a0 \u00a0if ((self = [super init])) {\r\n\u00a0 \u00a0\u00a0 \u00a0SCNetworkConnectionFlags flags = 0;\r\n\u00a0 \u00a0\u00a0 \u00a0if (SCNetworkCheckReachabilityByName([[theURL host] UTF8String], &amp;flags)) {\r\n\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0const SCNetworkConnectionFlags mask =\r\n\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0kSCNetworkFlagsReachable|\r\n\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0kSCNetworkFlagsConnectionRequired|\r\n\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0kSCNetworkFlagsConnectionAutomatic|\r\n\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0kSCNetworkFlagsInterventionRequired;\r\n\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0if ((flags&amp;mask)==kSCNetworkFlagsReachable) {\r\n\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0delegate = theDelegate;\r\n\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0data = nil;\r\n  \u00a0\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0conn = [[NSURLConnection alloc]\r\n\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0initWithRequest:[NSURLRequest requestWithURL:theURL]\r\n\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0cachePolicy:NSURLRequestReloadIgnoringLocalCacheData\r\n\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0timeoutInterval:60.0]\r\n\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0delegate:self];\r\n\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0return self;\r\n\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0}\r\n\u00a0 \u00a0\u00a0 \u00a0}\r\n\u00a0 \u00a0\u00a0 \u00a0[self autorelease];\r\n\u00a0 \u00a0\u00a0 \u00a0self = nil;\r\n\u00a0 \u00a0}\r\n\u00a0 \u00a0return self;\r\n} <\/code><\/pre>\n<p>This changes the alloc\/init pair to return nil whenever the URL host is unreachable&#8230; you&#8217;ll have to test for that of course.<\/p>\n<p><strong>Update:<\/strong> the downside to the above approach is that the SCNetworkCheckReachabilityByName() function will block until it gets the information &#8211; which may take several seconds, perhaps more on some systems. The solution I&#8217;m actually using is more complex, installing a reachability transition callback, which sets a global flag that is tested before activating the NSURLConnection.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>M. Uli Kusterer emailed me to say I&#8217;d forgotten that some people still have non-broadband connections. I had supposed that NSURLConnection would simply return an immediate error if the internet link is down &#8211; it turns out that it may autoconnect if the user has a dialup connection, for instance. The way around that is [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[26],"class_list":["post-1458","post","type-post","status-publish","format-standard","hentry","category-dev","tag-cocoa"],"featured_image_src":null,"author_info":{"display_name":"Rainer Brockerhoff","author_link":"https:\/\/brockerhoff.net\/blog\/author\/rbrockerhoff\/"},"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1q3Zc-nw","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/brockerhoff.net\/blog\/wp-json\/wp\/v2\/posts\/1458","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/brockerhoff.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/brockerhoff.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/brockerhoff.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/brockerhoff.net\/blog\/wp-json\/wp\/v2\/comments?post=1458"}],"version-history":[{"count":0,"href":"https:\/\/brockerhoff.net\/blog\/wp-json\/wp\/v2\/posts\/1458\/revisions"}],"wp:attachment":[{"href":"https:\/\/brockerhoff.net\/blog\/wp-json\/wp\/v2\/media?parent=1458"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/brockerhoff.net\/blog\/wp-json\/wp\/v2\/categories?post=1458"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/brockerhoff.net\/blog\/wp-json\/wp\/v2\/tags?post=1458"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}