1

我正在尝试设置可达性以在与 Internet 的连接丢失时显示警报视图。

我正在努力理解可达性实际上是如何工作的。我已阅读文档并设置示例应用程序以从 Apple 审查,但我想更好地理解代码及其工作方式。

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

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

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

代码:App Delegate 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。所以我不想在那段时间完全使应用程序无法访问。

4

1 回答 1

4

每当网络状态发生变化时,您的应用程序都会监听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 时遇到问题,那么只需下载 Reachability 的示例代码

如果您查看课堂上的方法reachabilityForInternetConnectionReachability您会发现它通常检查互联网连接,而不关注特定主机,它仅检查互联网网关是否可访问。

   + (Reachability*) reachabilityForInternetConnection;

    {

        struct sockaddr_in zeroAddress;

        bzero(&zeroAddress, sizeof(zeroAddress));

        zeroAddress.sin_len = sizeof(zeroAddress);

        zeroAddress.sin_family = AF_INET;

        return [self reachabilityWithAddress: &zeroAddress];

    }
于 2013-03-12T21:03:57.213 回答