iOS互联网可达性被称为两次

查看控制器来确定可达性,如果应用程序连接到互联网我有这里的代码:

//Check for internet internetReachableFoo = [Reachability reachabilityWithHostname:@"www.google.com"]; // Internet is reachable internetReachableFoo.reachableBlock = ^(Reachability*reach) { // Update the UI on the main thread dispatch_async(dispatch_get_main_queue(), ^{ NSLog(@"Yayyy, we have the interwebs!"); }); }; // Internet is not reachable internetReachableFoo.unreachableBlock = ^(Reachability*reach) { // Update the UI on the main thread dispatch_async(dispatch_get_main_queue(), ^{ NSLog(@"Someone broke the internet :("); UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Important" message: @"You are not connected to the internet. Please try again." delegate: nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alert show]; }); }; [internetReachableFoo startNotifier]; } 

这一切都完美的作品,但是当我没有互联网popup警报视图,然后当我再次获得互联网整个脚本再次运行,并有一个小的延迟时,当它检测到互联网正在连接到它实际返回google.com,因此它检查并声称互联网是不可及的,然后说它终于连接。 要点是因为它声称互联网没有连接两次它带来了两次UIAlertView。 有什么我可以做只有让alertview出现一次。 我已经尝试过移动这个脚本,我不太熟悉,我可以在alert视图中做更多的事情,使它不被再次调用。 任何帮助表示赞赏。

他们正确的方式来实现这一点

 -(BOOL)reachable { Reachability *r = [Reachability reachabilityWithHostName:@"www.google.com"]; NetworkStatus internetStatus = [r currentReachabilityStatus]; if(internetStatus == NotReachable) { return NO; } return YES; } if([self reachable]) { dispatch_async(dispatch_get_main_queue(), ^{ NSLog(@"Yayyy, we have the interwebs!"); }); }else{ UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Important" message: @"You are not connected to the internet. Please try again." delegate: nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alert show]; } 

由于Reachability与SCNetworkReachabilitySetCallback绑定,所以Matt的答案也与Apple的Reachability有关:

这是预期的行为。

AFNetworkReachabilityManager绑定到SCNetworkReachabilitySetCallback中,SCNetworkReachabilitySetCallback在SCNetworkReachabilityFlags发生变化时触发。 虽然每个callback中的标志都会改变,但callback之间的总体开/关区别可能不会改变。 这不是很好,但没有改变与SystemConfiguration的合同没有太多的事情要做。

我会这样说:你目前对可达性的使用是强烈的不鼓励。 用户不应该有触发可达性的模式警报。 如果有的话,可达性应该在非模态视图中传达,不妨碍用户与应用交互。

信贷去亚光

你也可以这样检查

 - (BOOL)connected { Reachability *reachability = [Reachability reachabilityForInternetConnection]; NetworkStatus networkStatus = [reachability currentReachabilityStatus]; return !(networkStatus == NotReachable); } 

使用它

 if( [self connected]) { // Do your stuff } else { UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Important" message: @"You are not connected to the internet. Please try again." delegate: nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alert show]; } 

我在3.0版本中遇到了同样的问题,我提出的解决scheme是,

  1. 通过制作全球可达性对象
  2. 从viewdidLoad()调用观察者而不是viewWillAppear()
  3. 删除viewwillDisappear()中的观察者
  override func viewWillDisappear(_ animated: Bool){ super.viewWillDisappear(true) print("--- : viewWillDisappear :----") NotificationCenter.default.removeObserver(self, name: ReachabilityChangedNotification, object: reachabilitee) NotificationCenter.default.removeObserver(self, name: NSNotification.Name("dismissLVC"), object: nil) }