Jenya Kyrmyza
Jenya Kyrmyza

Reputation: 327

internet connection reachability using afnetworking

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?

Upvotes: 0

Views: 591

Answers (2)

Jenya Kyrmyza
Jenya Kyrmyza

Reputation: 327

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);
     }];

}

Upvotes: 0

Lou Franco
Lou Franco

Reputation: 89242

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.

Upvotes: 2

Related Questions