Member-only story
Check internet connection by using Reachability in Swift (Swift: How can I have a listener that reports when connection is lost and when it comes back?)
Sometimes due to bad network connection app doesn’t performing well. It’s very important to show latest data to the user while user explorer the app. The updated data attract more user for the app and its impact positively. Here, I am gonna show you how to manage network connection continues while using app.
From this link you can get Reachability class and just drag and drop to XCode project:
https://github.com/ashleymills/Reachability.swift
Let’s see use of it,
In your AppDelegate use variable like:
var reachability: Reachability!
Now in didFinishLaunchingWithOptions we have to add Notification for the reachability.
do {
try reachability = Reachability()NotificationCenter.default.addObserver(self, selector: #selector(self.reachabilityChanged(_:)), name: Notification.Name.reachabilityChanged, object: reachability)try reachability.startNotifier()
} catch {
print(“This is not working.”)
}
For the notification observer we need to define method which indicate the network status:
@objc func reachabilityChanged(_ note: NSNotification) {let reachability = note.object as! Reachabilityif reachability.connection != .unavailable {if reachability.connection == .wifi {print(“Reachable via WiFi”)} else {print(“Reachable via Cellular”)}} else {print(“Not reachable”)}}
You can see more helpful articles like this just go through, https://medium.com/@javedmultani16