Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

check internet connection in cocoa application

How do I check internet connection in an OS X cocoa application? Can Apple's iOS Reachability example code be reused for this purpose?

Thanks,

Nava

like image 563
Nava Carmon Avatar asked Jun 08 '10 08:06

Nava Carmon


People also ask

How do I check my internet connection status?

Select the Start button, then type settings. Select Settings > Network & internet. The status of your network connection will appear at the top.

How does Nwpathmonitor check internet connection?

You can use the function usesInterfaceType(_:) to check which interface type this network path uses. This is the most effective way to figure out if your app is connected over WiFi, cellular or ethernet. print(“It's WiFi!”)

How do I check my network on IOS?

Make sure that your device is connected to a Wi-Fi or cellular network. Tap Settings > General > About. If an update is available, you'll see an option to update your carrier settings. To see the version of carrier settings on your device, tap Settings > General > About and look next to Carrier.


2 Answers

The current version of Reachability code (2.2) listed on Apple's site and referenced above does NOT compile as-is for a Mac OS X Cocoa application. The constant kSCNetworkReachabilityFlagsIsWWAN is only available when compiling for TARGET_OS_IPHONE and Reachability.m references that constant. You will need to #ifdef the two locations in Reachability.m that reference it like below:

#if TARGET_OS_IPHONE
      (flags & kSCNetworkReachabilityFlagsIsWWAN)               ? 'W' : '-',
#else
      0,
#endif

and

#if TARGET_OS_IPHONE
if ((flags & kSCNetworkReachabilityFlagsIsWWAN) == kSCNetworkReachabilityFlagsIsWWAN)
{
    // ... but WWAN connections are OK if the calling application
    //     is using the CFNetwork (CFSocketStream?) APIs.
    retVal = ReachableViaWWAN;
}
#endif
like image 172
dbainbridge Avatar answered Oct 01 '22 01:10

dbainbridge


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

like image 37
Unicorn Avatar answered Sep 30 '22 23:09

Unicorn