0

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;
    }
}
StuartM
  • 6,743
  • 18
  • 84
  • 160

2 Answers2

1

Basically, what you could do, is disable the UIAlertView button until you have access to the network again.

[alert addButtonWithTitle:@"OK"];
UIButton *submitButton = [[alert subviews] lastObject];
[submitButton setEnabled: … ];

And after your Connection is back, you just do the same thing again :)

Daij-Djan
  • 49,552
  • 17
  • 113
  • 135
bujarm
  • 9
  • 1
  • 1
    You offered a better solution, but it doesn't mean that this isn't what he wants... – bujarm Mar 14 '13 at 09:45
  • +1 for the alternate option, i like your thought. Although the intent was to have the button as Try Again... – StuartM Mar 14 '13 at 09:58
0

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

nsgulliver
  • 12,655
  • 23
  • 43
  • 64
  • Could you elaborate on the above please, I get an error implementing this. Is there something else I should setup if I have not understood this right. – StuartM Mar 14 '13 at 19:10
  • from what I understand I have to create another file subclass of UIAlertView, then add the first method. However, where does the connected method above go? If the first method is calling self connected does it not have to be in the same file (UIAlertView subclass)? Then I have to update my code to be connectionNotReachable = [[SubclassedUIAlert alloc... right? – StuartM Mar 14 '13 at 19:24
  • you could place anywhere, purpose of this method is to check the internet reachability status. and give you YES or NO depending on the status! – nsgulliver Mar 14 '13 at 19:30
  • If I create another file which is a subclass of UIAlertView then I can copy your first part in, however it does not work having [self connected] as it is looking within that file for the connected method? So is there something else I should be doing here? – StuartM Mar 16 '13 at 13:21
  • You could create connected method in the class where you are calling alert view also, you could check if connected then dismiss the alertview otherwise don't dismiss it. – nsgulliver Mar 16 '13 at 13:51
  • if i put the connected method in my subclass file i get an error - use of undeclared identifier networkStatus. If I try and put @implementation in the app delegate for my subclass of uialertview I get an error no visible interface for 'thesubclass' implements the selector 'connected'? – StuartM Mar 16 '13 at 13:52
  • Just for clarification, I am creating the UIAlertView in the AppDelegate. I have made a new file which is a subclass of UIAlertView called MESUIConnectionAlertView. I have edited the question with the code I am using to create the alert view if this helps – StuartM Mar 16 '13 at 13:55
  • If I would be in your situation, I would show the AlertView and then check the network reachability. if reachability changes then dismiss the alertview otherwise keep showing it. – nsgulliver Mar 16 '13 at 14:07