4

There are loads of questions on here about finding an active internet connection in an app, but none work if you are on a 3g connection and you have no data credit, or if you are on a WiFi network at a hotel that automatically redirects to a log in page and you have yet to enter the password. That kind of situation.

What is the fastest way to check if the internet connection is actually operational?

jscs
  • 62,161
  • 12
  • 145
  • 186
Don Peters
  • 91
  • 7

2 Answers2

0

You can use AFNetworkReachabilityManager class in AFNetworking.

https://github.com/AFNetworking/AFNetworking

above link will help you out in setting things. This will provide you with continuous network check as per your requirement. Here is few line of code:

[[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
    NSLog(@"Reachability: %@", AFStringFromNetworkReachabilityStatus(status));
}];

[[AFNetworkReachabilityManager sharedManager] startMonitoring];
rmaddy
  • 298,130
  • 40
  • 468
  • 517
Ashwani
  • 44
  • 3
-1

The best way to accomplish this, not using Reachability or any other API, is by creating a NSURL and to try to receive data from it. For the bad connection I added a timeout of 20sec.

Like:

- (void)checkConnection //Run this on a side queue, never on a main queue
{    NSURL *checkURL = [NSURL URLWithString:@"http://www.apple.com"];

    NSURLRequest *lRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:checkURL]
                                              cachePolicy:NSURLRequestUseProtocolCachePolicy
                                          timeoutInterval:20.0];

    NSData *data = [NSData dataWithContentsOfURL:lRequest];

    if (data)
    {
        connected = YES;
    }
    else
    {
        connected = NO;
    }
}

That would check if a specific website is reachable and set a timeout which indicates a slow network connection.

This code could have errors, as I haven't used/tried it yet! Comment this answer if there are any.

MasterRazer
  • 1,367
  • 3
  • 16
  • 40
  • And why is reachability _not_ the best way? You should really back that comment up with some profound evidence. What happens when `apple.com` no longer has a valid server/host. There goes two weeks waiting for an update to be approved – soulshined Sep 06 '15 at 13:43
  • @soulshined I didn't mean that it is the best way. It is probably the best way if you are not using any API. Furthermore Apple.com has a uptime of 97% which is high. I would recommend using Google.com instead, since they got a 99.9% uptime which is almost anytime. – MasterRazer Sep 06 '15 at 15:04
  • 1
    The point is that you shouldn't hard code an address like that when Reachability is at your fingertips. Addresses change, even if you don't use Apple.com, servers go down etc, Maybe saying 'the easiest way' is more accurate, this solution, definetly not the best. Reachability also contains notifications that updates your delegate when network changes, instead of manually looking to see if network changes – soulshined Sep 06 '15 at 15:11