For an iOS app, using api call to populate data in the app. Check for internet connectivity is required to make app user experience a more better rather than not opting to detect availability of internet.
Alamofire is network library written in swift and used in iOS app development for making network requests. This network library is very popular among iOS developers. So if you are, using alamofire library then you can use NetworkReachabilityManager class provided in Alamofire to check internet connectivity in your iOS app. In this tutorial, we will learn how to check for internet connectivity in iOS swift.
How to check for internet connectivity
First install pod for Alamofire. If already installed in your project then skip this step otherwise go to Alamofire page and follow instructions for installation for the pod. If done with pod installation, follow below steps to add internet connectivity check in your iOS app
Step 1:- Create a new swift class named “NetworkManager”, using xcode menu – File -> New -> File or ⌘+N. A menu pops up and select swift file from it and name it as “NetworkManager”.
Step 2:- We will make this class as singleton, by making its initializer as private. Below is the code that makes NetworkManager.swift as singleton.
class NetworkManager { static let shared = NetworkManager() private init(){} }
Step 3:- Import Alamofire module we installed using pod in the beginning of this tutorial. Then create a variable named, isInternetReachable of type bool. Secondaly, we will create a instance of NetworkReachabilityManager provided by Alamofire.
import Alamofire class NetworkManager { static let shared = NetworkManager() private init(){} let manager = NetworkReachabilityManager(host: "www.apple.com") fileprivate var isInternetReachable = false }
Step 4:- Next we will, create a function that will monitor for network changes. Inside this function we will use manger instance of NetworkReachabilityManager to start listening for network change on main queue. Start listener has callback which return us status of the internet as soon internet status of our device changes.
func startMonitoring() { manager?.startListening(onQueue: DispatchQueue.main, onUpdatePerforming: { (status) in switch status { case .notReachable: print("network connection status - lost") self.isInternetReachable = false case .reachable(.ethernetOrWiFi): print("network connection status - ethernet/WiFI") self.isInternetReachable = true case .reachable(.cellular): print("network connection status - cellular") self.isInternetReachable = true default: self.isInternetReachable = false break } }) }
Step 5:- Finally in your AppDelegate.swift or in your app root class call startMonitoring function.
NetworkManager.shared.startMonitoring()
How to check internet in inner classes of your project
You can use isInternetReachable variable declared in NetworkManager class, as written below
print("Internet is available == \(NetworkManager.shared.isInternetReachable)")
Check for the internet status in iOS app
Now, if you are running your app on simulator then turn off and on internet of your mac and check console statements in xcode. If you are running your app on real iPhone/iPad device then turn off and on wifi from control center.
Where to go from here
In this tutorial, we learned how to check for internet connectivity in iOS app written in swift using Alamofire library. This is the very easy way to check for internet connectivity in iOS app using swift.