How to detect changes in internet connectivity in iOS in delegate style?

I need a function to start only when the system detects that there is no Internet connection, and then another function to start when the system detects an Internet connection.

I am thinking of something like this:

func onInternetConnection() { //Enable actions } func onInternetDisconnection() { //Disable actions, alert user } 

I will also need a way to detect when the system reconnects, so I can tell the user that he is reconnecting, as in Facebook Messenger.

How can i do this?

I use Moya / Alamofire for my network layer.

+5
source share
2 answers

It works with Alamofire.

 import Alamofire // In your view did load or in app delegate do like this let reachabilityManager = NetworkReachabilityManager() reachabilityManager.listener = { status in switch status { case .notReachable: print("The network is not reachable") self.onInternetDisconnection() case .unknown : print("It is unknown whether the network is reachable") self.onInternetDisconnection() // not sure what to do for this case case .reachable(.ethernetOrWiFi): print("The network is reachable over the WiFi connection") self.onInternetConnection() case .reachable(.wwan): print("The network is reachable over the WWAN connection") self.onInternetConnection() } } 
+3
source

Alamofire and Rechability are a library and have some features for checking Internet connectivity. You can use one of them.

0
source

All Articles