kReachabilityChangedNotification被多次调用 [英] kReachabilityChangedNotification is called multiple times

查看:166
本文介绍了kReachabilityChangedNotification被多次调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用可达性类来检查我何时收到互联网连接。

I'm using the Reachability classes for checking when I got an internet connection en when it goes down.

这是我的代码:

      IN VIEW DID LOAD:

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

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


    Then the notification method
    -(void) checkNetworkStatus:(NSNotification *)notice
    {
        // called after network status changes
        NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
        switch (internetStatus)
        {
            case NotReachable:
            {
                NSLog(@"The internet is down. IN AGENDA");
                self.internetActive = NO;

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

        NetworkStatus hostStatus = [hostReachable currentReachabilityStatus];
        switch (hostStatus)
        {
            case NotReachable:
            {
                NSLog(@"A gateway to the host server is down.IN AGENDA");
                self.hostActive = NO;

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

And finally how I add en remove the observer
-(void)viewWillAppear:(BOOL)animated{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkNetworkStatus:) name:kReachabilityChangedNotification object:nil];
}
-(void)viewDidDisappear:(BOOL)animated{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:kReachabilityChangedNotification object:nil];
}

问题

似乎viewcontroller多次获取通知。
这是我的NSLOG

It seems like that the viewcontroller gets the notification multiple times. This is my NSLOG

2014-01-04 10:32:16.299 Adsolut[10009:333b] CALLED reachabilityChanged
2014-01-04 10:32:16.299 Adsolut[10009:470b] CALLED reachabilityChanged
2014-01-04 10:32:16.300 Adsolut[10009:70b] The internet is down. IN AGENDA
2014-01-04 10:32:16.301 Adsolut[10009:70b] A gateway to the host server is down.IN AGENDA
2014-01-04 10:32:16.301 Adsolut[10009:70b] The internet is down. IN AGENDA
2014-01-04 10:32:16.302 Adsolut[10009:70b] A gateway to the host server is down.IN AGENDA
2014-01-04 10:32:16.313 Adsolut[10009:470b] CALLED reachabilityChanged
2014-01-04 10:32:16.313 Adsolut[10009:4a03] CALLED reachabilityChanged
2014-01-04 10:32:16.314 Adsolut[10009:70b] The internet is working via WIFI.IN AGENDA
2014-01-04 10:32:16.314 Adsolut[10009:333b] CALLED reachabilityChanged
2014-01-04 10:32:16.315 Adsolut[10009:70b] A gateway to the host server is down.IN AGENDA
2014-01-04 10:32:16.314 Adsolut[10009:f63] CALLED reachabilityChanged
2014-01-04 10:32:16.315 Adsolut[10009:70b] The internet is working via WIFI.IN AGENDA
2014-01-04 10:32:16.316 Adsolut[10009:70b] A gateway to the host server is down.IN AGENDA
2014-01-04 10:32:16.316 Adsolut[10009:70b] The internet is working via WIFI.IN AGENDA
2014-01-04 10:32:16.317 Adsolut[10009:70b] A gateway to the host server is down.IN AGENDA
2014-01-04 10:32:16.317 Adsolut[10009:70b] The internet is working via WIFI.IN AGENDA
2014-01-04 10:32:16.318 Adsolut[10009:70b] A gateway to the host server is down.IN AGENDA

有人可以帮忙吗?

推荐答案

仅在appdelegate didFinishLaunchingWithOptions中添加可达性通知,并在appDelegate中设置Bool变量isInternetAvailable中的可达性值

Add the reachability notification in the appdelegate didFinishLaunchingWithOptions only and set the value of reachability in Bool variable isInternetAvailable in appDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

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

}


- (void)checkNetworkStatus:(NSNotification *)notice {
    // called after network status changes

    NetworkStatus internetStatus = [self.internetReachable currentReachabilityStatus];
    switch (internetStatus)
    {
        case NotReachable:
        {
            //#######NSLog(@"The internet is down.");
            self.isInternetAvailable = FALSE;
            break;
        }
        case ReachableViaWiFi:
        {
            //#######NSLog(@"The internet is working via WIFI");
            self.isInternetAvailable = TRUE;
            break;
        }
        case ReachableViaWWAN:
        {
            //#######NSLog(@"The internet is working via WWAN!");
            self.isInternetAvailable = TRUE;
            break;
        }

        default:
        {
            //#######NSLog(@"The internet is working via mobile SIM!");
            self.isInternetAvailable = FALSE;
            break;
        }
    }
}

这篇关于kReachabilityChangedNotification被多次调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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