无论我使用哪个主机,都无法访问可达性主机

时间:2013-02-14 21:46:43

标签: ios connection host reachability

这种情况(NSLog(@“主机服务器的网关已关闭。”); 总是出于某种原因。

我在幕后使用Apple Reachability类。 我试图插入其他主机,但没有运气请帮助。

提前致谢。

这是代码

@implementation ConnectionManager
@synthesize internetActive, hostActive;

-(id)init {
self = [super init];
if(self) {

}
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkNetworkStatus:) name:@"NetworkReachabilityChangedNotification" object:nil];

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

hostReachable = [Reachability reachabilityWithHostName:@"www.google.com"];
[hostReachable startNotifier];



return self;
}

- (void) checkNetworkStatus:(NSNotification *)notice
{
NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
switch (internetStatus)

{
    case NotReachable:
    {
        NSLog(@"The internet is down.");
        self.internetActive = NO;

        break;

    }
    case ReachableViaWiFi:
    {
        NSLog(@"The internet is working via WIFI.");
        self.internetActive = YES;

        break;

    }
    case ReachableViaWWAN:
    {
        NSLog(@"The internet is working via WWAN.");
        self.internetActive = YES;

        break;

    }
}

NetworkStatus hostStatus = [hostReachable currentReachabilityStatus];
switch (hostStatus)

{
    case NotReachable:
    {
        NSLog(@"A gateway to the host server is down.");
        self.hostActive = NO;

        break;

    }
    case ReachableViaWiFi:
    {
        NSLog(@"A gateway to the host server is working via WIFI.");
        self.hostActive = YES;

        break;

    }
    case ReachableViaWWAN:
    {
        NSLog(@"A gateway to the host server is working via WWAN.");
        self.hostActive = YES;

        break;

    }
}

}

1 个答案:

答案 0 :(得分:0)

由于您没有提到internetReachable显示相同的错误,我假设您能够连接到互联网并且该部分工作正常。对于hostReachable部分,您可以尝试将其更改为:

更改

[Reachability reachabilityWithHostName:@"www.google.com"]; 

[Reachability reachabilityWithHostName:@"http://www.google.com"];