从Apple下载可达性,使用此方法检查活动连接:
-(BOOL)isReachable{
Reachability *r = [Reachability reachabilityWithHostName:@"http://www.google.com"];
if(NotReachable == [r currentReachabilityStatus]) {
NSLog(@"Not Reachable");
return NO;
}
NSLog(@"Reachable");
return YES;
}
尽管已连接,但每次都返回NO?无法弄清楚为什么......
有什么想法吗?感谢。
另一方面,任何人都可以推荐一个很好的清洁Reachability单件类?
答案 0 :(得分:30)
已编辑:您应该从http
来电中移除协议reachabilityWithHostName
,然后将其更新为
Reachability *reachability = [Reachability reachabilityWithHostname:@"www.google.com"];
NetworkStatus netStatus = [reachability currentReachabilityStatus];
答案 1 :(得分:5)
使用此代码检查设备是否已连接到互联网
在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);
}
答案 2 :(得分:2)
Tony Million的Reachability也遇到了同样的问题:网络状态始终设置为NotReachable。 我通过添加SystemConfiguration Framework来修复它
希望有所帮助
答案 3 :(得分:1)
我使用KSReachability。它适用于NSNotification,块,KVO,以及是否有ARC。
KSReachability *reachability = [KSReachability reachabilityToHost:@"www.google.com"];
reachability.onReachabilityChanged = ^(KSReachability* reachability) {
NSLog(@"Update: network is %@", reachability.reachable ? @"up." : @"down.");
};
答案 4 :(得分:1)
如果您正在尝试查看设备是否可以访问互联网,您应该使用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
检查您可以使用的其他类中可以访问的网络。
[[NetworkManager sharedInstance] isNetworkReachable];
答案 5 :(得分:0)
另一个完整的答案:
-(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;
}