How to check internet is access or not when device connected with WIFI?

You can check internet is accessible or not when your device connected with WIFI with following code.
You need to import Reachability.h & Reachability .m in Appdelegate class and do following code in didFinishLaunchingWithOptions: function.

self.reachability = [Reachability reachabilityWithHostName:@”www.google.com”];
//self.reachability = [Reachability reachabilityForInternetConnection];

// Start Monitoring
[self.reachability startNotifier];

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(reachabilityChanged:)
name:kReachabilityChangedNotification
object:nil];

#pragma mark – Check Internet Network Reachability
-(void)reachabilityChanged:(NSNotification*)note
{

NetworkStatus remoteHostStatus = [self.reachability currentReachabilityStatus];

if (remoteHostStatus == NotReachable)
{
// Internet Connection not reachable
}
else if(remoteHostStatus == ReachableViaWiFi)  {
//Check Internet Connection Authenticated or Not when WIFI connected
[[[NSURLSession sharedSession] dataTaskWithURL:[NSURL URLWithString:@”http://google.com/”] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
{
NSHTTPURLResponse *HTTPResponse = (NSHTTPURLResponse *)response;
NSDictionary *dict = [HTTPResponse allHeaderFields];
NSLog(@”%@”,dict);
NSString *strAccess = [dict valueForKey:@”Accept-Ranges”];

if ([strAccess isEqualToString:@”none”]) {
NSLog(@”Device is Not connected to the Internet”);
}
else{

NSLog(@”Device is connected to the Internet”);
UIViewController *VC = self.window.rootViewController;

UINavigationController* navVc = (UINavigationController*) VC;

for (UIViewController *objVC in [navVc viewControllers])
{
if ([objVC isKindOfClass:[PrivateChatVC class]])
{
[self.delegate updateSendButton:YES];
//[[NSNotificationCenter defaultCenter] postNotificationName:@”sendButtonEnable” object:nil];
break;

}
}

}
}] resume];

}
else if (remoteHostStatus == ReachableViaWWAN)  {
}
}

You may also like

Leave a Reply