1

这种情况 (NSLog(@"A gateway to the host server is down."); 总是出于某种原因运行。

我在幕后使用 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;

    }
}

}
4

1 回答 1

0

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

改变

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

[Reachability reachabilityWithHostName:@"http://www.google.com"];
于 2013-02-15T00:15:30.190 回答