如何在wifi源丢失连接时检测到网络可达性的丢失? [英] How to detect loss of internet reachability when wifi source loses connection?

查看:124
本文介绍了如何在wifi源丢失连接时检测到网络可达性的丢失?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力获得真实的互联网连接状态。我已经使用了Apple的Reachability但它只给了我wifi连接的状态,即没有覆盖设备通过wifi连接(到路由器或热点)的情况,但wifi路由器或热点本身没有连接到互联网。通过从wifi路由器拉入互联网输入电缆可以重现这种情况。对于 reachabilityForInternetConnection ReachabilityWithHostName ,可达性通知程序返回 ReachableViaWiFi 。我非常关心这个问题。我也通过 NSURLConnection 尝试了它,但是这太耗力了,我个人不喜欢那个继续发出URL请求的解决方案,不过在后台线程中。

I am trying hard to get the authentic internet connectivity status. I have used Apple's Reachability but it's only giving me the status of wifi connectivity i.e. not covering the case when the device is connected via wifi (to a router or hotspot), but the wifi router or hotspot itself is not connected to the internet. This scenario is reproducible by pulling the internet input cable from wifi router. The reachability's notifier returns ReachableViaWiFi for both reachabilityForInternetConnection and ReachabilityWithHostName. I am quite much in a fix with this issue. I tried it via the NSURLConnection too but that's draining battery too much and personally I don't like that solution to keep making URL requests, though in a background thread.

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

Here's the code that I am using (courtesy of an SO fellow, don't remember the exact link though)

 [[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;

        }
}
}


推荐答案

根据可达性它不仅检查wifi而且还检查WWAN / 3G并且没有互联网访问,为什么你认为它不会检查WIFI以外?

According to Reachability it checks for not only wifi but also for WWAN/3G and no internet access, why do you think it does not check other than WIFI?

 typedef enum {

    NotReachable = 0,

    ReachableViaWiFi,

    ReachableViaWWAN

} NetworkStatus;

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

if you want to check the reachability to a particular host then you could set the host yourself like this

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;
        }
}

这篇关于如何在wifi源丢失连接时检测到网络可达性的丢失?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆