【问题标题】:Working with iOS Reachability to display a UIAlertView使用 iOS Reachability 显示 UIAlertView
【发布时间】:2013-02-28 14:27:13
【问题描述】:

我正在尝试设置 Reachability 以在 Internet 连接丢失时显示警报视图。

我很难理解可达性实际上是如何工作的。我已经阅读了documentation 并设置了示例应用程序以供 Apple 审查,但我想更好地理解代码及其工作原理。

在我的设置中,我正在寻找两件事: 1. 网络连接中断时告诉我 2. 与主机地址的连接丢失时告诉我

代码是在我的应用程序委托中设置的,因为我希望 UIAlertView 在连接丢失时出现在应用程序内的任何位置。

对于场景 1,我希望显示一个 UIAlertView 来向用户发送消息说连接丢失。单击时将出现一个“重试”按钮,然后再次测试连接以查看 Internet 连接是否已恢复,如果没有再次显示警报视图。

代码: 应用委托 imp 文件:

//Called by Reachability whenever status changes.
- (void)reachabilityChanged:(NSNotification* )note {
Reachability *curReach = (Reachability *)[note object];
NSParameterAssert([curReach isKindOfClass: [Reachability class]]);
NSLog(@"Reachability changed: %@", curReach);
networkStatus = [curReach currentReachabilityStatus];

if (networkStatus == NotReachable) {
    NSLog(@"NOT REACHABLE");\
    UIAlertView *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;
} else {
    NSLog(@"REACHABLE");
}
}

- (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];

}

困惑:我不明白这段代码实际上是如何工作的。所以在我的application didFinishLaunchingWithOptions: 中,我打电话给[self monitorReachability]

我猜这会设置 hostReach 和 internetReach 的通知程序。但是在 reachabilityChanged 方法中,我如何确定这是 hostReach == NotReachable 还是 internetReach == Not Reachable 之间的区别。

理想情况下,对于无法访问的 hostReach,我目前不想做太多事情,因为这可能是当时无法访问这个特定的 api。所以我不想在那段时间完全使应用程序无法访问。

【问题讨论】:

    标签: ios ios5 uialertview reachability


    【解决方案1】:

    您的应用程序将监听kReachabilityChangedNotification 并在网络状态发生变化时提示您。 您注册这样的通知

     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityHasChanged:) name:kReachabilityChangedNotification object:nil];
    
        self.internetReachable = [[Reachability reachabilityForInternetConnection] retain];
        [self.internetReachable startNotifier];
    
    
    // check if a host is reachable
    self.hostReachable= [Reachability reachabilityWithHostname:@"www.google.com"];
    [self.hostReachable startNotifier];
    

    基本上,您可以在开关盒内设置一个全局标志,例如BOOL isReachable,并根据网络可达性状态进行更新。在您的应用程序中,无论您身在何处,都可以检查全局标志 if(!isReachable) 然后显示警报

    -(void) reachabilityHasChanged:(NSNotification *)notice
    {
        // called after network status changes
        NetworkStatus internetStatus = [self.internetReachable currentReachabilityStatus];
        switch (internetStatus)
        {
            case NotReachable:
            {
              UIAlertView *alert =[[UIAlertView alloc]initWithTitle:@"Errot" message:@"internet not reachable" delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
             [alert show];
             isReachable=NO;
    
                break;
            }
            case ReachableViaWiFi:
            {
              NSLog(@"The internet is working via WIFI.");
              UIAlertView *alert =[[UIAlertView alloc]initWithTitle:@"Info" message:@"WIFI reachable" delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
              [alert show];
              isReachable=YES;
    
                break;
            }
            case ReachableViaWWAN:
            {
              NSLog(@"The internet is working via WWAN.");
              UIAlertView *alert =[[UIAlertView alloc]initWithTitle:@"Info" message:@"WWAN/3G" delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
              [alert show];
              isReachable=YES;
               break;
            }
        }
    
      NetworkStatus hostStatus = [self.hostReachable currentReachabilityStatus];
    
    
    switch (hostStatus)
    {
        case ReachableViaWWAN:
        {
          //NSLog(@"3G");
    
          hostActive=YES;
          break;
        }
        case ReachableViaWiFi:
        {
    
          // NSLog(@"WIFI");
    
          hostActive=YES;
          break;
        }
        case NotReachable:
        {
    
          hostActive=NO;
    
          break;
        }
    
      }
    
    
     }
    

    如果您在开始使用 Reachability 时遇到问题,那么只需 download the sample code for Reachability

    如果您查看Reachability 类中的reachabilityForInternetConnection 方法,您会发现它通常检查互联网连接,而不关注特定主机,它只检查互联网网关是否可访问.

       + (Reachability*) reachabilityForInternetConnection;
    
        {
    
            struct sockaddr_in zeroAddress;
    
            bzero(&zeroAddress, sizeof(zeroAddress));
    
            zeroAddress.sin_len = sizeof(zeroAddress);
    
            zeroAddress.sin_family = AF_INET;
    
            return [self reachabilityWithAddress: &zeroAddress];
    
        }
    

    【讨论】:

    • 谢谢,我确实提到我在我的问题中设置了 Apple 的示例应用程序。我的困难在于理解互联网可达和主机可达之间的区别。以上所有内容实际上都在我的问题之内,只是与您使用不同互联网连接状态的案例略有不同......
    • 是的 Internet 可访问意味着 Apple 默认将主机可访问设置为 LAN/Internet 网关,但您可以通过将其设置为自定义主机来设置您自己的自定义主机可访问。
    • 上面的代码你用了吗?将标签正确连接到插座?
    • 好的,那么我如何获取无法访问的 hostReach?
    • 更新了应答码,可以注册主机,查看类似网络连接的状态。你可以看到我的答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-17
    • 1970-01-01
    • 1970-01-01
    • 2011-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多