Check Internet Reachability with Reachability library

Reachability

Click here to Download Reachability Library

Drag and Drop Reachability in Xcode project

AppDelegate.h

#import “Reachability.h”

@property (nonatomic, retain) Reachability *reachabile;

AppDelegate.m

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

    

    Reachability *aReachable = [[Reachability alloc]init];

    self.reachabile  = aReachable;

    self.reachabile = [Reachability reachabilityWithHostName:@”apple.com”];

    [self.reachabile startNotifier];

    

    // Override point for customization after application launch.

    return YES;

}

Add new file in Xcode

iOS – Source – Cocoa Touch Class

Click Next

Class : Utility

Subclass of : NSObject

Utility.h

#import “AppDelegate.h”

#import “Reachability.h”

+ (BOOL)isNetworkReachable;

Utility.m

+ (BOOL)isNetworkReachable

{

    BOOL isReachable = NO;

    AppDelegate *aAppdelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;

    Reachability *netReach = [aAppdelegate reachabile];

    

    NetworkStatus netStatus = [netReach currentReachabilityStatus];

    BOOL isConnectionRequired = [netReach connectionRequired];

    

    if ((netStatus == ReachableViaWiFi) || ((netStatus == ReachableViaWWAN) && !isConnectionRequired))

    {

        isReachable = YES;

    }

    

    return isReachable;

}

ViewController.h

#import “Utility.h”

ViewController.m

if ([Utility isNetworkReachable])

    {

        NSLog(@”Network Reachable”);

    }

    else

    {

        NSLog(@”Network is not Rechable”);

    }

Click here to download Check Internet Reachability with Reachability library sample Project

Leave a comment