0

This is a duplicate of this question. SCNetworkReachabilityGetFlags returns 0 even when wireless available

I tried all the solutions proposed in that post. But none of them worked for me.

    SCNetworkReachabilityRef  reachabilityRef = SCNetworkReachabilityCreateWithName(NULL, [hostname UTF8String]);
    BOOL success = SCNetworkReachabilityGetFlags(reachabilityRef, &flags); //flags are always 0.
    bool isAvailable = success && (flags & kSCNetworkFlagsReachable) && !(flags & kSCNetworkFlagsConnectionRequired);

    CFRelease(reachabilityRef);

    if(isAvailable)
    {
        NSLog(@"Host is reachable: %d", flags);
    }
    else
    {
        NSLog(@"Host is unreachable");
    }

I am trying to connect to a host via VPN. Can some one please suggest a workaround?

Community
  • 1
  • 1
Sandeep
  • 7,156
  • 12
  • 45
  • 57
  • Do you get an error code? Define 'not working'. – Undo Apr 25 '13 at 20:59
  • Not working means the flags are always 0 and the subsequent and conditions fail. – Sandeep Apr 25 '13 at 21:22
  • Also, have you tried setting up the notification as outlined in Apple's [Reachability](http://developer.apple.com/library/ios/#samplecode/Reachability/Introduction/Intro.html) sample (see the code in the app delegate)? Do you ever get a notification of change of the reachability status? – Rob Apr 25 '13 at 23:45
  • I assume your host name is just host name, not IP number, no scheme (e.g. "http://"), no directory or port numbers, etc., just host name? Have you logged the host name? You say `flags` (not `isAvailable`) is `0`. What is `success`? Is that zero or non-zero? If zero, have you checked to see what the error codes are as described in the [System Configuration Reference](https://developer.apple.com/library/mac/#documentation/SystemConfiguration/Reference/SystemConfiguration_Utilities/Reference/reference.html)? Have you tried this with other hosts, both VPN and non-VPN? – Rob Apr 26 '13 at 00:44

1 Answers1

2

I've used this API with great success in a shipping app. First, the hostname must something like www.apple.com. Reachability takes some time to determine if the host is there or not.

The strategy I took in my app was to assume at launch it was there, and monitor the reachability notifications that kick in after many (like 20) seconds. If you cannot wait that long, there are other posts on this here with strategies to deal with that, for instance, try pinging the host immediately.

Note that there are many hurdles on mobile to determining this status at launch, and there is no perfect solution.

What I did was assume it was up, put up a spinner that said "Connecting...", and if I got a reachability failure later on took it down (or took it down when the request failed). From then on it seems to update regularly (I use to take my app on trains through tunnels, and I could see it show "disconnect" and "connected" states as network access changed.

David H
  • 40,852
  • 12
  • 92
  • 138