Created
April 14, 2015 13:35
-
-
Save jkereako/5ef4d49a61a03a6d017d to your computer and use it in GitHub Desktop.
Simple routine for synchronously checking whether an address is reachable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.