0

How can I check whether the app is connected to the internet or not? currently, I am using this code in my appdelegate.m file

dispatch_queue_t connectivityThread = dispatch_queue_create("com.gm.kart.connectivity", NULL);

dispatch_async(connectivityThread, ^{
    while (true){
        if([GMMConnectivity hasConnectivity])
            NSLog(@"%@", @"connected");
        else
            NSLog(@"Not connected");

        usleep(10000000);
    }
});

and when I click my login button I want to do a check whether the internet is connected or not using NSnotificationcenter?

Please help me

Sunil Garg
  • 14,608
  • 25
  • 132
  • 189
Naveen
  • 1,251
  • 9
  • 20
  • 38
  • have you search something ? http://www.google.com/#output=search&sclient=psy-ab&q=How+to+check+internet+connectivity+in+ios%3F&oq=How+to+check+internet+connectivity+in+ios%3F&gs_l=hp.3..0i22i30l2.950.950.0.1947.1.1.0.0.0.0.173.173.0j1.1.0...0.0...1c.1.9.psy-ab.FQR8PLKiNzs&pbx=1&bav=on.2,or.r_qf.&bvm=bv.45368065,d.bmk&fp=55f9ca2dd31d2c85&biw=1600&bih=799 – Buntylm Apr 17 '13 at 10:03
  • tried..but nothing related to my code – Naveen Apr 17 '13 at 10:05
  • I don't know GMMConnectivity, but here is how you do it with Reachability (the Apple provided class) and NSNotificationCenter: http://stackoverflow.com/a/15854823/412916 – Jano Oct 07 '13 at 12:34
  • possible duplicate http://stackoverflow.com/questions/1083701/how-to-check-for-an-active-internet-connection-on-iphone-sdk – Ayaz Feb 10 '14 at 09:38

2 Answers2

11

After download bellow example.

http://developer.apple.com/iphone/library/samplecode/Reachability/index.html

you can use it in your Project like bellow steps:-

included Apple's Reachability.h & .m from their Reachability example.

add the SystemConfiguration framework.

put bellow method in to your appdelegare.m file:-

- (BOOL) connectedToNetwork{
    Reachability* reachability = [Reachability reachabilityWithHostName:@"google.com"];
    NetworkStatus remoteHostStatus = [reachability currentReachabilityStatus];

    if(remoteHostStatus == NotReachable)
    {
        isInternet =NO;
    }
    else if (remoteHostStatus == ReachableViaWWAN)
    {
        isInternet = TRUE;
    }
    else if (remoteHostStatus == ReachableViaWiFi)
    { isInternet = TRUE;

    }
    return isInternet;
}

isInternet is a BOOL declear in to your .h class

as per your code:-

dispatch_queue_t connectivityThread = dispatch_queue_create("com.GMM.assamkart.connectivity", NULL);

dispatch_async(connectivityThread, ^{
    while (true){
       isInternet =[self connectedToNetwork];
    if (isInternet)
    {
           NSLog(@"connected");
        }
        else
        {
            NSLog(@"Not connected");
        }
       // usleep(10000000);
    }
});
Nitin Gohel
  • 49,482
  • 17
  • 105
  • 144
-1
-(BOOL) connectedToInternet

{
    NSString *URLString = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.google.com"]];

return ( URLString != NULL ) ? YES : NO;

}
Taryn
  • 242,637
  • 56
  • 362
  • 405
AP_
  • 1,013
  • 12
  • 13
  • 2
    This code is synchronous so it would interrupt your main thread. You'd also have to download that webpage every time you wanted to check for internet connection. Definitely use Reachability instead. – olivaresF Dec 14 '13 at 23:34