Skip to content

Instantly share code, notes, and snippets.

@jkereako
Created April 14, 2015 13:35
Show Gist options
  • Save jkereako/5ef4d49a61a03a6d017d to your computer and use it in GitHub Desktop.
Save jkereako/5ef4d49a61a03a6d017d to your computer and use it in GitHub Desktop.
Simple routine for synchronously checking whether an address is reachable
@interface SynchronousReachability ()
@property (nonatomic, readonly, getter=isNetworkReachable) BOOL networkReachable;
@end
@implementation SynchronousReachability
- (BOOL)isNetworkReachable {
SCNetworkReachabilityFlags flags;
SCNetworkReachabilityRef address;
Boolean success;
bool reachable;
address = SCNetworkReachabilityCreateWithName(kCFAllocatorDefault, "www.apple.com");
success = SCNetworkReachabilityGetFlags(address, &flags);
CFRelease(address);
reachable = success
&& !(flags & kSCNetworkReachabilityFlagsConnectionRequired)
&& (flags & kSCNetworkReachabilityFlagsReachable);
return (BOOL)reachable;
}
@jkereako
Copy link
Author

jkereako commented May 27, 2015

Description

Blocks the main thread to test for internet connectivity. It's intended use is to have internet connectivity as a prerequisite for whatever code is to follow.

Keep in mind this is bad practice (see the first bullet point in Alamofire's README) and should be used if the product owner (which could be you, the developer) thinks this is absolutely necessary. Otherwise, follow Matt Thompson's advice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment