Internet connectivity in iOS – How to check

Swift Tutorials Detect change in internet connection using Network framework

Earlier we use Reachability classes to check for internet connectivity in iOS sdk. In swift, we commonly use cocoa-pod named Reachability to monitor for network changes. With iOS 12.0, apple provide a new framework named Network framework. In Network framework, we have one class named NWPathMonitor that monitor for network connectivity in iOS sdk.

In this post, we will learn how to monitor for network changes using NWPathMonitor of Network framework. Follow the below steps to check for internet connectivity in iOS using swift language.

First, import Network framework

import Network

Next, we will create a function named monitorNetwork. This function will be responsible for monitoring changes in network connection for our iOS app.

Now create an instance of NWPathMonitor.

func monitorNetwork() {
      let monitor = NWPathMonitor()

The NWPathMonitor has two type’s of initializer’s

  • First one, has no parameters indicating the we need to monitor any type of internet connectivity. Either cellular network or WiFi connectivity.
  • Second initialize’r, has a parameter named requiredInterfaceType. This give developer flexibility to monitor for a particular type of network connection.

Types of InterfaceType in Network framework

 public enum InterfaceType {

        /// A virtual or otherwise unknown interface type
        case other

        /// A Wi-Fi link
        case wifi

        /// A Cellular link
        case cellular

        /// A Wired Ethernet link
        case wiredEthernet

        /// The Loopback Interface
        case loopback

Detect internet connectivity in iOS

To detect change in internet connectivity using network framework, we will listen for network events provide by iOS on pathUpdateHandler. Here we will detect if status of path, returned in pathUpdateHandler is staisfied or not. If path is satisfied that means we have active internet connection and if path is not satisfied then there is no network connectivity. Lastly, we will add monitor to background queue.

   func monitorNetwork() {
        let monitor = NWPathMonitor()
        monitor.pathUpdateHandler = { path in
            if path.status == .satisfied {
                print("Intenet connected")
            } else {
                print("No internet")
        let queue = DispatchQueue(label: "Network")
        monitor.start(queue: queue)

Where to go from here

In this post we learned, how to detect for active internet connection in swift using network framework. However, Network framework is only available from iOS 12.0 and above. So if your app is also supporting iOS lower than iOS 12.0. Then you need to look for Reachability pod, so that one can check for internet connectivity in iOS app using swift language.