16

从 Apple 下载的可达性,使用此方法检查活动连接:

-(BOOL)isReachable{

    Reachability *r = [Reachability reachabilityWithHostName:@"http://www.google.com"];

    if(NotReachable == [r currentReachabilityStatus]) {
        NSLog(@"Not Reachable");
        return NO;
    }

    NSLog(@"Reachable");
    return YES;  

}

尽管已连接,但每次都返回 NO?想不通为什么...

有任何想法吗?谢谢。

在旁注中,任何人都可以推荐一个好的干净的可达性单例类吗?

4

6 回答 6

30

已编辑:http您应该从reachabilityWithHostName通话中删除协议,因此将其更新为

 Reachability *reachability = [Reachability reachabilityWithHostname:@"www.google.com"];
 NetworkStatus netStatus = [reachability currentReachabilityStatus];
于 2012-07-01T17:53:37.807 回答
5

使用此代码检查设备是否已连接到互联网

在 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:41:14.270 回答
2

我对 Tony Million 的可达性有同样的问题:网络状态总是设置为 NotReachable。我通过添加 SystemConfiguration 框架来修复它

希望能帮助到你

于 2014-08-04T09:37:32.710 回答
1

我使用KSReachability。它适用于 NSNotification、blocks、KVO,以及有无 ARC。

KSReachability *reachability = [KSReachability reachabilityToHost:@"www.google.com"];
reachability.onReachabilityChanged = ^(KSReachability* reachability) {
    NSLog(@"Update: network is %@", reachability.reachable ? @"up." : @"down.");
};
于 2012-07-01T18:04:21.467 回答
1

如果您尝试查看设备是否可以访问互联网,您可能应该使用reachabilityForInternetConnection 而不是reachabilityWithHostName:。此外,这两个调用都需要一点时间来启动(它仍然会以毫秒为单位,但比到达下一行的 if 条件所需的时间要长。)这是一个单例类的示例使用可达性。

static NetworkManager* sharedInstance = nil;

@interface NetworkManager()
@property (nonatomic, strong) Reachability* reachability;
@end

@implementation NetworkManager
@synthesize reachability;

+ (NetworkManager*)sharedInstance
{
    @synchronized(self) {
        if (sharedInstance == nil) {
            sharedInstance = [[NetworkManager alloc] init];
        }
    }
    return sharedInstance;
}

- (id)init
{
    reachability = [WYReachability reachabilityForInternetConnection];
}

- (BOOL)isNetworkReachable
{
    return ([self.reachability currentReachabilityStatus] != NotReachable);
}
@end

要检查您可以使用的其他类中可访问的网络。

[[NetworkManager sharedInstance] isNetworkReachable];
于 2012-07-01T20:53:17.890 回答
0

另一个完整的答案:

-(BOOL) isReachable:(NSString *)url
{
    //Retrieve the host name by given url address.
    NSString *hostName = [[NSURL URLWithString:url] host];
    Reachability *r = [Reachability reachabilityWithHostName:hostName];

    if(NotReachable == [r currentReachabilityStatus]) {
        NSLog(@"Not Reachable");
        return NO;
    }
    NSLog(@"Reachable");
    return YES;
}
于 2014-09-01T07:01:35.187 回答