Swift: How can I get a listener that tells me when the connection is lost and when it will return?

I would like to call a method when my iOS application starts, but I just want to call this method when there is a connection. I found that in Objective-C you can use Reachable , but it turns out that this method is not part of Swift.

I found a pod called Reachability.swift , and I am using the example that was provided:

 override func viewWillAppear(animated: Bool) { let reachability: Reachability do { reachability = try Reachability.reachabilityForInternetConnection() } catch { print("Unable to create Reachability") return } NSNotificationCenter.defaultCenter().addObserver(self, selector: "reachabilityChanged:", name: ReachabilityChangedNotification, object: reachability) do { try reachability.startNotifier() } catch { print("This is not working.") return } } func reachabilityChanged(note: NSNotification) { let reachability = note.object as! Reachability if reachability.isReachable() { if reachability.isReachableViaWiFi() { print("Reachable via WiFi") } else { print("Reachable via Cellular") } } else { print("Not reachable") } } 

However, this does not work properly. I only work when I enter this ViewController, but not when I turn WiFi on and off.

+5
source share
1 answer

I solved this problem by declaring reachability as an instance variable of ViewController:

 var reachability: Reachability! 

Therefore, this variable should be removed from the viewWillAppear method.

Swift 2

 override func viewWillAppear(animated: Bool) { do { reachability = try Reachability.reachabilityForInternetConnection() } catch { print("Unable to create Reachability") return } NSNotificationCenter.defaultCenter().addObserver(self, selector: "reachabilityChanged:", name: ReachabilityChangedNotification, object: reachability) do { try reachability.startNotifier() } catch { print("This is not working.") return } } func reachabilityChanged(note: NSNotification) { let reachability = note.object as! Reachability if reachability.isReachable() { if reachability.isReachableViaWiFi() { print("Reachable via WiFi") } else { print("Reachable via Cellular") } } else { print("Not reachable") } } 

Swift 3 (provided by Burning )

 var reachability: Reachability! override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) reachability = Reachability() NotificationCenter.default.addObserver(self, selector: #selector(self.reachabilityChanged(_:)), name: Notification.Name.reachabilityChanged, object: reachability) do { try reachability?.startNotifier() } catch { print("This is not working.") return } } func reachabilityChanged(_ note: NSNotification) { let reachability = note.object as! Reachability if reachability.connection != .none { if reachability.connection == .wifi { print("Reachable via WiFi") } else { print("Reachable via Cellular") } } else { print("Not reachable") } } 

This worked correctly :).

+11
source

Source: https://habr.com/ru/post/1236642/


All Articles