Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在iOS中检查网络可达性_Ios_Objective C_Networking_Reachability - Fatal编程技术网

如何在iOS中检查网络可达性

如何在iOS中检查网络可达性,ios,objective-c,networking,reachability,Ios,Objective C,Networking,Reachability,我有一个小问题。我使用可达性类测试互联网连接,它工作得很好。然而,假设用户正在使用Wifi,但他们无法访问internet,它应该重试大约10秒,然后它应该向用户显示一个警报以切换到那个里的蜂窝网络。下面是我的代码。当Wifi开启时,我没有达到网络连接速度慢的程度 self.internetReachable = [Reachability reachabilityForInternetConnection]; [self.internetReachable startNotifier]; /

我有一个小问题。我使用可达性类测试互联网连接,它工作得很好。然而,假设用户正在使用Wifi,但他们无法访问internet,它应该重试大约10秒,然后它应该向用户显示一个警报以切换到那个里的蜂窝网络。下面是我的代码。当Wifi开启时,我没有达到网络连接速度慢的程度

self.internetReachable = [Reachability reachabilityForInternetConnection];
[self.internetReachable startNotifier];

//_hasConnectivity = ([self.internetReachable currentReachabilityStatus] != NotReachable);
if([self.internetReachable currentReachabilityStatus] == NotReachable) {
    _hasConnectivity = NO;
}
else if([self.internetReachable currentReachabilityStatus] == ReachableViaWiFi){
    _hasConnectivity = YES;
}
else if([self.internetReachable currentReachabilityStatus] == ReachableViaWWAN) {
    _hasConnectivity = YES;
}

尝试使用以下代码返回
BOOL
值,返回
YES
如果连接可用,则返回
NO

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

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

   CFRelease (dReference);

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

感谢您的快速回答,所以在这种情况下,当用户显示Wifi连接时,它将起作用,但由于速度缓慢,他们无法访问internet。对于我的情况,internet与3g连接,并且显示连接已断开,则不起作用。