How to check internet connection reachability in wireless local area network?

Featured Image

You think you will find answer on this question here? I am sorry to disappoint but you are not going to find right answer here. Here you will find ways how you can not check internet connection reachability in some wireless local area network. :-)

I know you will now say there is “Reachability” named sample app at developer connect library to download, but it ain’t what I am looking for. What I am looking for is feature that will tell me do I have internet connection while I have network connection, to be more accurate, do I have internet connection while I am connected to some wireless local area network? I know, some of you will now say: “Well you can make NSURLConnection and wait for didFailWithError or connectionDidFinishLoading delegate methods”. Yeah, but this again ain’t what I am looking for.

What I was really looking for is something like pinging some host. Again, you will now say: “Well, you might don’t know but Reachability sample app has way to test reachability of given host.” Yeah, but this again ain’t what I am looking for.

I just hope that Apple’s developers will soon add some really easy way to do something like this. Please don’t hesitate to leave comments, and please tell me I am wrong on this one and easy way really exist. Please that I am blind as mole and I can’t see! :-) Thanks!

7
Top

Enjoyed this post?

Subscribe to our RSS Feed, Follow us on Twitter and spread it to your friends!

Author

Ivan Kalaica

Senior OSX and iOS SDK Developer

At Inchoo, Ivan Kalaica is a Senior OSX and iOS SDK developer.

Other posts from this author

Discussion 7 Comments

Add Comment
  1. Chris

    Indeed reachability is the answer – it checks if the host “apple.com” is available – therefore checking you’re internet connection. here is simple code I striped out of reachability for my app:

    -(BOOL)connectedToWeb{
    BOOL connected;
    const char *host = “www.apple.com”;
    SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithName(NULL, host);
    SCNetworkReachabilityFlags flags;
    connected = SCNetworkReachabilityGetFlags(reachability, &flags);
    BOOL isConnected = connected && (flags & kSCNetworkFlagsReachable) && !(flags & kSCNetworkFlagsConnectionRequired);
    CFRelease(reachability);
    return isConnected;
    }

    You will need to import these at the top of your .m file

    #import
    #import

    And make sure you have added the SystemConfiguration.framework to your project (add existing framework)

    And you can use this to check it works:

    if([self connectedToWeb]) NSLog(@”Connected”);
    else NSLog(@”Not Connected”);

  2. Chris

    hey dunno if its just my browser but i cant see the import files you need to import

  3. Ivan Kalaica

    Thanks Chris, but as I have already mention, I have seen Apple’s Reachability sample app and that code snippet. To see my main issue try to do this: 1. Connect iPhone on your router via wifi connection; 2. Unplug internet connection cable from your router , but don’t shout it down. Your iPhone must still be connected to router’s wireless local area network. 3. Final, try to use app with this code. If you didn’t split your processes on another thread your app will freeze.

    Cheers,
    Ivan

  4. MOMEKS

    thank you Chris

    i used your code on the main.m file ! but igot this error :

    FATAL ERROR : method definition not in @implementation context

    i imported : #import

    but still got FATAL ERROR
    what can i do ?

  5. MOMEKS

    SystemConfiguration/SCNetworkReachability.h

  6. Mike

    Hi Ivan,

    I am stuck with the same problem.
    Did you figure it out?

    I want to check the real internet connection not just to router.

    Hope to hear from U.

    Thanks

  7. Mohan

    Thanks a lot Chris, that is really a notable function

Add Your Comment

Please wrap all source codes with [code][/code] tags.
Top