1

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

//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];

    }

这一切都很完美,但是当我没有互联网时,警报视图会弹出,然后当我再次上网时,整个脚本会再次运行,并且当它检测到互联网正在连接到它实际返回时有一个小的延迟google.com 并因此在说它最终连接之前检查并声称互联网无法访问。要点是,由于它声称两次未连接互联网,因此会两次显示 UIAlertView。我有什么办法只让警报视图显示一次。我试过移动这个脚本,但我不太熟悉我可以在警报视图上做的更多事情,以使其不再被调用。任何帮助表示赞赏。

4

4 回答 4

3

他们正确的实现方法是

-(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];
}
于 2013-07-30T06:12:59.220 回答
2

由于 Reachability 与 SCNetworkReachabilitySetCallback 相关联,因此matt的答案也与 Apple 的 Reachability 相关:

这是预期的行为。

AFNetworkReachabilityManager 与 SCNetworkReachabilitySetCallback 相关联,它会在 SCNetworkReachabilityFlags 更改的任何时间触发。尽管标志在每个回调中都发生了变化,但整体的开/关区别在回调之间可能不会改变。这不是很好,但是如果不更改与 SystemConfiguration 的合同,就没有什么可做的了。

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

归功于马特

于 2016-02-17T09:50:16.443 回答
0

你也可以像这样检查

- (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];
}
于 2013-07-30T06:06:30.670 回答
0

我在 swift 3.0 中遇到了同样的问题,我想出的解决方案是,

  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)
}
于 2017-04-25T07:06:41.110 回答