-5

I have one problem - i need to check really internet connection. What i do now? Now i use Reachability class to check if is internet connection, but my app go to crash in this case: I've connected to the wifi in airport, it's was free wi fi but if i am not registred i can't surf. But Reachability thinkin that i've internet connection.How i use Reachability now?

if(self.internetReachableFoo.isReachable)
{
// We have internet          
}
else
{
//we have no internet
}

So my question is, how i can use Reachability or other tools to check real internet connection? thanks.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Boris Kuzevanov
  • 1,232
  • 1
  • 12
  • 21
  • 2
    Please refrain from using **annoying formatting**. – dandan78 Jan 22 '15 at 12:22
  • Try taking a look at this existing question: http://stackoverflow.com/questions/1083701/how-to-check-for-an-active-internet-connection-on-iphone-sdk?rq=1 – narner Jan 22 '15 at 14:26

3 Answers3

2

I you wan't to check internet connectivity then you can use reachability block

Reachability *reachability = [Reachability reachabilityWithHostname:@"www.google.com"];

reachability.reachableBlock = ^(Reachability *reachability) {
    NSLog(@"Network is reachable.");
};

reachability.unreachableBlock = ^(Reachability *reachability) {
    NSLog(@"Network is unreachable.");
};

// Start Monitoring
[reachability startNotifier];
Muhammad Waqas Bhati
  • 2,775
  • 20
  • 25
  • Maybe because the network will let you ping but redirects regular traffic such as that on port 80 and 443 to a proxy. I will add that I don't know how reachability works. – nickdnk Jan 22 '15 at 12:31
0

You could attempt to connect to services you know will always be available, such as Google's public DNS servers. A simple ping will suffice.

nickdnk
  • 4,010
  • 4
  • 24
  • 43
0

I'm using this solution:

@implementation SomeClass{

    Reachability* hostReachable;
}

Add this to your viewDidLoad method

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkNetworkStatus:) name:kReachabilityChangedNotification object:nil];

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

And this method:

-(void) checkNetworkStatus:(NSNotification *)notice

{

    NetworkStatus hostStatus = [self.hostReachable currentReachabilityStatus];
    switch (hostStatus)
    {
        case NotReachable:
        {
            NSLog(@"NotReachable");
            break;
        }
        case ReachableViaWiFi:
        {
            NSLog(@"ReachableViaWiFi");
            break;
        }
        case ReachableViaWWAN:
        {
            NSLog(@"ReachableViaWWAN");
            break;
        }
    }
}
Bogdan Matveev
  • 518
  • 2
  • 14