0

Possible Duplicate:
Check for internet connection - iOS SDK

I'm searching the fastest and simplest way for check the connection in iOS.

I've found this:

-(BOOL)connectedToNetwork  {
    NSURL* url = [[NSURL alloc] initWithString:@"http://google.com/"];
    NSData* data = [NSData dataWithContentsOfURL:url];
    if (data != nil)
        return YES;
    return NO;
}

Do you know if is there something even simpler???

Guys, thanks for all the answers, but I'm searching for the simplest, lightest, solution, not for the best one (i.e. distinction between 3G/Wi-Fi is not needed, I'm only searching a YES/NO reach for a website)

Community
  • 1
  • 1
napolux
  • 15,574
  • 9
  • 51
  • 70
  • 1
    Checking Internet connection with website is not recommended. Because even checking with google. In future google site alone can go down(who knows ;)) but you will have internet connection. So use rechability code to check internet conn. its recommended by apple. Check my answer. – ipraba Jan 25 '13 at 06:21

6 Answers6

4

Take a look at the Reachability Example provided by Apple.

The problem your approach may have is that you could have a timeout and thus, the synchronized download of some data may block your app. As a result Apple may reject your app.

The Reachability Example can be used as follows:

Reachability *_reachability = [Reachability reachabilityForInternetConnection];
NetworkStatus remoteHostStatus = [_reachability currentReachabilityStatus];
if (remoteHostStatus == NotReachable) {
    // not reachable
} else if (remoteHostStatus == ReachableViaWiFi) {
    // reachable via Wifi
} else if (remoteHostStatus == ReachableViaWWAN) {
    // reachable via WWAN
}
who9vy
  • 1,091
  • 9
  • 19
2

I suggest dont go with this approach. I faced a rejection of one of my app because of this code. Instead go with Apple's Reachability Classes.

Ankur
  • 5,086
  • 19
  • 37
  • 62
2

Use this code to check whether the device is connected to internet or not

use this code in viewDidLoad :

 Reachability* internetReachable; = [Reachability reachabilityForInternetConnection];
    [internetReachable startNotifier];

    hostReachable = [Reachability reachabilityWithHostName: @"www.apple.com"] ;
    [hostReachable startNotifier];

and add this function to your code:

-(void) checkNetworkStatus:(NSNotification *)notice
{
    recheabilityBool=FALSE;
    nonrecheabilityBool=FALSE;
    // called after network status changes
    NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
    switch (internetStatus)
    {
        case NotReachable:
        {
            nonrecheabilityBool=TRUE;
            internetCon=0;
            //NSLog(@"The internet is down.");


            break;
        }
        case ReachableViaWiFi:
        {
            NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
            internetCon=404;
            [prefs setInteger:internetCon forKey:@"conKey"];

            //NSLog(@"The internet is working via WIFI.");
            break;

        }
        case ReachableViaWWAN:
        {
            //NSLog(@"The internet is working via WWAN.");

            break;
        }
    }

    NetworkStatus hostStatus = [hostReachable currentReachabilityStatus];
    switch (hostStatus)
    {
        case NotReachable:
        {
            internetCon=0;
            if( nonrecheabilityBool==FALSE)
            {
                //NSLog(@"A gateway to the host server is down.");
            }
            break;

        }
        case ReachableViaWiFi:
        {
            if(recheabilityBool==FALSE)
            {

                recheabilityBool=TRUE;

                NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
                internetCon=404;
                [prefs setInteger:internetCon forKey:@"conKey"];


                //NSLog(@"The internet is working via WIFI.");
                break;
            }


            //NSLog(@"A gateway to the host server is working via WIFI.");

            break;
        }
        case ReachableViaWWAN:
        {
            //NSLog(@"A gateway to the host server is working via WWAN.");
            break;
        }
    }
}


- (BOOL)connected
{
    Reachability *reachability = [Reachability reachabilityForInternetConnection];
    NetworkStatus networkStatus = [reachability currentReachabilityStatus];
    return !(networkStatus == NotReachable);
}
Nithinbemitk
  • 2,710
  • 4
  • 24
  • 27
1
Reachability* reachability = [Reachability sharedReachability];
[reachability setHostName:@"www.example.com"];    // set your host name here
NetworkStatus remoteHostStatus = [reachability remoteHostStatus];

How to check for an active Internet connection on iOS or OSX?

Community
  • 1
  • 1
asukharev
  • 477
  • 6
  • 12
  • Yes, we shouldn't use "www.apple.com" , we should use our respective host names. If apple.com not reachable that doesn't mean that ur app is not reachable to our services. We should check with our host names only if available. – arango_86 Aug 21 '17 at 10:04
0

As @who9vy said use Reachability Example

Import the Two classes Reachability.h and Reachability.m into your project

Use method to check the Internet Connection

- (BOOL)isConnectedToInternet
{
    Reachability *reachability = [Reachability reachabilityForInternetConnection]; 
    NetworkStatus networkStatus = [reachability currentReachabilityStatus];
    return !(networkStatus == NotReachable);
}
ipraba
  • 16,485
  • 4
  • 59
  • 58
0

The best way to check reachability is Apple Rechability class

Check this link

Hope it helps you..

P.J
  • 6,547
  • 9
  • 44
  • 74