可达性不按预期工作

从Apple下载可达性,使用此方法检查活动连接:

-(BOOL)isReachable{ Reachability *r = [Reachability reachabilityWithHostName:@"http://www.google.com"]; if(NotReachable == [r currentReachabilityStatus]) { NSLog(@"Not Reachable"); return NO; } NSLog(@"Reachable"); return YES; } 

尽pipe连接,每一次都返回NO? 不知道为什么…

有任何想法吗? 谢谢。

在一个侧面说明,任何人都可以推荐一个很好的清洁可达性单身class吗?

编辑:你应该从你的reachabilityWithHostName调用删除协议, http ,所以更新到

  Reachability *reachability = [Reachability reachabilityWithHostname:@"www.google.com"]; NetworkStatus netStatus = [reachability currentReachabilityStatus]; 

使用此代码来检查设备是否连接到互联网

在viewDidLoad中使用这段代码:

  Reachability* internetReachable; = [Reachability reachabilityForInternetConnection]; [internetReachable startNotifier]; hostReachable = [Reachability reachabilityWithHostName: @"www.apple.com"] ; [hostReachable startNotifier]; 

并将此函数添加到您的代码中:

 -(void) checkNetworkStatus:(NSNotification *)notice { recheabilityBool=FALSE; nonrecheabilityBool=FALSE; // called after network status changes NetworkStatus internetStatus = [internetReachable currentReachabilityStatus]; switch (internetStatus) { case NotReachable: { nonrecheabilityBool=TRUE; internetCon=0; //NSLog(@"The internet is down."); break; } case ReachableViaWiFi: { NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; internetCon=404; [prefs setInteger:internetCon forKey:@"conKey"]; //NSLog(@"The internet is working via WIFI."); break; } case ReachableViaWWAN: { //NSLog(@"The internet is working via WWAN."); break; } } NetworkStatus hostStatus = [hostReachable currentReachabilityStatus]; switch (hostStatus) { case NotReachable: { internetCon=0; if( nonrecheabilityBool==FALSE) { //NSLog(@"A gateway to the host server is down."); } break; } case ReachableViaWiFi: { if(recheabilityBool==FALSE) { recheabilityBool=TRUE; NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; internetCon=404; [prefs setInteger:internetCon forKey:@"conKey"]; //NSLog(@"The internet is working via WIFI."); break; } //NSLog(@"A gateway to the host server is working via WIFI."); break; } case ReachableViaWWAN: { //NSLog(@"A gateway to the host server is working via WWAN."); break; } } } - (BOOL)connected { Reachability *reachability = [Reachability reachabilityForInternetConnection]; NetworkStatus networkStatus = [reachability currentReachabilityStatus]; return !(networkStatus == NotReachable); } 

我与Tony Million的Reachability有同样的问题:networking状态总是设置为NotReachable。 我通过添加SystemConfiguration框架来修复它

希望能帮助到你

我使用KSReachability 。 它适用于NSNotification,块,KVO和有或没有ARC。

 KSReachability *reachability = [KSReachability reachabilityToHost:@"www.google.com"]; reachability.onReachabilityChanged = ^(KSReachability* reachability) { NSLog(@"Update: network is %@", reachability.reachable ? @"up." : @"down."); }; 

如果您试图查看设备是否可以访问Internet,则应该使用reachabilityForInternetConnection而不是reachabilityWithHostName :. 而且,这两个调用都需要一点时间来启动(它仍然在毫秒级,但比到达下一行的if条件所需的时间要长)。下面是一个单例类的例子:使用可达性。

 static NetworkManager* sharedInstance = nil; @interface NetworkManager() @property (nonatomic, strong) Reachability* reachability; @end @implementation NetworkManager @synthesize reachability; + (NetworkManager*)sharedInstance { @synchronized(self) { if (sharedInstance == nil) { sharedInstance = [[NetworkManager alloc] init]; } } return sharedInstance; } - (id)init { reachability = [WYReachability reachabilityForInternetConnection]; } - (BOOL)isNetworkReachable { return ([self.reachability currentReachabilityStatus] != NotReachable); } @end 

要检查可以在其他类中使用的networking。

 [[NetworkManager sharedInstance] isNetworkReachable]; 

另一个完整答案:

 -(BOOL) isReachable:(NSString *)url { //Retrieve the host name by given url address. NSString *hostName = [[NSURL URLWithString:url] host]; Reachability *r = [Reachability reachabilityWithHostName:hostName]; if(NotReachable == [r currentReachabilityStatus]) { NSLog(@"Not Reachable"); return NO; } NSLog(@"Reachable"); return YES; }