Thursday, 20 February 2014

iPhone Development:Check for internet connection in Iphone SDK







Most of our apps required to communicate with remote server so that they can connect with centralized database for our app. To achieve this functionality we use web-services for remote connection and made our request using NSUrlConnection Class. Now as we all already know ho to connect with remote server from our app one of the hurdle arises, how can we check whether we have internet connection or not. In this post i am going to tell you how to check for internet connection availability in iPhone SDK.Objective C.
You have to use Reachability classes provide by apple.You can download the Reachabaility files from below link
Reachability.h
Reachability.m

Add above two files to your project and then add SystemConfiguration framework

Now go to the class where you want to check for internet availability and import Reachability file 
#import "Reachability.h"
To check for interent connection you can use given below code

-(void)CheckForInternet
{
    Reachability *reachability = [Reachability reachabilityForInternetConnection];
    NetworkStatus internetStatus = [reachability currentReachabilityStatus];
    if (internetStatus == NotReachable) {
        UIAlertView *networkAlert = [[UIAlertView alloc]initWithTitle:@"Sorry!" message:@"Unable to connect with server\nPlease try again later...." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [networkAlert show];
        [networkAlert release];
        return FALSE;
    }
    else
    {
        //available internet connection
    }

}



No comments:
Write comments