How to check for network reachability on iOS in a non-blocking manner?

不问归期 提交于 2019-12-06 02:46:41

The first one tests whether it can reach google.com, while the second one simply checks whether there's any internet connection possible (which it thinks there is, even though there's packet loss).

Basically just put it in a thread or in a background queue. The only way to reliably know whether you have a good connection is to test it, and any test is going to be blocking or asynchronous. You simply can't have it instantly available, ever.

check this function:

- (BOOL)isNetworkAvailable
{
CFNetDiagnosticRef diag;        
diag = CFNetDiagnosticCreateWithURL (NULL, (__bridge CFURLRef)[NSURL URLWithString:@"www.apple.com"]);



CFNetDiagnosticStatus status;
status = CFNetDiagnosticCopyNetworkStatusPassively (diag, NULL);        

CFRelease (diag);

if ( status == kCFNetDiagnosticConnectionUp )
{
    //NSLog (@"Connection is up");
    return YES;
} else {
    NSLog (@"Connection is down");
    return NO;

}
}

This will work just fine..

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!