1

到達可能性についての投稿を見ましたが、人々は実際には問題に正確な答えを与えていません。私のアプリケーションでは、アップルのReachabilityコードを使用し、appDelegateでは次のコードを使用しています。


-(BOOL)checkInternet {

Reachability *reachability = [Reachability reachabilityWithHostName:@"www.google.com"];

NetworkStatus internetStatus = [reachability currentReachabilityStatus];
BOOL internet;

if ((internetStatus != ReachableViaWiFi) && (internetStatus != ReachableViaWWAN)) {
    internet = NO;
}else {
    internet = YES;
}
return internet;    
}

したがって、問題は、インターネットに接続している場合でも、このコードはインターネットに接続していないことを示しています。誰かがこれを機能させるために何をすべきか知っていますか?

ありがとう、

4

2 に答える 2

7

おそらく+[Reachability reachabilityForInternetConnection]、特定の名前の到達可能性ではなく、使用する必要があります(もちろん、それが実際に必要な場合を除きます)。

結局のところ、インターネット接続が機能しているときに特定のサーバーにアクセスできない理由はさまざまです。

これが私がすることです:

BOOL hasInet;
Reachability *connectionMonitor = [Reachability reachabilityForInternetConnection];
[[NSNotificationCenter defaultCenter]
    addObserver: self
    selector: @selector(inetAvailabilityChanged:)
    name:  kReachabilityChangedNotification
    object: connectionMonitor];

hasInet = [connectionMonitor currentReachabilityStatus] != NotReachable;

その後

-(void)inetAvailabilityChanged:(NSNotification *)notice {
    Reachability *r = (Reachability *)[notice object];
    hasInet = [r currentReachabilityStatus] != NotReachable;
}

これは私にとってうまく機能します。

于 2010-04-19T07:59:42.580 に答える
0

このコードを使用して、デバイスがインターネットに接続されているかどうかを確認します

viewDidLoadでこのコードを使用します:

 Reachability* internetReachable; = [Reachability reachabilityForInternetConnection];
    [internetReachable startNotifier];

    hostReachable = [Reachability reachabilityWithHostName: @"www.apple.com"] ;
    [hostReachable startNotifier];

この関数をコードに追加します。

-(void) checkNetworkStatus:(NSNotification *)notice
{
    recheabilityBool=FALSE;
    nonrecheabilityBool=FALSE;
    // called after network status changes
    NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
    switch (internetStatus)
    {
        case NotReachable:
        {
            nonrecheabilityBool=TRUE;
            internetCon=0;
            //NSLog(@"The internet is down.");


            break;
        }
        case ReachableViaWiFi:
        {
            NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
            internetCon=404;
            [prefs setInteger:internetCon forKey:@"conKey"];

            //NSLog(@"The internet is working via WIFI.");
            break;

        }
        case ReachableViaWWAN:
        {
            //NSLog(@"The internet is working via WWAN.");

            break;
        }
    }

    NetworkStatus hostStatus = [hostReachable currentReachabilityStatus];
    switch (hostStatus)
    {
        case NotReachable:
        {
            internetCon=0;
            if( nonrecheabilityBool==FALSE)
            {
                //NSLog(@"A gateway to the host server is down.");
            }
            break;

        }
        case ReachableViaWiFi:
        {
            if(recheabilityBool==FALSE)
            {

                recheabilityBool=TRUE;

                NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
                internetCon=404;
                [prefs setInteger:internetCon forKey:@"conKey"];


                //NSLog(@"The internet is working via WIFI.");
                break;
            }


            //NSLog(@"A gateway to the host server is working via WIFI.");

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

- (BOOL)connected
{
    Reachability *reachability = [Reachability reachabilityForInternetConnection];
    NetworkStatus networkStatus = [reachability currentReachabilityStatus];
    return !(networkStatus == NotReachable);
}
于 2013-01-03T05:46:17.880 に答える