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 :).
source share