iOS: Check whether internet connection is available

On April 28, 2012, in iOS, Objective C, by Dipin Krishna

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;
	}
}

6 Responses to “iOS: Check whether internet connection is available”

  1. Malini says:

    Very good article.More work in less code.

    Thanks

  2. Saad says:

    Great tutorials, great examples with complete explanations.
    Dipin bhai please make some tutorial on color picker also, like if I have my own picture in an imageview and I want to get the color and its values, e.g: like if i click on head it returns me black color with its values rgb hsv and hex code..

  3. RathaKrisna says:

    Can you add Source code Download options please.. many of your article is really helped me lot. if it is having download option means, it will be more helpful for us.. thank you for such a great work.

  4. greg says:

    Awesome example! Thank you.

  5. Excellent. Works perfectly.

    Thanks

Leave a Reply

*

Search