iOS / iPhone可达性 - 如何使用Reachability.m / .h检查互联网何时丢失/无法访问 [英] iOS/iPhone Reachability - How to only check when internet is lost/not reachable using Reachability.m/.h

查看:122
本文介绍了iOS / iPhone可达性 - 如何使用Reachability.m / .h检查互联网何时丢失/无法访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我正在通过apple reachability.m / .h使用该类,它可以工作,除了它通知我任何更改,我想只通知用户网络是否无法访问。目前,如果我有互联网连接,然后松散网络,它告诉我。但是,当你重新连接到网络时,它也告诉我,我不想要。我希望它只告诉我什么时候有丢失/没有网络。

Currently i am using the class by apple reachability.m/.h and it works, except it notifies me for any change, where as i would like to only notify the user if the network is not reachable. Currently if i have a internet connection and then loose the network it tells me. However when you reconnect to the network it also tells me, which i do not want. I want it to only tell me when there is a loss/no network.

我认为这与电话有关:

- (void)viewWillAppear:(BOOL)animated
{
    // check for internet connection
    [[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.ca"] retain];
    [hostReachable startNotifier];

    // now patiently wait for the notification
}

当调用 - [NSNotificationCenter addObserver:selector:name:object:] 时,该名称是否具有任何其他功能,然后字面上是一个名称?这是我第一次使用NSNotificationCenter,所以我不太熟悉这个问题。

when calling -[NSNotificationCenter addObserver:selector:name:object:], does the name have any other function then being literally a name? this is my first time using NSNotificationCenter so i am not well versed in this matter.

编辑:

这是我的checkNetworkStatus函数:(问题是我得到NotReachable作为网络连接回来,NSAlert多次关闭)

Here is my checkNetworkStatus function: (The problem is i am getting "NotReachable" as the network connection is coming back and NSAlert goes off multiple times)

- (void) checkNetworkStatus:(NSNotification *)notice
{
        // called after network status changes
NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
switch (internetStatus)

{
    case NotReachable:
    {
        UIAlertView * alert  = [[UIAlertView alloc] initWithTitle:@"Network Failed" message:@"Please check your connection and try again." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil ];
        [alert show];
        NSLog(@"The internet is down.");

        break;

    }
    case ReachableViaWiFi:
    {               
        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:
    {
        NSLog(@"A gateway to the host server is down.");

        break;

    }
    case ReachableViaWiFi:
    {
        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;

    }
}

}

推荐答案

当状态发生变化时,可达性将发送通知,但您对该通知的处理完全取决于您。如果您不想告诉用户网络已恢复,则您不必这样做。

Reachability will send a notification when the status has changed, but what you do with that notification is entirely up to you. If you don't want to tell the user that the network is back, you don't have to.

NSNotificationCenter方法中的name参数指示通知你订阅了。当一个对象发布通知时,它会以特定名称发布。

The "name" parameter in the NSNotificationCenter method indicates what notification you are subscribing to. When an object posts a notification, it does so with a particular name.

这篇关于iOS / iPhone可达性 - 如何使用Reachability.m / .h检查互联网何时丢失/无法访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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