0

我试图以两种方式监控 Rechability。连接到主机并连接到 Internet。对于任何一个我都在尝试显示 UIAlertView。目前这工作正常。

我想要实现的是,如果用户单击警报视图按钮以再次检查可达性,但我不确定如何执行此操作。理想情况下,我不希望 alertview 再次关闭并显示,而是保持显示并再次请求可达性检查。这可能吗?

这是我当前的 appdelegate imp 代码,在 appdelegate 中使用,因为我想随时检查连接。

- (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;
    }
}
4

2 回答 2

1

基本上,您可以做的是禁用 UIAlertView 按钮,直到您再次可以访问网络。

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

在您的 Connection 恢复后,您只需再次执行相同的操作 :)

于 2013-03-14T09:34:02.160 回答
0

您可以拥有自定义的 AlertView ,您可以在其中实现这样的方法

  -(void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated {
       if (![self connected])
          return;
       [super dismissWithClickedButtonIndex:buttonIndex animated:animated];
    }

当您的警报启动时,您可以通过此方法自己检查连接

- (BOOL)connected 
{
    Reachability *reachability = [Reachability reachabilityForInternetConnection];  
    NetworkStatus networkStatus = [reachability currentReachabilityStatus]; 
    return !(networkStatus == NotReachable);
}

终于看到if([self connected])然后关闭按钮

于 2013-03-14T09:39:05.553 回答