I am working on an application and i have to check internet connectivity. I am using two files Reachability.h and Reachability.m. Now i have to show an alert when internet is not working.
But when my observer check the internet it shows alert view more then one time. and some times it shows the alert more 9-10 times.
my code is here:
in .h file
Reachability* internetReachable;
Reachability* hostReachable;
NetworkStatus internetStatus;
in .m file
internetReachable = [[Reachability reachabilityForInternetConnection] retain];
[internetReachable startNotifier];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkNetworkStatus:) name:kReachabilityChangedNotification object:nil];
hostReachable = [[Reachability reachabilityWithHostName: @"www.apple.com"] retain];
[hostReachable startNotifier];
- (void) checkNetworkStatus:(NSNotification *)notice
{
internetStatus = [internetReachable currentReachabilityStatus];
if (internetStatus != NotReachable){
NSLog(@"internet is on");
}
else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error!" message:@"Server not connected or down!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
internetCount++;
}
}
this code is in delegate and similarly i am using this code in other classes where ever i have to check internet
plz help
I don’t use Reachability because I’ve had problems like you, so I created my own implementation. Just import the header file below and then call
[self networkIsReachable]to check for an internet connection.