I am trying to monitor Rechability in two ways. Connection to a host and connection to the internet. For either I am trying to display a UIAlertView. This works fine at the moment.
What I am trying to acheive is if the user clicks the alert view button for it to check reachability again, but I am not sure how to do this. Ideally I do not want the alertview to dismiss and show again but stay shown and simply request reachability check again. Is this possible?
This is my current appdelegate imp code, used in appdelegate as I want to check the connection at any point at the moment.
- (void)monitorReachability {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:ReachabilityChangedNotification object:nil];
self.hostReach = [Reachability reachabilityWithHostName: @"api.parse.com"];
[self.hostReach startNotifier];
self.internetReach = [Reachability reachabilityForInternetConnection];
[self.internetReach startNotifier];
}
//Called by Reachability whenever status changes.
- (void)reachabilityChanged:(NSNotification* )note {
NetworkStatus internetStatus = [self.internetReach currentReachabilityStatus];
switch (internetStatus) {
case NotReachable:
{
isReachable=NO;
break;
}
case ReachableViaWiFi:
{
NSLog(@"The internet is working via WIFI.");
isReachable=YES;
break;
}
case ReachableViaWWAN:
{
NSLog(@"The internet is working via WWAN.");
isReachable=YES;
break;
}
}
NetworkStatus hostStatus = [self.hostReach currentReachabilityStatus];
switch (hostStatus) {
case ReachableViaWWAN:
{
hostActive=YES;
break;
}
case ReachableViaWiFi:
{
hostActive=YES;
break;
}
case NotReachable:
{
hostActive=NO;
break;
}
}
if (hostActive == YES && isReachable == YES) {
connectionNotReachable.hidden = YES;
}
if (isReachable == NO) {
NSLog(@"Internet connection lost");\
connectionNotReachable = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"connectionNotReachableTitle", @"Title for alert view - connection Not Reachable") message:NSLocalizedString(@"connectionNotReachableMessage", @"Message for alert view - connection Not Reachable") delegate:self cancelButtonTitle:nil otherButtonTitles:NSLocalizedString(@"connectionNotReachableTryAgain", @"Try again button for alert view - connection Not Reachable"), nil];
[connectionNotReachable show];
return;
}
if (hostActive == NO) {
NSLog(@"Host not active, internet connection");
UIAlertView *hostNotReachable = [[UIAlertView alloc] initWithTitle:@"Host Not Reachable" message:@"No Host" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[hostNotReachable show];
return;
}
}
You can have custom AlertView in which you can implement the method like this
-(void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated {
if (![self connected])
return;
[super dismissWithClickedButtonIndex:buttonIndex animated:animated];
}
while your alert is up, you could check the connectivity yourself by this method
- (BOOL)connected
{
Reachability *reachability = [Reachability reachabilityForInternetConnection];
NetworkStatus networkStatus = [reachability currentReachabilityStatus];
return !(networkStatus == NotReachable);
}
finally see if([self connected])
then dismiss the button