Using Reachability to test for Internet Connectivity IOS

Any apps that you build today will require some form of internet connectivity.

Today’s app is getting more and more intuitive and should have the ability to check on the internet connective and prompt the user of the app.

To do that we will be using the following library, Reachability. https://github.com/tonymillion/Reachability

There are 2 ways to setup the app in your Xcode project.

Part 1 : Getting Reachability to your project

1. Manual

1.1 Check out a copy from the above github url, add the Reachability.h/.m file to your project

2. Cocoapod

2.1 Add the following line to your pod file, pod ‘Reachability’

2.2 Run pod install on terminal.

 

Part 2: Setting up the SystemConfiguration Framework.

1. Go to the Project -> Targets -> Build Phases tab.

2. Add SystemConfiguration.Framework into your project.

 

Part 3: Codes to set up Reachability to test internet connectivity.

AppDelegate.h 

Import the Reachability.h, at the very top of the file.

#import “Reachability.h”

AppDelegate.m

Within the method,

– (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

Add the following codes

 Reachability *reachability = [ReachabilityreachabilityWithHostname:@”http://localhost:3000/”];

    

    reachability.reachableBlock = ^(Reachability *reachability) {

        NSLog(@”Network is reachable.”);

    };

    

    reachability.unreachableBlock = ^(Reachability *reachability) {

        NSLog(@”Network is unreachable.”);

    };

    

    // Start Monitoring

    [reachability startNotifier];

Leave a Reply

Your email address will not be published. Required fields are marked *