0

In my app if user do not have internet connection I need to show "NO internet connection" message.

To use Reachability like that:

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

is bad idea, because internet could be available but host is not, right ?

So how to check internet connection without host ? Thanks...

Jim
  • 8,874
  • 16
  • 68
  • 125
  • Read through the answer to http://stackoverflow.com/questions/6062835/updating-iphone-reachability-on-network-changes - as it is a much better mechanism for reacting in the event of the network not being present. i.e. you should only show the 'no network connection' when the app has tried to make the network function by making network requests, then you check the reachability results, rather than relying on the reachability at the outset. – Anya Shenanigans Apr 06 '12 at 09:38

2 Answers2

1
Reachability *internetReachable = [Reachability reachabilityForInternetConnection];
[internetReachable startNotifier];
if(internetReachable.isReachable)
    NSLog(@"True");
Kuldeep
  • 2,589
  • 1
  • 18
  • 28
0

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 internet 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 internet 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 internet 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 internet connection might be the problem

joern
  • 27,354
  • 7
  • 90
  • 105
  • I have heard that apple can reject the app if it could show a message "No internet connection" if internet is available..Something like that...Trying to find that.. – Jim Apr 06 '12 at 09:52
  • It think you should be fine, if you try to make the request to your server first and use Reachability only if your server does not respond to check if you can reach another host. But that's just my personal opinion. – joern Apr 06 '12 at 10:03