Site icon Dipin Krishna

iOS: Check whether internet connection is available

xcode

Here, i am just checking whether network connectivity is available to a server and its done synchronously.
You can use Reachability if you need notifications when there is any change in the network connectivity.

For this you have to include the SystemConfiguration.framework in your project.

Now add,

#import "SystemConfiguration/SystemConfiguration.h"

and the below function to your .m file.

- (BOOL) isConnectionAvailable
{
	SCNetworkReachabilityFlags flags;
        BOOL receivedFlags;
 
        SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithName(CFAllocatorGetDefault(), [@"dipinkrishna.com" UTF8String]);
        receivedFlags = SCNetworkReachabilityGetFlags(reachability, &flags);
        CFRelease(reachability);
 
        if (!receivedFlags || (flags == 0) )
        {
        	return FALSE;
        } else {
		return TRUE;
	}
}
Exit mobile version