0

我想在我的应用程序中检查互联网连接。在这样做时,我使用以下代码。internetStatus 和主机状态有什么区别。我应该使用哪个来检查 ipad 是否有互联网连接。

-(void) checkNetworkStatus:(NSNotification *)notice
{
    // called after network status changes
    NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
    switch (internetStatus)
    {
        case NotReachable:
        {
            //NSLog(@"The internet is down.");
            //self.internetActive = NO;
            //NSLog(@"A gateway to the host server is down.");
            //self.hostActive = NO;
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Connection Failed"
                                                            message:@"No internet connection"
                                                           delegate:nil
                                                  cancelButtonTitle:@"Exit"
                                                  otherButtonTitles:nil];
            [alert setDelegate:self];
            [alert show];
            [alert release];


            break;
        }
        case ReachableViaWiFi:
        {
            //NSLog(@"The internet is working via WIFI.");
            //self.internetActive = YES;

            break;
        }
        case ReachableViaWWAN:
        {
            //NSLog(@"The internet is working via WWAN.");
            //self.internetActive = YES;

            break;
        }
    }

    NetworkStatus hostStatus = [hostReachable currentReachabilityStatus];
    switch (hostStatus)
    {
        case NotReachable:
        {
            //NSLog(@"A gateway to the host server is down.");
            //self.hostActive = NO;
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Connection Failed"
                                                              message:@"No internet connection"
                                                             delegate:nil
                                                    cancelButtonTitle:@"Exit"
                                                    otherButtonTitles:nil];
            [alert setDelegate:self];
            [alert show];
            [alert release];

            break;
        }
        case ReachableViaWiFi:
        {
            //NSLog(@"A gateway to the host server is working via WIFI.");
            //self.hostActive = YES;

            break;
        }
        case ReachableViaWWAN:
        {
            //NSLog(@"A gateway to the host server is working via WWAN.");
            //self.hostActive = YES;

            break;
        }
    }
}
4

1 回答 1

1

internetStatus或者hostStatus只是实例名称,您可以使用任何名称来表示状态。但特别是如果您没有设置主机,那么您可以使用internetStatus它来访问互联网(如果它可以访问),默认情况下,苹果会检查互联网网关或互联网连接的互联网可用性,在这种情况下,我们不知道主机名检查互联网连接,但是您可以使用hostStatus特定主机的任何实例名称,您可以自己设置随机主机,例如www.google.com并检查它是否可访问。程序类似。

基本上状态取决于您的主机名,可以是默认的,也可以像这样自己设置;

hostReachable = [Reachability reachabilityWithHostName: @"www.google.com"];
[hostReachable startNotifier];
于 2013-02-21T20:16:40.663 回答