Best Approach For Checking Internet Connection in iOS
<p>I have been searching through internet to find the best way to check for internet connection in iOS that works for both in IPv4 and IPv6 network environment. I found there are many possible answers but very confused on which one to apply. What I need to do is the following:</p>

<pre><code>   // Check for internet connection

if (internetConnectionIsAvailable) {
   // make rest calls
}else
   // show cached data
}
</code></pre>

<p>I found the following options that are suggested in internet.</p>

<p><strong>OPTION 1:</strong>
<strong>Use apple Reachability class</strong></p>

<p>With Reachability I can check internet connectivity as following which is short and direct, and don't have to wait for any response.</p>

<pre><code> - (BOOL)connected
    {
        Reachability *reachability = [Reachability reachabilityForInternetConnection];
        NetworkStatus networkStatus = [reachability currentReachabilityStatus];
        return !(networkStatus == NotReachable);
    }

- (void)viewDidLoad {
    [super viewDidLoad];

    if (![self connected]){
     // Not connected
    }else{
    // Connected
    }

}
</code></pre>

<p>But here internet checking is done at pre-flight, which is against suggestion by apple engineer as seen on <a href="https://developer.apple.com/videos/play/wwdc2015/719/" rel="noreferrer">WWDC video</a>. Also if you check ReadMe.md, it has been mentioned that Reachability fully supports IPv6. But if method <strong>reachabilityForInternetConnection</strong> is checked in Reachability.m they have used sockaddr_in, sockaddr_in6 has not been used which is recommended for IPv6 network. So I don't know if it will work in IPv6 network.</p>

<p><strong>OPTION 2: Use Alamofire to connect</strong> </p>

<ul>
<li><p>Just try to connect as in one of <a href="https://stackoverflow.com/questions/32185705/check-internet-connection-availability?answertab=votes#tab-top">stackoverflow answer</a>. The code also shown below:-</p>

<pre><code>Alamofire.request(.GET,"http://superrandomdomainnamethatisnotused.com/superrandompath").responseString {
    (request, response, stringData, error) in

    if let networkError = error {
        if (networkError.code == -1009) {
            println("No Internet \(error)")
        }
    }
}
</code></pre></li>
</ul>

<p>But isn't it time consuming to try to connect to some host, wait for the response and then know if online/offline. </p>

<p><strong>OPTION 3:</strong> Use Tony Million's version of <a href="https://github.com/tonymillion/Reachability" rel="noreferrer">Reachability</a>. But the author has warned of app being rejected while using this. I can use this as code shown below:-</p>

<pre><code>// Checks if we have an internet connection or not
- (void)testInternetConnection
{   
    internetReachableFoo = [Reachability reachabilityWithHostname:@"www.google.com"];

    // Internet is reachable
    internetReachableFoo.reachableBlock = ^(Reachability*reach)
    {
        // Update the UI on the main thread
        dispatch_async(dispatch_get_main_queue(), ^{
            NSLog(@"There in internet connection");
        });
    };

    // Internet is not reachable
    internetReachableFoo.unreachableBlock = ^(Reachability*reach)
    {
        // Update the UI on the main thread
        dispatch_async(dispatch_get_main_queue(), ^{
            NSLog(@"Someone broke the internet :(");
        });
    };

    [internetReachableFoo startNotifier];
}
</code></pre>

<p>But in this option, my question is, is this right approach to test internet by connecting <a href="http://google.com" rel="noreferrer">http://google.com</a> . Does this guarantee 100% results? What if google is down? If the app is used in country where google.com is not allowed whats the result? </p>

<p>So I have been in a big dilemma which option to choose, if these options are the right way or is there a better way of checking internet which guarantee 100% results and works in both IPv4 and IPv6 network. Can anyone suggest me which option is better or should I go for other approach, what is the better way to accomplish this. </p>

<p>Your reply will be much appreciated.</p>