How to use iOS Reachability

36,861

Solution 1

Reachability is a network helper utility class, its used to get various informations about the connection status

What is the main purposes of Reachability?

Reachability is used to query the network status, and to register your listeners to get informed when connectivity changes.

Is this the main purpose of Reachability, to show the user a message?

No, its main usage is to test if there is Internet connectivity, or to get notified if the connectivity changes

For example if NSURLConnection request has failed do I need to use Reachability somehow to resend the request?

Yes, you could use it. For example, what I normally do in my project is to save all the requests that have been made to a remote server.

Let's say I want to download 10 files. When any file fails the download process due to no Internet connection, I save them to an array of failed downloads.

Then, when Reachability informs me that the Internet connection has been restored, I iterate through this array and start the download process again.

What is the proper use of Reachability?

It depends, on your patterns and needs.

Is it common to use only one instance when app launch, and then listen to network changes?

Yes, that is what I do. In my projects, I only have 1 instance of a download manager class, and this class has the only alive instance of Reachability.

Or should I check myself the reachability status before every network request?

You can do that without having multiple instance of Reachability classes. What I normally do is to have a method inside my download manager that tells me using Reachability if there is connection or not.

Is it enough to use reachabilityWithHostName or do I need also reachabilityForLocalWiFi and reachabilityForInternetConnection?

I'm not sure about this one, but what I normally do is to test connectivity on all the means. I don't differentiate between 3G or WiFi, but there are some implementations where this info (WiFi or 3G) could be useful.

Solution 2

Reachability is an example project that Apple has made. People use this as an API over the SystemConfiguration framework. As you already have seen there are methods to check if a host is reachable and so on.

The way I use the Reachabilty project is that I have made a class with a class method that returns a boolean if the host I am requesting data from is available. If it is then the method returns YES and if it is not, it returns NO (obviously).

Now, in the application where I am making use of connections you need/should check if it is possible to start a connection as the Apple documentation states. I have a simple if and then show an appropriate alert message that the request could not be completed at the moment.

There are no requirements that you should automatically try again if the request could not be carried out the first time. The main purpose of this is to keep your application away from crashing, and at the same time give the user an message that it could not be done.

I do not use the notifications myself, but that is because I am not interested in monitoring whether or not connection can be carried out. This is something that you will have to decide by your application demands.

You are not forced to use all the methods in the Reachablitiy class, it is sufficient to use one of them. It is documented what the different methods offer and when they are ment to be used in the header file.

Remember to include the SystemConfiguration framework.

Share:
36,861
Eyal
Author by

Eyal

Updated on January 09, 2020

Comments

  • Eyal
    Eyal over 4 years

    I'm developing an iPhone app that uses the network. The iPhone communicate with my server via HTTP request and should work on WiFi and 3G.
    I currently use NSURLConnection initWithRequest to send async requests to my server and get responses (but I will soon move to work with ASIHTTPRequest library)

    I understood that with this kind of apps(apps that requires internet connection) I should (must?) use Reachability.

    After searching the web and looking at Apple's Reachability example code i still don't understand some basic stuff:

    What is the main purposes of Reachability?

    In apple's example they detect network issues with the host, WiFi and 3G and present the user with an appropriate message.
    Is this the main purpose of Reachability, to show the user a message? Or do I need to use it for other more practical scenarios? For example if NSURLConnaction request has failed do I need to use Reachability somehow to resend the request?

    What is the proper use of Reachability?

    Is it common to use only one instance when app launch, and then listen to network changes? Or should I check myself the reachability status before every network request?
    Is it enough to use reachabilityWithHostName or do I need also reachabilityForLocalWiFi and reachabilityForInternetConnection?

    One more thing, I understood apple can reject apps that use the network and don't use Reachability.
    What are the "must" do methods I should implement?
    Will it be enough to just notify the user that currently there is no internet?

  • Eyal
    Eyal almost 12 years
    Thanks for your reply, can u please check the "one more thing" that I added to the question
  • Eyal
    Eyal almost 12 years
    Thanks for your reply, when checking if the host is available before every network request, will this add an overhead to every request?
  • Andreas
    Andreas almost 12 years
    Yes, but that should be minimal. I have not noticed any noticeable overhead of doing it though. To the questions you added I answered it already I think. Feel free to ask if something is still unclear :)
  • Omar Abdelhafith
    Omar Abdelhafith almost 12 years
    Am completely unaware of that, i have 5+ apps on the appstore, they all use Reachability, i used reachability as i described above in the answer, so i dont know about what must be implemented, i normally implement what i need
  • Eyal
    Eyal almost 12 years
    So tell me if I understand right, to get the same result but with notifications, I will keep some global boolean "HostIsAvailable", this boolean will be updated when I'm getting the Reachability notifications, then before every network request I'll check if it's YES, if not I'll show the user an alert message.
  • Andreas
    Andreas almost 12 years
    Yes, that is one possible solution. You will register your app delegate as the receiver of those notifications and update the variable(s) when one of the notifications is received. Then in your view controllers you could do this MyAppDelegate* myAppDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate] to access your variables.
  • Khushbu Shah
    Khushbu Shah about 11 years
    You said that one of the main purpose of reachability is "to register your listeners to get informed when connectivity changes", but it does not give callback every time (try to switch routers and check).
  • SiavA
    SiavA over 4 years
    One important moment. You shouldn't make logic that sends (or doesn't) request based on Reachability, cuz it may give you false positive and false negative results. So answering "Or should I check myself the reachability status before every network request?": Yeah, you could check it to inform user about expected result, but you should try to send request anyway.