Check Internet Connectivity with NWPathMonitor In Swift
Monitor network changes using the Network framework.
In this article, we will learn how to check the internet status of Apple devices. Also, we will create a class to make things easier and clear. Let’s start with learning what is NWPathMonitor. 🤟
What is NWPathMonitor?
If you have an app that needs internet connectivity you need an observer that checks the current network status. Apple provides a very powerful library to monitor network changes. Apple’s “Network” framework provides a bunch of useful classes but there is a class specifically designed for monitoring network changes: NWPathMonitor. You can check instantly the internet reachability by using NWPathMonitor. Also, this framework provides that user’s connection type such as cellular or wi-fi.
How to use NWPathMonitor?
Let’s learn how to use NWPathMonitor with creating a useful class. 😎
Before we get started, import the Network library in your Swift project.
import Network
Let’s start with creating a class called NetworkManager
.
final class ConnectionManager {
static let shared = ConnectionManager()
}