Skip to content

Instantly share code, notes, and snippets.

@Geri-Borbas
Last active December 18, 2015 13:19
Show Gist options
  • Save Geri-Borbas/5789393 to your computer and use it in GitHub Desktop.
Save Geri-Borbas/5789393 to your computer and use it in GitHub Desktop.
static void reachabilityCallback(SCNetworkReachabilityRef reachabilityRef, SCNetworkReachabilityFlags flags, void* info)
{
RCViewController *viewController = (__bridge RCViewController*)info; //Cast context object.
[viewController showReachabilityFlags:flags]; //Show.
//Tear down reachability.
SCNetworkReachabilityUnscheduleFromRunLoop(reachabilityRef, CFRunLoopGetMain(), kCFRunLoopDefaultMode);
CFRelease(reachabilityRef);
}
-(void)reach:(NSString*) hostName
{
//Create with context.
SCNetworkReachabilityRef reachabilityRef = SCNetworkReachabilityCreateWithName(NULL, [hostName UTF8String]);
SCNetworkReachabilityContext context = {0, (__bridge void*)self, nil, nil, nil};
if (NO) //The synchronous way, works well with IP addresses as hostname.
{
//Get flags.
SCNetworkReachabilityFlags flags;
SCNetworkReachabilityGetFlags(reachabilityRef, &flags);
[self showReachabilityFlags:flags]; //Show.
}
else //The asynchronous way, NOT WORKS (!!!) with IP addresses as hostname.
{
//Set callback, then register.
SCNetworkReachabilitySetCallback(reachabilityRef, reachabilityCallback, &context);
SCNetworkReachabilityScheduleWithRunLoop(reachabilityRef, CFRunLoopGetMain(), kCFRunLoopDefaultMode);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment