First you have download the Reachability class from below link.
Reachability Class
Then import the Reachability class in AppDelegate.h file.
#import Reachability.h
Please write the below code in AppDelegate.h file.
@property(nonatomic)Reachability *internetReachability;
@property(nonatomic)BOOL isInternet;
Note that APP_DELEGATE is an instance of AppDelegate and IS_INTERNET is an instance of isInternet variable which declared in AppDelegate.h file.
#define APP_DELEGATE ((AppDelegate *)[[UIApplication sharedApplication] delegate])
#define IS_INTERNET APP_DELEGATE.isInternet
After that just copy and paste the below code in your AppDelegate.m file and call the setupTheInternetConnection method in didFinishLaunchingWithOptions method.
-(void)setupTheInternetConnection {
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
//Setup Internet Connection
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
self.internetReachability = [Reachability reachabilityForInternetConnection];
[self.internetReachability startNotifier];
[self updateInterfaceWithReachability:self.internetReachability];
}
- (void) reachabilityChanged:(NSNotification *)note {
Reachability* curReach = [note object];
NSParameterAssert([curReach isKindOfClass:[Reachability class]]);
[self updateInterfaceWithReachability:curReach];
}
- (void) updateInterfaceWithReachability: (Reachability*) curReach {
if(curReach == self.internetReachability) {
NetworkStatus netStatus = [curReach currentReachabilityStatus];
switch (netStatus) {
case NotReachable: {
IS_INTERNET = FALSE;
break;
}
case ReachableViaWWAN: {
IS_INTERNET = TRUE;
break;
}
case ReachableViaWiFi: {
IS_INTERNET = TRUE;
break;
}
}
}
}
Now you can check the internet connection in any of your controller by using the value of IS_INTERNET.
Hope it will work for you.