Network reachability status monitoring on iOS (Part 2)

Sauvik Dolui
5 min readOct 24, 2016

--

Monitoring Network Reachability Status on Popular iOS Apps

Before you dig a little bit deeper:

In Part 1 of this post, I have discussed the basic setups of the project. Before you start monitoring network reachability from your own classes, please go through the first part of this blog post sequence. If you have already finished that one, you are on your marks.

Revision of Part 1:

In Part 1, we have set up a singleton class ReachabilityManager. We tried to start or stop monitoring network status change with the followings.

Till now ReachabilityManager simply puts some debug logs on console whenever there is a change in the network status. Stop, stop 👿… What did I say? It just puts some logs on console!!!. I am sure this is not what you want finally.

What you need:

You may have a list of tasks in your current project where you need to constantly monitor network reachability. The list may contain the followings

  1. Disable/Enable the Login button when the network is unreachable/reachable.
  2. Show a notification coming down from Status Bar, when your user became offline just before she/he checks out of her/his shopping cart.
  3. Automatic Pause/Resume network layer long term services.
  4. And so on…

So the basic task is to have a scope to execute your code when there is a change in network reachability status.

The concept:

To implement this, ReachabilityManager shared instance will have a list of listeners. There is a limitation, only objects conforming to NetworkStatusListener protocol can be a listener for ReachabilityManager. Sounds good?

Let’s start coding:

We will start from where we ended up in Part 1. Download the intermediate project from GitHub and open NetworkStatusMonitor.xcworkspace. We will add NetworkStatusListener protocol just before the class definition of ReachabilityManager in ReachabilityManager.swift file.

We will need to have an array of objects which will be interested to listen to change in reachability status. So we add another property to ReachabilityManager.

To add or remove a listener, we are going to add two more helper methods in ReachabilityManager.

Now we need to send a message to each listener whenever there is a change in reachability. To achieve this we will just update func reachabilityChanged(_:) in the following way.

It’s time to get benefited:

So far we have designed ReachabilityManager to listen to the change in network reachability and send messages back to its listeners who are interested in the status change. Can we get benefited from that? Of course, my friend!!! We have a plan for that.

Let’s start using that in our “awesome” login form. Open pregenerated View Controller Scene from Main.storyboard and create a login form similar to that one designed bellow.

Login Form Desing on Storyboard

Well, that might not have a professional look at all, but it’s enough to help us to demonstrate how to use NetworkStatusListener protocol.

Now move back to ViewController.swift, we will create an @IBOutlet for the login button.

Login button IBOutlets

Let’s try to listen:

To be a listener, we need to add our ViewController instance to the listeners array of ReachabilityManager. Here, we are interested to listen to that reachability status change when view controller is on screen. Whenever view controller scene is displayed on the screen, it’s sure that viewWillAppear(_:) will get called. In the same way viewDidDisappear(_:) is invoked when scene has disappeared from the screen. If you want to know more about view controller life cycle methods you can check out this Stackoverflow answer.

So we will assign and remove this view controller as listeners to network reachability status by overriding viewWillAppear(_:) and viewDidDisappear(_:) in the following way.

Let’s try to build (⌘ + B) our project. The compiler will not be happy with you at this time. 😡 You will get Argument type ‘ViewController’ does not conform to expected type ‘NetworkStatusListener error.

What’s that? As the compiler says, our view controller is not conforming to the NetworkStatusListener protocol. An object implementing NetworkStatusListener protocol can only be added to listeners array of ReachabilityManager. ViewController needs to implement that protocol.

Try to add the following code snippet at the very end of ViewController.swift.

What’s now if you build your project? It seems that now angry compiler becomes happy this time. All errors are gone 😇.

The million $question:

What we will do here? Let us think 🤔. Can we enable/disable Login button depending upon the reachable/unreachable status of the network? Well, you might be thinking that that would be one stuff what we can do. Let’s do that by updating the func networkStatusDidChange(status:).

The result: 😇

Enabling/Disabling login button on network reachability status change

Anything more?

So we have travelled a long way and finally added “a scope to execute your code when there is a change in network reachability status”. If you find difficulty implementing this, you can have a look at the final project on GitHub.

Thanks for reading. 🙂

Promotions:

Don’t forget to read my other blogs😏.

1. A Smart Way to Manage Colour Schemes for iOS Application Development.

2. Handling Fonts in iOS Development, a Simpler Way.

3. Developing a Tiny Logger in Swift.

4. Making a Stateful TableView for iOS.

5. A few git tricks & tips.

--

--