简体   繁体   English

iPhone:如何在没有主机的情况下使用可达性?

[英]iPhone: How to use Reachability without host?

In my app if user do not have inte.net connection I need to show "NO inte.net connection" message.在我的应用程序中,如果用户没有 inte.net 连接,我需要显示“无 inte.net 连接”消息。

To use Reachability like that:像这样使用可达性:

Reachability *netReach = [Reachability reachabilityWithHostName:@"host.name"];

is bad idea, because inte.net could be available but host is not, right?是个坏主意,因为可以使用 inte.net 但主机不可用,对吗?

So how to check inte.net connection without host?那么如何在没有主机的情况下检查 inte.net 连接呢? Thanks...谢谢...

Reachability *internetReachable = [Reachability reachabilityForInternetConnection];
[internetReachable startNotifier];
if(internetReachable.isReachable)
    NSLog(@"True");

You are absolutely right.你是绝对正确的。 Reachability only checks whether a certain host is reachable or not.可达性只检查某个主机是否可达。 The problem is, that without sending a request to a host there is no way (that I can think of) how to determine whether you are connected to the inte.net or not.问题是,如果不向主机发送请求,就无法(我能想到)如何确定您是否已连接到 inte.net。

So what you can do (and what Apple is doing in its Reachability example): Choose a host where you can be 99.99% sure that the host is available.那么您可以做什么(以及 Apple 在其 Reachability 示例中所做的):选择一个您可以 99.99% 确定主机可用的主机。 Like for example google.com or apple.com例如 google.com 或 apple.com

I think the fact that apple is relying on a certain host to be available to check inte.net connection (and the fact that they haven't come up with something else, without dependency on a certain host) is a pretty good sign that checking inte.net connection in this manner is not such a bad idea.我认为苹果依赖于某个主机来检查 inte.net 连接的事实(以及他们没有想出其他东西,而不依赖于某个主机的事实)是一个很好的迹象表明检查以这种方式连接 inte.net 并不是一个坏主意。

EDIT编辑

Of course the best way (as described in the answer to the question that Petesh is pointing to) would be to simply make the request you want to make and then handle errors accordingly if they occur.当然,最好的方法(如 Petesh 指向的问题的答案中所述)是简单地提出您想要提出的请求,然后在错误发生时相应地处理错误。 In other words: 1. Make your request 2. If it fails use Reachability to see if a missing inte.net connection might be the problem换句话说: 1. 提出您的请求 2. 如果失败,请使用 Reachability 查看缺少的 inte.net 连接是否可能是问题所在

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM