Add observer to internet connection
Suppose you want to check that your internet connectivity changes when it turns on /off in your application.You have to add the observer to your application of checking that it tells you that your internet connection is on or off. Follow this code to check
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
func applicationDidBecomeActive(_ application: UIApplication) { self.startMonitoring() } func startMonitoring() { let reachability = Reachability()! NotificationCenter.default.addObserver(self, selector: #selector(self.reachabilityChanged), name: ReachabilityChangedNotification, object: reachability) do{ try reachability.startNotifier() print("dfdffdeffeffefef") } catch { print("Could not start reachability notifier") } } |
This will start monitoring internet connection . to check your connection follow :-
1 2 3 4 5 6 7 8 9 10 11 12 |
func reachabilityChanged(notification: Notification) { let reachability = notification.object as! Reachability switch reachability.currentReachabilityStatus { case .notReachable: print("Network became unreachable") case .reachableViaWiFi: print("Network reachable through WiFi") case .reachableViaWWAN: print("Network reachable through Cellular Data") } } |