Easiest way to detect Internet connection on iOS? Easiest way to detect Internet connection on iOS? ios ios

Easiest way to detect Internet connection on iOS?


I did a little more research and I am updating my answer with a more current solution. I am not sure if you have already looked at it but there is a nice sample code provided by Apple.

Download the sample code here

Include the Reachability.h and Reachability.m files in your project. Take a look at ReachabilityAppDelegate.m to see an example on how to determine host reachability, reachability by WiFi, by WWAN etc. For a very simply check of network reachability, you can do something like this

Reachability *networkReachability = [Reachability reachabilityForInternetConnection];   NetworkStatus networkStatus = [networkReachability currentReachabilityStatus];    if (networkStatus == NotReachable) {            NSLog(@"There IS NO internet connection");        } else {             NSLog(@"There IS internet connection");        }

@BenjaminPiette's: Don't forget to add SystemConfiguration.framework to your project.


Seeing as this thread is the top google result for this type of question, I figured I would provide the solution that worked for me. I was already using AFNetworking, but searching didn't reveal how to accomplish this task with AFNetworking until midway through my project.

What you want is the AFNetworkingReachabilityManager.

// -- Start monitoring network reachability (globally available) -- //[[AFNetworkReachabilityManager sharedManager] startMonitoring];[[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {    NSLog(@"Reachability changed: %@", AFStringFromNetworkReachabilityStatus(status));    switch (status) {        case AFNetworkReachabilityStatusReachableViaWWAN:        case AFNetworkReachabilityStatusReachableViaWiFi:            // -- Reachable -- //            NSLog(@"Reachable");            break;        case AFNetworkReachabilityStatusNotReachable:        default:            // -- Not reachable -- //            NSLog(@"Not Reachable");            break;    }}];

You can also use the following to test reachability synchronously (once monitoring has started):

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


Sorry for replying too late but I hope this answer can help somebody in future.

Following is a small native C code snippet that can check internet connectivity without any extra class.

Add the following headers:

#include<unistd.h>#include<netdb.h>

Code:

-(BOOL)isNetworkAvailable{    char *hostname;    struct hostent *hostinfo;    hostname = "google.com";    hostinfo = gethostbyname (hostname);    if (hostinfo == NULL){        NSLog(@"-> no connection!\n");        return NO;    }    else{        NSLog(@"-> connection established!\n");        return YES;    }}

Swift 3

func isConnectedToInternet() -> Bool {    let hostname = "google.com"    //let hostinfo = gethostbyname(hostname)    let hostinfo = gethostbyname2(hostname, AF_INET6)//AF_INET6    if hostinfo != nil {        return true // internet available      }     return false // no internet    }