简体   繁体   中英

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.

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?

So how to check inte.net connection without host? 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.

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. Like for example google.com or 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.

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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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