【问题标题】:How to get change in network connection notification from iOS Reachability Class?如何从 iOS 可达性类中获取网络连接通知的变化?
【发布时间】:2013-07-23 08:15:38
【问题描述】:

您好,每当用户在我的应用程序中获得网络连接时,我想捕获我已经添加了 apples Reachability 类,下面是我在 appDelegate 类 didFinishLaunchingWithOptions 方法中使用的 sn-p,

Reachability* reachability = [Reachability reachabilityForInternetConnection];
        [reachability startNotifier];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];

我的reachabilityChanged选择器方法如下

- (void)reachabilityChanged:(NSNotification*)notification
{
    Reachability* reachability = notification.object;
    if(reachability.currentReachabilityStatus == NotReachable)
        NSLog(@"Internet off");
    else
        NSLog(@"Internet on");
}

但是在这里,当我关闭飞行模式以及在手机中获得网络连接时,我没有收到任何通知。

我错过了什么吗?

【问题讨论】:

标签: ios objective-c reachability


【解决方案1】:

我在 appdelegate 中使用一个变量将当前网络状态存储为布尔值

@property (nonatomic, assign) BOOL hasInet;

.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [self setUpRechability];
}


-(void)setUpRechability
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNetworkChange:) name:kReachabilityChangedNotification object:nil];

    reachability = [Reachability reachabilityForInternetConnection];
    [reachability startNotifier];

    NetworkStatus remoteHostStatus = [reachability currentReachabilityStatus];

    if          (remoteHostStatus == NotReachable)      {NSLog(@"no");      self.hasInet-=NO;   }
    else if     (remoteHostStatus == ReachableViaWiFi)  {NSLog(@"wifi");    self.hasInet-=YES;  }
    else if     (remoteHostStatus == ReachableViaWWAN)  {NSLog(@"cell");    self.hasInet-=YES;  }

}

- (void) handleNetworkChange:(NSNotification *)notice
{
    NetworkStatus remoteHostStatus = [reachability currentReachabilityStatus];

    if          (remoteHostStatus == NotReachable)      {NSLog(@"no");      self.hasInet-=NO;   }
    else if     (remoteHostStatus == ReachableViaWiFi)  {NSLog(@"wifi");    self.hasInet-=YES;  }
    else if     (remoteHostStatus == ReachableViaWWAN)  {NSLog(@"cell");    self.hasInet-=YES;  }

//    if (self.hasInet) {
//        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Net avail" message:@"" delegate:self cancelButtonTitle:OK_EN otherButtonTitles:nil, nil];
//        [alert show];
//    }
}

【讨论】:

  • handleNetworkChange 调用多次可能是没有可用的 wifi 连接,如何控制相同
【解决方案2】:

也许你应该在 startnotifier 之前添加观察者

[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(handleNetworkChange:) name: kReachabilityChangedNotification object: nil];
reachability = [Reachability reachabilityForInternetConnection];
[reachability startNotifier];

【讨论】:

    【解决方案3】:

    只要连接状态发生变化,您就可以使用可达性包装器来获取基于块的回调。

    GitHub:UHBConnectivityManager

    如果您使用的是 cocoapods。

    pod 'UHBConnectivityManager'
    

    【讨论】:

      【解决方案4】:

      最简单的方法:

      reach = [Reachability reachabilityWithHostName:@"www.google.com"];
       reach.reachableBlock = ^(Reachability *reach){
          NSLog(@"Reachable");
      };
      
      reach.unreachableBlock = ^(Reachability *reach){
          NSLog(@"Unreachable");
      };
      [reach startNotifier];
      

      每当网络发生变化时,都会调用相应的块。

      【讨论】:

        【解决方案5】:

        将可达性作为您的属性而不是局部变量。它会起作用的。

        【讨论】:

          猜你喜欢
          • 2012-04-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多