1
votes

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.

3

3 Answers

1
votes

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

1
votes

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"];
0
votes

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;
    }
}
}