Greetings readers, today we will resume our Swift learning journey with another interesting topic on how add to observer to the internet connection in Swift.
Introduction
In modern app development, ensuring your application handles internet connectivity issues gracefully is crucial. Regardless of the type of your application being aware of the network status can significantly enhance the user experience.
In this blog post, we’ll dive into how you can add an observer for internet connectivity in a Swift application. We will be using the notification observer to observe the internet connection.
Implementation
To set up an observer for internet connectivity, follow these steps:
Step 1:- Checked internet Connectivity
Firstly, we will need to check the network connectivity and keep the track of connection status. We will use the Network framework for such purpose.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import Network class NetworkMonitor { static let shared = NetworkMonitor() private let monitor = NWPathMonitor() private let queue = DispatchQueue(label: "NetworkMonitorQueue") var isConnected: Bool = true { didSet { print("Current Network Connection Status changed: \(isConnected)") } } private init() { monitor.pathUpdateHandler = { [weak self] path in self?.isConnected = path.status == .satisfied } monitor.start(queue: queue) } } |
With the above code, it will keep track of the change in the network status. Now we need to add a way to update our UI regarding the changes.
Step 2:- Add Notification Observer
We can add a NotificationCenter to update our UI regarding the internet connection. To integrate the NotificationCenter, you need to post the notification event from our NetworkMonitor class like:-
1 2 3 4 5 |
var isConnected: Bool = true { didSet { NotificationCenter.default.post(name: NSNotification.Name(rawValue: "connectivityChanged"), object: nil, userInfo: ["isConnected": isConnected]) } } |
Once the event, is posted you will then need to catch the event where you want to show the effects of an internet connection.
1 2 |
let initiate = NetworkMonitor.shared NotificationCenter.default.addObserver(self, selector: #selector(handleNetworkChange(_:)), name: NSNotification.Name(rawValue: "connectivityChanged"), object: nil) |
In the above example, we are using a selector to handle the response. You will need to declare the selector with the class to handle the actions.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
@objc func handleNetworkChange(_ notification: Notification) { if let isConnected = notification.userInfo?["isConnected"] as? Bool { DispatchQueue.main.async { if isConnected { self.statusLabel.text = "Internet is connected."; } else { self.statusLabel.text = "No internet connection."; } } print(isConnected ? "Internet is connected." : "No internet connection.") } } |
Output
Once implemented all our steps, you should be able to see the output like:-
Conclusion
In conclusion, using an observer for internet connection status in your Swift app is a valuable strategy for ensuring a better user experience and for managing the internet connectivity on the application.
You can check out more amazing blogs with us on the Mobikul Blog.