Check Internet Reachability

ViewController.m

#import “Utility.h”

#import “ViewController.h”

@interface ViewController ()

@end

@implementation ViewController

– (void)viewDidLoad {

    [super viewDidLoad];

    

    if ([Utility isNetworkReachable])

    {

        NSLog(@”Network Reachable”);

    }

    else

    {

        NSLog(@”Network is not Rechable”);

    }

    

    

    

    // Do any additional setup after loading the view, typically from a nib.

}

– (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end

Utility.h

#import <Foundation/Foundation.h>

#import “AppDelegate.h”

#import “Reachability.h”

@interface Utility : NSObject

+ (BOOL)isNetworkReachable;

@end

Utility.m

#import “Utility.h”

@implementation Utility

+ (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;

}

@end

Download Sample  Project From Github

Leave a comment