可达性不起作用

Posted

技术标签:

【中文标题】可达性不起作用【英文标题】:Reachability not working 【发布时间】:2014-04-02 11:51:28 【问题描述】:

我目前正在编写一个需要互联网连接(TCP 到服务器)的游戏。我正在尝试使用 Apple 的 Reachability 类,但它不起作用。我是 NotificationCenter 和 Reachability 的新手,所以如果这是一个愚蠢的问题,请原谅。

LoginViewController.h 中与可达性相关的道具

@property (nonatomic) Reachability *hostReachability;
@property (nonatomic) Reachability *internetReachability;
@property (nonatomic) Reachability *wifiReachability;
@property BOOL internetActive;
@property BOOL hostActive;

在 LoginViewController.m 中

//Reachability
    [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(handleNetworkChange:) name: kReachabilityChangedNotification object: nil];

    NSString* hostIP = @"<my server IP is here, removed for obvious reasons>";

    hostReachability = [Reachability reachabilityWithHostname:hostIP];

    [hostReachability startNotifier];

    NetworkStatus hostStatus = [internetReachability currentReachabilityStatus];

    if(hostStatus == NotReachable) NSLog(@"no");
    else if (hostStatus == ReachableViaWiFi) NSLog(@"wifi"); 
    else if (hostStatus == ReachableViaWWAN) NSLog(@"cell"); 

    hostReachability = [Reachability reachabilityForInternetConnection];

    [internetReachability startNotifier];

    NetworkStatus internetStatus = [internetReachability currentReachabilityStatus];

    if(internetStatus == NotReachable) NSLog(@"no");
    else if (internetStatus == ReachableViaWiFi) NSLog(@"wifi"); 
    else if (internetStatus == ReachableViaWWAN) NSLog(@"cell"); 

handleNetworkChange:过程

- (void)handleNetworkChange:(NSNotification*)notification
    NetworkStatus internetStatus = [internetReachability currentReachabilityStatus];

    switch (internetStatus) 
        case NotReachable:
        
            NSLog(@"No internet connection");
            self.internetActive = NO;
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No internet connection"
                                                            message:@"Could not connect to the internet. Please try again later."
                                                           delegate:nil
                                                  cancelButtonTitle:@"OK"
                                                  otherButtonTitles:nil];
            [alert show];
        
        break;
        case ReachableViaWiFi:
        
            NSLog(@"The internet is working via WIFI.IN AGENDA");
            self.internetActive = YES;

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

            break;
        

        default:
        break;
    

    NetworkStatus hostStatus = [hostReachability currentReachabilityStatus];
    switch (hostStatus)
    
        case NotReachable:
        
            NSLog(@"A gateway to the host server is down.IN AGENDA");
            self.hostActive = NO;
            NSLog(@"No internet connection");
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No connection to host"
                                                            message:@"Could not connect to the host server. Please try again later."
                                                           delegate:nil
                                                  cancelButtonTitle:@"OK"
                                                  otherButtonTitles:nil];
            [alert show];

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

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

            break;
        
    

运行应用程序时,无论我是否实际连接到互联网,以及服务器软件是否正在运行,我总是在终端中收到“no no”。

提前致谢。

【问题讨论】:

【参考方案1】:

这里的主要问题是您的代码中的拼写错误; LoginViewController.mhostReachability = [Reachability reachabilityForInternetConnection]; 的行应该是internetReachability = [Reachability reachabilityForInternetConnection];。目前的情况是,您永远不会实例化internetReachability,然后我猜[internetReachability currentReachabilityStatus] 将始终返回nil

以下是您可以使用的基本示例:

@interface LoginViewController ()

// ...

@property (strong, nonatomic) Reachability *hostReachability;
@property (strong, nonatomic) Reachability *internetReachability;
@property (strong, nonatomic) Reachability *wifiReachability;

@end

@implementation LoginViewController

// ...

- (void)setupReachabilityMonitoring

    [NSNotificationCenter.defaultCenter addObserver:self
                                           selector:@selector(reachabilityChanged:)
                                               name:kReachabilityChangedNotification
                                             object:nil];

    _hostReachability = [Reachability reachabilityWithHostName:@"8.8.8.8"];
    [_hostReachability startNotifier];

    _internetReachability = [Reachability reachabilityForInternetConnection];
    [_internetReachability startNotifier];

    _wifiReachability = [Reachability reachabilityForLocalWiFi];
    [_wifiReachability startNotifier];

    [self logReachabilityStatus];


- (void)reachabilityChanged:(NSNotification *)notification

    if ([notification.object isKindOfClass:Reachability.class]) 
        [self logReachabilityStatus];
    


- (void)logReachabilityStatus

    NSString *hostStatus = _hostReachability.currentReachabilityStatus ? @"up" : @"down";
    NSString *internetStatus = _internetReachability.currentReachabilityStatus ? @"up" : @"down";
    NSString *wifiStatus = _wifiReachability.currentReachabilityStatus ? @"up" : @"down";

    NSLog(@"Internet is %@, wifi is %@, host is %@", internetStatus, wifiStatus, hostStatus);


@end

【讨论】:

【参考方案2】:

我正在使用这个库使用块,所以它不会冻结应用程序并在后台运行

https://github.com/tonymillion/Reachability

【讨论】:

使用 Apple 的 Reachability 类并在后台线程上调用它实际上很简单。如果不需要,为什么要导入第三方代码?【参考方案3】:

我认为问题出在您的“主机名”上。这不是您服务器的 IP,而是您的主机名,例如 www.apple.com。您可以查看 Apple 的example code。在第 87 行,他们将主机名设置为 @"www.apple.com"

【讨论】:

但是我应该检查我的服务器是否可用,对吗?至少对我来说,将“主机名”实体指向我的服务器的 IP 地址是有意义的。它仍然无法解释为什么我在测试互联网连接时得到“否”。

以上是关于可达性不起作用的主要内容,如果未能解决你的问题,请参考以下文章

需要帮忙。 Apple 的带有地址的可达性类不起作用

可达性转到“Wi-Fi 设置”按钮不起作用

reachabilityWithAddress 在 Objective C 编程中不起作用

Swift 4 中的可达性问题

AFNetworking - 可达性总是返回 -1

transpportrule不起作用