How to check Internet connection in iOS apps?

Hi Coders,

Am going to explain about to manage the Internet check in Xcode projects. Apple providing the “Reachability” class to manage the Internet connection and connection status.

Here you can download the “Reachability” class files.

Follow the steps in Xcode project:

1. Import “Reachability” class files both .m and .h into the project

2. I have one Singleton class file named “MyAppManager”. There I Imported “Reachability.h” file

3. Create a  BOOL method like below,

    In .h file:

      + (BOOL) checkForInternetConnection;      

    In .m file:

  + (BOOL) checkForInternetConnection {

    Reachability *reachabilityObj = [Reachability reachabilityForInternetConnection];

    NetworkStatus status = [reachabilityObj currentReachabilityStatus];

    

    if (!status == NotReachable) {

        return YES;

    } else {

        return NO;

    }

}

4. In the ViewController class import “MyAppManager.h” file

5. Add the code like below to check the Internet connection,

 

if ([MyAppManager checkForInternetConnection]) {

NSLog(@“Internet connected”);

} else {

NSLog(@“No Internet Connection. Please check your Internet connection”);

}

6. Here we can manage the API Call or other stuffs.

Hope it will help you lot. Please share your comments and feedback. Thanks.

Happy Coding!

Yuvaraj Manickam