iOS可达性:主机方法问题

发布于 2024-12-06 04:24:35 字数 1084 浏览 8 评论 0原文

我创建了一种方法来检查主机是否可以访问。我从苹果开发者网站下载了 Reachability 类(.h 和 .m)并导入到我的项目中。我已将 NSString 名称作为 URL(主机名)传递。主机名是 http://www.google.com。但是,无论我传递给此方法的主机名是什么,它总是返回 NO (connectToHost)。代码如下:

- (BOOL) checkHostAvailability:(NSString *)Name{
        BOOL connectToHost;
        hostReach = [[Reachability reachabilityWithHostName:Name] retain];
        [hostReach startNotifier];

        NetworkStatus hostStatus = [hostReach currentReachabilityStatus];
        NSLog(@"Name: %@", Name);
        NSLog(@"hostStatus is %@", hostStatus);

        if(hostStatus == NotReachable){
                NSLog(@"Here is the checkHostAvailability Method and host NOT reachable");
                connectToHost = NO;
       }else{
                NSLog(@"Here is the checkHostAvailability Method and host is reachable");
                connectToHost = YES;
       }
       return connectToHost;


}

经过几个小时的调查,我发现 NetworkStatus hostStatus 始终等于 null。我认为这就是该方法不起作用的原因。我花了8个小时找出这段代码的问题并搜索了这个网站,但我仍然找不到问题和解决方案。

请帮助并非常感谢。

I have create a method to check if the host is reachable. I had download the Reachability class (both .h & .m) from apple developer websites and import to my project. I have pass the NSString Name as the URL (host Name). The hostName is http://www.google.com. However, no matter what host name I pass to this method and its always return NO (connectToHost). The code as below:

- (BOOL) checkHostAvailability:(NSString *)Name{
        BOOL connectToHost;
        hostReach = [[Reachability reachabilityWithHostName:Name] retain];
        [hostReach startNotifier];

        NetworkStatus hostStatus = [hostReach currentReachabilityStatus];
        NSLog(@"Name: %@", Name);
        NSLog(@"hostStatus is %@", hostStatus);

        if(hostStatus == NotReachable){
                NSLog(@"Here is the checkHostAvailability Method and host NOT reachable");
                connectToHost = NO;
       }else{
                NSLog(@"Here is the checkHostAvailability Method and host is reachable");
                connectToHost = YES;
       }
       return connectToHost;


}

After a few hours of investigation, I have found out that the NetworkStatus hostStatus always equal to null. I assume this is why this method is not working. And I have spend 8 hours to find out the problem with this code and search out this website, however I still couldn't find the problem and solution.

Please help and much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风轻花落早 2024-12-13 04:24:35

从主机名中删除“http://”。

Remove the 'http://' from the host name.

梦旅人picnic 2024-12-13 04:24:35

如果您想使用 http://www.google.com 作为主机,则可以传递“google.com”作为主机名。请勿包含 http://、结尾斜杠或结尾斜杠后面的任何内容。包含 www. 即可。

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

If you wanted to use http://www.google.com as your host, you would pass 'google.com' as the hostname. Do not include http://, the ending slash or anything that could follow an ending slash. www. is fine to include.

[Reachability reachabilityWithHostName:@"google.com"];
假面具 2024-12-13 04:24:35

这是我用于所有连接检查的代码(请参见此处:iOS 开发者库-可达性),它对我来说工作得很好:

-(BOOL) hasConnection{

//check network status _ iphone settings
Reachability *internetReachability = [Reachability reachabilityForInternetConnection];
[internetReachability startNotifier];


NetworkStatus status=[internetReachability currentReachabilityStatus];
if (status == NotReachable) {
    NSLog(@"No internet");
    //[internetReachability stopNotifier];
    return NO;
}else {
    NSLog(@"Internet is ON");
   // [internetReachability stopNotifier];

    //check internet connection _reachable path

    Reachability *hostReachable=[Reachability   reachabilityWithHostName:@"www.apple.com"];
    BOOL connectionRequired =[hostReachable connectionRequired];
    NSLog(@"%hhd",connectionRequired);

    [hostReachable startNotifier];

    Reachability *wifiReachability=[Reachability reachabilityForLocalWiFi];
    [wifiReachability startNotifier];

    NetworkStatus hostStatus=[hostReachable currentReachabilityStatus];
    if(hostStatus == NotReachable){
        NSLog(@"No internet connection");
        [hostReachable stopNotifier];
        return NO;
    }else{
        NSLog(@"Connection is ok");
        [hostReachable stopNotifier];
        return YES;
    }
}
}

This is the code I use for all the connection checks (see here: iOS Developer Library -Reachability) and it is working fine for me :

-(BOOL) hasConnection{

//check network status _ iphone settings
Reachability *internetReachability = [Reachability reachabilityForInternetConnection];
[internetReachability startNotifier];


NetworkStatus status=[internetReachability currentReachabilityStatus];
if (status == NotReachable) {
    NSLog(@"No internet");
    //[internetReachability stopNotifier];
    return NO;
}else {
    NSLog(@"Internet is ON");
   // [internetReachability stopNotifier];

    //check internet connection _reachable path

    Reachability *hostReachable=[Reachability   reachabilityWithHostName:@"www.apple.com"];
    BOOL connectionRequired =[hostReachable connectionRequired];
    NSLog(@"%hhd",connectionRequired);

    [hostReachable startNotifier];

    Reachability *wifiReachability=[Reachability reachabilityForLocalWiFi];
    [wifiReachability startNotifier];

    NetworkStatus hostStatus=[hostReachable currentReachabilityStatus];
    if(hostStatus == NotReachable){
        NSLog(@"No internet connection");
        [hostReachable stopNotifier];
        return NO;
    }else{
        NSLog(@"Connection is ok");
        [hostReachable stopNotifier];
        return YES;
    }
}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文