Enhancing Network Reachability with NWPathMonitor in Swift

Saeid Rezaeisadrabadi
3 min readAug 14, 2023

--

In the past, I shared my initial experiences with Network Reachability in Swift 4 through a Medium article. You can revisit that story.

In this new article, I aim to delve into the usage of the NWPathMonitor introduced in Swift 5.x. Maintaining consistent network reachability remains essential for offering a seamless user experience within modern applications.

Through the NWPathMonitor class, a powerful tool in Swift, you can effortlessly track alterations in network connectivity and respond proactively.

NWPathMonitor

This story will guide you through implementing NWPathMonitor to ensure your app remains responsive and user-friendly despite shifting network conditions.

Understanding NWPathMonitor:

NWPathMonitor plays a pivotal role in the Network framework, a feature introduced in iOS 12 and subsequent versions. It enables real-time observation of network path adjustments, encompassing Wi-Fi, cellular, and wired connections. Monitoring these network path modifications empowers you to tailor your app’s behavior based on variables like network availability, connection types, and data transfer scenarios.

Step 1:

To start, initiate a new class named NetworkMonitor.swift and make sure to import the Network framework:

import Network

final class NetworkMonitor {

}

Step 2:

Instantiate an NWPathMonitor object to start monitoring network path changes:

import Network

final class NetworkMonitor {
let pathMonitor = NWPathMonitor()
}

Step 3:

Start monitoring network path changes and receive updates using the pathUpdateHandler closure:

import Network
import Combine

final class NetworkMonitor {
let networkStatus = PassthroughSubject<Bool, Never>()
var onWifi = true
var onCellular = true

private let pathMonitor = NWPathMonitor()
private let monitorQueue = DispatchQueue(label: "NetworkMonitor")

init() {
pathMonitor.pathUpdateHandler = { [weak self] path in
guard let self else { return }

networkStatus.send(path.status == .satisfied)
onWifi = path.usesInterfaceType(.wifi)
onCellular = path.usesInterfaceType(.cellular)
}

pathMonitor.start(queue: monitorQueue)
}
}

I’ve also imported Combine, as I intend to publish the network status for other parts of the app to observe. Two additional properties are introduced to differentiate between the network type: whether the device is connected via Wi-Fi or cellular. A new queue is created to initiate network monitoring.

Putting It All Together:

By integrating NWPathMonitor into your app, you gain the ability to dynamically adapt to changes in network availability and connection types. Whether you’re showcasing notifications, altering UI components, or fine-tuning data retrieval strategies, NWPathMonitor equips you to develop a responsive app that seamlessly adjusts to varying network circumstances.

Conclusion:

Network reachability stands as an indispensable aspect of contemporary app development. By incorporating NWPathMonitor into your Swift project, you can ensure your app retains its robustness and user-friendliness even amidst evolving network conditions. Regardless of whether you’re developing a social networking app, a news aggregator, or an e-commerce platform, harnessing NWPathMonitor will amplify your app’s reliability and performance, ultimately contributing to a more gratified user base.

--

--