Search code examples
ioscocoa-touchhost

Check if a host is up on iOS SDK


I'm making an iOS app where I'd need to check if a host, for example, google.com, is available (or is up/down). I've been using this code:

NSString *host = theTextField.text;
        bool success = false;
        const char *host_name = [host
                                 cStringUsingEncoding:NSASCIIStringEncoding];

        SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithName(NULL,
                                                                                    host_name);
        SCNetworkReachabilityFlags flags;
        success = SCNetworkReachabilityGetFlags(reachability, &flags);
        bool isAvailable = success && (flags & kSCNetworkFlagsReachable) && 
        !(flags & kSCNetworkFlagsConnectionRequired);
        if (isAvailable) {

            status.text = @"The host is up! :)";

        } else {
            status.text = @"The host is down! :(";
        }

    }

But I've a felling it doesn't works like it should. For example if I enter google.com it says it's up, okay, but if I enter the url of my webpage (which is down ATM) says it's up as well (the address is zad0xsis.net). If I enter a fake address it says it's down, so it may work right and I may be thinking odd things IDK.


Solution

  • zad0xsis.net is up for me.

    The Reachability use a the same principle as the ping command line (DNS Resolution + Ping in fact).

    I think you should use NSURLConnection to see if the webserver is up.