Skip to content

Instantly share code, notes, and snippets.

@drodriguez
Forked from fguillen/MyReachability.m
Created February 10, 2010 09:10
Show Gist options
  • Save drodriguez/300168 to your computer and use it in GitHub Desktop.
Save drodriguez/300168 to your computer and use it in GitHub Desktop.
// Reachability Objective-C minimal version
// I'm new so memory leaks are possible on this code
// Extracted from:
// http://www.idevapps.com/forum/archive/index.php/t-3329.html
// (look for an imported_kelvin's post)
//
+ (BOOL)networkAvailable{
SCNetworkReachabilityRef netreach;
SCNetworkConnectionFlags flags;
struct sockaddr_in zero;
memset(&zero, 0, sizeof(zero));
zero.sin_len = sizeof(zero);
zero.sin_family = AF_INET;
netreach = SCNetworkReachabilityCreateWithAddress( kCFAllocatorSystemDefault, (struct sockaddr *) zero );
if (!SCNetworkReachabilityGetFlags( netreach, &flags )) {
return FALSE;
}
CFRelease(netreach);
BOOL reachable = !(flags & (kSCNetworkReachabilityFlagsConnectionRequired | kSCNetworkReachabilityFlagsTransientConnection));
return reachable;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment