Should I listen for reachability updates in every UIViewController?

I see many examples of achievement opportunities when people only display a message when their reachability status changes.
But recently, I saw in the Foursquare app that they display a message every time a user tries to perform an action that requires an Internet connection.
I think this is a more reliable and better UX to remind the user that he cannot do anything without the Internet. Mostly because users can switch between applications, do something else, and forget that he has no connection when he returns.
Also, as soon as they return the connection, I see that they are retrieving data from the Internet and updating the user interface.

What I'm really looking for is the best way to do this. How it's done?
Do they have a common UIViewController that checks for availability every time it needs a connection?
Or do they have some kind of proxy class before each Internet request that cancels the request and displays a message? How do you guys deal with this?

Thank.

EDIT:

The solution I came up with uses AFNetworking, which also provides an accessibility status in the field.
Basically, I created AFHTTPClient and set a reachability callback block for it to listen for state changes. The AFHTTPClient object is a widely used application (a type of singleton). (actually I have one AFHTTPClient per host, I need to contact a.com, b.com ...).

Then, when I need to execute the request, I create a new AFHTTPRequestOperation (AFJSONRequestOperation in my case) and I insert it into the AFHTTPClient object. In the operation failure block, I check if the host is accessible using the networkReachabilityStatus property for AFHTTPClient. If it is not available, I show a message that the user does not have an Internet connection.

I wrapped it up, so I don’t need to do this every time I create an operation. So, now in the application, every time the user tries to do something, when there is no connection, he received a message that he does not have Internet access.
I also use the reachability callback to reload the data on the screen as soon as I get the connection (or rather, as soon as I have to connect).
I don’t know if this is better, but I am pleased to know that the application will take care of reloading important data as soon as a new connection appears.

If someone is interested in sample code, I can provide it.

+9
ios reachability
Sep 19 2018-12-12T00: 00Z
source share
1 answer

In a WWDC conversation this year, an Apple developer on the scene recommended that users never base their access to an application’s Internet applications in an application status. Accessibility example. Often reachability does not provide complete information (it is based on a complex mechanism), and the proposal provided by the engineer is as follows:

  • try to connect to the internet no matter what status status is available. then set the user interface hint based on the success / failure result.
  • if it crashes due to network problems, then register with Reachability and try again when Reachability gives a green light; this is necessary if you want to automatically recover from a failure state
  • in any case, it gives the user the ability to "force retry", whatever the status of "Reach". If it succeeds, reset the user interface prompt.

What the Apple engineer said is completely true: you can often see console error messages in error messages while the Internet connection is completely live.

Other: there is no better “network prompt” than the one displayed on the status bar: if you have a Wi-Fi icon, 3G / 4G icon, the strength of the cellular field.

Returning to the original question: there is no absolute best way to manage this material, it depends a lot on the architecture of the application. If you prefer to configure your network materials in a dedicated class (not a UIViewController, but in a subclass of NSObject), then it makes sense to define a read-only property for this class, which is updated with success / failure after the last Internet connection with the application server (no it makes sense to ping other servers, such as Google or Apple: in the first place, this is not elegant, and in the second place, a problem may arise on your servers serving the application, and not in the status of the device’s Internet connection!).

 @property (readonly) BOOL lastConnectionToMyServerSuccess 
Then your view controllers can register (by KVO or central notification) so that this property changes and updates its interface accordingly, showing an icon or something else (I repeat: leave the user the opportunity to try manually connecting to the Internet). View controllers must unregister from KVO when hidden from view ("viewWillDisappear:") or unloaded ("viewDidLoad:") or dealloc'd.

Of course, this adds some additional complexity. For example: you are using the application, the internet light was green. Then you pause it, do something else, and after a few minutes return to the application. In this case, the application must ping on its servers in order to restore the status of Internet light again, since after a few minutes the network conditions could change (for example, you are on a train). In any case, all loaded view controllers will receive a KVO notification from the dedicated network class and update themselves.

+15
Sep 19 '12 at 8:17
source share



All Articles