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は、到達可能かどうかに関係なくインターネットアクセスに使用できます。デフォルトでは、アップルはインターネットゲートウェイまたはインターネット接続へのインターネットの可用性をチェックします。この場合、インターネット接続を確認しますが、特定のホストに任意のインスタンス名を使用できますが、 www.google.comhostStatusなどのランダムなホストを自分で設定して、到達可能かどうかを確認できます。手順も同様です。

基本的に、ステータスはホスト名によって異なります。ホスト名はデフォルトにすることも、自分で次のように設定することもできます。

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