首页 文章

如何在wifi源丢失连接时检测到网络可达性的丢失?

提问于
浏览
8

我正在努力获得真实的互联网连接状态 . 我使用Apple 's Reachability but it'只给了我wifi连接的状态,即没有覆盖设备通过wifi连接(到路由器或热点)的情况,但wifi路由器或热点本身没有连接到互联网 . 通过从wifi路由器拉入互联网输入电缆可以重现这种情况 . 对于 reachabilityForInternetConnectionReachabilityWithHostName ,可达性通知程序返回 ReachableViaWiFi . 我对这个问题非常感兴趣 . 我也通过 NSURLConnection 尝试了它,但是尽管在后台线程中,仍然像那个继续发出URL请求的解决方案 .

这是我正在使用的代码(由SO老师提供,但不记得确切的链接)

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

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

    // check if a pathway to a random host exists
    hostReachable = [[Reachability reachabilityWithHostName: @"www.google.com"] retain];
    [hostReachable startNotifier];

然后在观察者方法中:

- (void) checkNetworkStatus:(NSNotification *)notice{

    // called after network status changes

    NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
    switch (internetStatus)

    {
        case NotReachable:
        {
            NSLog(@"The internet is down.");
            self.isInternetReachable = NO;

            break;

        }
        case ReachableViaWiFi:
        {
            NSLog(@"The internet is working via WIFI.");
            self.isInternetReachable = YES;

            break;

        }
        case ReachableViaWWAN:
        {
            NSLog(@"The internet is working via WWAN.");
            self.isInternetReachable = YES;

            break;

        }
    }
    NetworkStatus hostStatus = [hostReachable currentReachabilityStatus];
    switch (hostStatus)

    {
        case NotReachable:
        {
            NSLog(@"A gateway to the host server is down.");
            self.isHostReachable = NO;

            break;

        }
        case ReachableViaWiFi:
        {
            NSLog(@"A gateway to the host server is working via WIFI.");
            self.isHostReachable = YES;

            break;

        }
        case ReachableViaWWAN:
        {
            NSLog(@"A gateway to the host server is working via WWAN.");
            self.isHostReachable = YES;

            break;

        }
}
}

3 回答

  • 7

    根据Reachability它不仅检查wifi而且还检查WWAN / 3G并且没有互联网接入,为什么你认为除了WIFI之外它不检查?

    typedef enum {
    
        NotReachable = 0,
    
        ReachableViaWiFi,
    
        ReachableViaWWAN
    
    } NetworkStatus;
    

    如果你想检查特定主机的可达性,那么你可以像这样自己设置主机

    Reachability *hostReach = [Reachability reachabilityWithHostName: @"www.google.com"];
    
    NetworkStatus netStatus = [hostReach currentReachabilityStatus];
    
     switch (netStatus)
        {
            case ReachableViaWWAN:
            {
                NSLog(@"WWAN/3G");
                break;
            }
            case ReachableViaWiFi:
            {
                NSLog(@"WIFI");
                break;
            }
            case NotReachable:
            { 
                NSLog(@"NO Internet");
                break;
            }
    }
    
  • 0

    见乔丹的答案 . Apple的官方Reachability.m中有一个愚蠢的错误 . returnValue的类型应为“NetworkStatus”而不是“BOOL”:

    - (NetworkStatus)networkStatusForFlags:(SCNetworkReachabilityFlags)flags
    {
        ...
        NetworkStatus returnValue = NotReachable;
        ...
    }
    

    否则,您最终会遇到以下可怕后果:

    • 即使蜂窝连接可用,也会报告为不可用 .

    • 如果Wifi不可用,如果蜂窝连接可用,将报告Wifi可用 .

    不敢相信苹果从不打扰解决这个问题 .

    (P.S.新的堆叠溢出,并没有代表赞成乔丹)

  • 4

    即使断开连接,我也得到了Wifi标志 . Reachability.m方法中存在一个错误:

    - (NetworkStatus)networkStatusForFlags:(SCNetworkReachabilityFlags)flags
    

    它使用BOOL作为返回值,但它为具有3个值的struct赋值:

    typedef enum : NSInteger {
        NotReachable = 0,
        ReachableViaWiFi,
        ReachableViaWWAN
    } NetworkStatus;
    

    因此,如果Wifi或Cellular可用,它将是ReachableViaWiFi(作为BOOL可以是0或1,但不是两个)

    要修复它,只需更改上面的方法:

    BOOL returnValue = NotReachable;
    

    为了这:

    int returnValue = NotReachable;
    

    你很高兴去 . 希望能帮助到你 .

相关问题