views:

200

answers:

2

Hi

How do I check internet connection in cocoa application? Can Reachability code from iphone apple example be reused for this purpose?

Thanks,

Nava

+1  A: 

This code will help you to find if internet is reachable or not:

-(BOOL)isInternetAvail
{
    BOOL bRet = FALSE;
    const char *hostName = [@"google.com" cStringUsingEncoding:NSASCIIStringEncoding];
    SCNetworkConnectionFlags flags = 0;

    if (SCNetworkCheckReachabilityByName(hostName, &flags) && flags > 0) 
    {
        if (flags == kSCNetworkFlagsReachable)
        {
            bRet = TRUE;
        }
        else
        {
        }
    }
    else 
    {
    }
    return bRet;
}

For more information you can look at the iphone-reachability

Unicorn
will this work for cocoa, not iphone application?
Nava Carmon
This will work for cocoa.For iphone you might have replace the flags and method name based on SDK documentation.
Unicorn
thanks, will try it out
Nava Carmon
+2  A: 

Apple has a nice code which does it for you. You can check if your connection is WiFi for instnace or just cell/WiFi. link text

krzyspmac
Yes, I'm using this for checking the connectivity for iPhone. My question was whether it will work for regular cocoa application either
Nava Carmon
http://developer.apple.com/iphone/library/samplecode/Reachability/Introduction/Intro.html says: Runtime Requirements: Mac OS X 10.5.3, iPhone OS 3.0, so my guess is yes.
krzyspmac