Posted By

357_gaby on 05/18/09


Tagged

iphone


Versions (?)

Who likes this?

7 people have marked this snippet as a favorite

alladinian
davidsi
umang_nine
avanadra
sb
nsbuckner
Tintenklecks


Check Internet Connection


 / Published in: Objective C
 

taken from apple source code -- seismic sample application

worth mentioning: they only check for connectivity once -- invoking performance as the reason

  1. static BOOL checkNetwork = YES;
  2. if (checkNetwork) { // Since checking the reachability of a host can be expensive, cache the result and perform the reachability check once.
  3. checkNetwork = NO;
  4.  
  5. Boolean success;
  6. const char *host_name = "earthquake.usgs.gov";
  7.  
  8. SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithName(NULL, host_name);
  9. SCNetworkReachabilityFlags flags;
  10. success = SCNetworkReachabilityGetFlags(reachability, &flags);
  11. _isDataSourceAvailable = success && (flags & kSCNetworkFlagsReachable) && !(flags & kSCNetworkFlagsConnectionRequired);
  12. CFRelease(reachability);
  13. }
  14. return _isDataSourceAvailable;

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: 357_gaby on May 18, 2009

Requires System.Configuration framework

You need to login to post a comment.