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;
}
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];
}
}
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]);
}