0

Hello friends, I want to check internet connection each & every time is active or not in my iPhone apps so please tell me any link or any idea to develop this functionality.

Thanks in advance..

Nikunj Jadav
  • 3,417
  • 7
  • 38
  • 54

4 Answers4

3

Use Reachability Classes

+ (BOOL)isNetworkAvailable {

    Reachability *internetReach;
    internetReach = [Reachability reachabilityForInternetConnection];
    [internetReach startNotifier];
    NetworkStatus netStatus = [internetReach currentReachabilityStatus];
    if(netStatus == NotReachable) { 
        NSLog(@"Network Unavailable");
        return NO;
    }
    else
        return YES;
}
visakh7
  • 26,380
  • 8
  • 55
  • 69
  • @Nikunj, Just a tip: Adding dots after "Thanks a lot" makes it sound a bit sarcastic :p – Svish Aug 04 '11 at 10:37
0
//check for internet connection
NSStringEncoding enc;
NSError *error;
NSString *connected = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.apple.com"] usedEncoding:&enc error:&error];
    if (connected == nil) {
        //no internet connection..display alert
    } else {
       //call your function
    }

This should help you.

Neelesh
  • 3,673
  • 8
  • 47
  • 78
  • This would dalay and if internet is not reachable the thread will stop for around 30 seconds. This is not acceptable – Odys Aug 04 '11 at 10:28
0

You can use the Reachability example from apple developer.

I use it like this:

-(BOOL) isInternetReachable 
{
    Reachability *r = [Reachability reachabilityForInternetConnection];
    NetworkStatus internetStatus = [r currentReachabilityStatus];
    if(internetStatus == NotReachable) 
    {
        return NO;
    }
    return YES;
}

You can also check to see if a certain server is up with the provided methods.

Odys
  • 8,951
  • 10
  • 69
  • 111
0

With the help of Reachability Class and also go through...

Community
  • 1
  • 1
alloc_iNit
  • 5,173
  • 2
  • 26
  • 54