1

我看到了任何关于可达性的帖子,但人们并没有真正给出这个问题的确切答案。在我的应用程序中,我使用来自苹果的可达性代码,在我的 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 回答