قالب وردپرس درنا توس
Home / IOS Development / Network frame in IOS: How to monitor network status changes

Network frame in IOS: How to monitor network status changes



Hello guys and welcome! Beyond doubt, all apps that exchange data with servers need to know one thing all the time: whether they are connected to the Internet or not. When offline, it is usually desirable to change the user experience and update the user interface to reflect the app's inability to perform network-based operations. Moreover, even when an app is connected to the Internet, it is always extremely useful to know the type of connection, such as Wi-Fi or mobile. No one would like to use an app that retrieves a large amount of data over a cellular network without knowing it, as it will incur additional charges on the user's mobile data plan. Users should be able to turn on and off such functionality on their own.

Fortunately, getting the necessary information to determine everything above has become very smooth when Apple made the Network frame available in iOS 1

2. With that, network status becomes and is notified of changes to it is a standard and simple process, and we will see the details of this post today. Before iOS 12, getting all the necessary network information and observing for changes, was a cumbersome task that was based on the SCNetworkReachability API, a more C-like solution. Over the years, several custom implementations seemed to make it easier to work with SCNetworkReachability, but with the network framework being present for almost a year now, it will soon be history.

In this post we will not only see the details of the network framework and how to use it to monitor network changes, but we will also make it a reusable component by creating a small custom framework based on the implementation we will be doing in the next parts. Cherry on the cake should be the presentation of all the steps required to make the custom framework available for distribution as a CocoaPod. Interestingly enough?

Demo Project overview

Today we are going to work on a relatively simple project where we are going to show network related information on a table view. Specifically, the app will show if the device is connected to any network interface (Wi-Fi, mobile, etc.) what the interface is, all available interface types, and whether you are using the current interface that is considered to be be an animal operation or not. We discuss more about all these in the parts that come next.

  Network Frame IOS Example

Everything above is intended to be the result of some prior work, and it will be the implementation of a custom class that we will name NetStatus . In it we use the network frame from the iOS SDK, and we create a custom, easy-to-use API that can be integrated into any app from there. There are not many things we can do with the network framework, so our execution tasks will be completed soon. And that's a good news as we get the chance to create a small custom framework based on the NetStatus class, so we can easily use it in various iOS projects.

It is a startup project for you to download, where some preliminary work has already been done. At some point in the post, you are asked to keep a copy of the project as it is at that time, because we will make significant changes to it. You also get two variations of the final project: The first will include the demo application along with the NetStatus class as one project, the second will include the custom framework and demo application as different projects, with the framework to be integrated into the demo app using a local pod.

Then enjoy reading and get ready to meet the network frame first. Then you learn how to create your own open-source Swift-based framework that can be distributed as a CocoaPod.

Creating a Singleton Class

We begin by going straight to the implementation of a custom class. Through the methods and features of this class, we will build and provide a reusable API that makes the network's framework easily accessible.

Our work takes place in the file NetStatus.swift that you find in the startup project and it is currently empty. We keep the same name for our class:

Before moving on, let's import the network frame so that we can access the API:

NetStatus will be a singleton class, so it will not be necessary to create instances of it when it is necessary to use it. There are two requirements according to the singleton pattern: First, we must initialize a static shared instace of the class inside the class:

Second, we must have a private initiator:

Great! This is what we have so far: