0

I am trying to detect the internet is working or not on the device using AFNetworking ; referred to the answer here on the SO AFNetworking 2.0 Reachability . I am able to detect device is connected with WiFi or 3G..

The problem is Actually internet is not working like the WIFI is on but internet is not working.

How Can i detect internet is working .Like we check using ping....

I am checking using the following

-(BOOL)connected {

  __block BOOL reachable;

// Setting block doesn't means you area running it right now
[[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {

    switch (status) {
        case AFNetworkReachabilityStatusNotReachable:
            NSLog(@"No Internet Connection");
            reachable = NO;
            break;
        case AFNetworkReachabilityStatusReachableViaWiFi:
            NSLog(@"WIFI");

            reachable = YES;
            break;
        case AFNetworkReachabilityStatusReachableViaWWAN:
            NSLog(@"3G");
            reachable = YES;
            break;
        default:
            NSLog(@"Unkown network status");
            reachable = NO;
            break;

    }
}];
// and now activate monitoring
[[AFNetworkReachabilityManager sharedManager] startMonitoring];

return reachable;

}

Community
  • 1
  • 1
hii
  • 255
  • 4
  • 18
  • 1
    If firewall loging required than u cant access internet in case of device is connected to wifi or 3g. – Er.Shreyansh Shah Apr 17 '15 at 11:37
  • No login required in my case ,But Even if login required i need to check is i am able to ping say google.com from device when the AFNetworkReachabilityStatusReachableViaWiFi is found to be TRUE – hii Apr 17 '15 at 11:40
  • You can check the same question : http://stackoverflow.com/questions/15146607/detect-internet-connectivity-status-not-just-wifi-ios – Nitin Gohel Apr 17 '15 at 12:06

2 Answers2

1

Add notification observer for change in network status. Like add following code in app delegate class under finish launching class

 [[AFNetworkReachabilityManager sharedManager] startMonitoring];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveNetworkChnageNotification:) name:AFNetworkingReachabilityDidChangeNotification object:nil];

Add global alertview in .h file. as it will appear and disappear on network status change.

here is notification function called on :

- (void)receiveNetworkChnageNotification:(NSNotification *)notification
{
    if (![self isReachable])
    {
        self.alertView = [[UIAlertView alloc] initWithTitle:@"No Internet Connection" message:@"You have no internet connection on your device, please check and try again later." delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
        [self.alertView show];
    }
    else
    {
        [self.alertView dismissWithClickedButtonIndex:0 animated:YES];
    }
}
Kamal Bhardwaj
  • 948
  • 1
  • 10
  • 25
0

You can use Reachability to determine if you have a functioning connection to a network. However if you really need to determine for some reason that you have a viable internet connection, then the best way is to attempt to establish an actual connection and examine the results. Use NSURLConnection for that.

NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.apple.com/"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
NSMutableData *receivedData = [NSMutableData dataWithCapacity:0];
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

if(!theConnection) { // Something went wrong.
    receivedData = nil;
}

To determine if you established a viable connection, examine the result of the connection using the the delegate method:

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@"Connection failed! Error - %@ %@", [error localizedDescription],
        [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
}
Cliff Ribaudo
  • 8,932
  • 2
  • 55
  • 78