0

I check internet connection following way.

in viewDidload

  [[AFNetworkReachabilityManager sharedManager] startMonitoring];

then

   - (BOOL)connected {
        return [AFNetworkReachabilityManager sharedManager].reachable;
    }

But even if i don't have internet connection but 3g is on, it still returns true.

How can i detect if the real internet connection exists?

Jenya Kyrmyza
  • 327
  • 1
  • 5
  • 16

2 Answers2

2

Reachability being true doesn't mean that the next network access you do will succeed -- you need to assume that network access can always fail.

It's good at letting you know the user has turned off network access (like Airplane mode), but if you are on a bad network, dropping lots of packets, then Reachability will still return true. It should also detect if you can't get any Wifi or 3G at all. But, if you have one bar -- it's going to return true, even if that means that network access won't really work.

Lou Franco
  • 87,846
  • 14
  • 132
  • 192
  • There are tradeoffs (1) you are using the user's bandwidth (2) your network request can still fail (3) you might not be able to ping google, but your network request would have succeeded -- probably timing. – Lou Franco Feb 12 '16 at 14:40
  • Reachability is trying to make the best tradeoff it can about using the users battery and bandwidth. You must still always assume network requests could fail. A heartbeat back to your server is something people do, but keep the tradeoffs in mind. – Lou Franco Feb 12 '16 at 14:42
  • anyway i'm handling failure, so checking google ping is fine for me – Jenya Kyrmyza Feb 12 '16 at 14:58
0

I did it this way. I know it's no elegant, but...

+ (void)checkInternet:(connection)block
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

    NSURL *url = [NSURL URLWithString:@"http://www.google.com/"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    request.HTTPMethod = @"HEAD";
    request.cachePolicy = NSURLRequestReloadIgnoringLocalAndRemoteCacheData;
    request.timeoutInterval = 10.0;

    [NSURLConnection sendAsynchronousRequest:request
                                       queue:[NSOperationQueue mainQueue]
                           completionHandler:
     ^(NSURLResponse *response, NSData *data, NSError *connectionError)
     {
         [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
         block([(NSHTTPURLResponse *)response statusCode] == 200);
     }];

}
Jenya Kyrmyza
  • 327
  • 1
  • 5
  • 16