Reachability.swift
Reachability.swift copied to clipboard
Can only get the Notification to notice 1 change then it stops noticing them.
So I have an app I'm developing. I'd like to thank Mr Mills for his contribution with this library.
In my AppDelegate:
var reachability: Reachability!
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
//NotificationCenter.default.addObserver(self, selector: #selector(reachabilityChanged), name: ReachabilityChangedNotification, object: nil)
reachability = Reachability.init()
do {
try reachability.startNotifier()
print("setting up notifier")
} catch {
print("notifier failed.")
}
return true
}
Then I have a View Controller where I set up the reference to the notifier and try to listen for the changes to it. It will change the label when I disconnect the WiFi however when it reconnectes there is no change to the label and the notifier doesn't fire off the function.
class contactPrefsViewController: UIViewController {
@IBOutlet weak var labelConnected: UILabel!
var reachability2: Reachability!
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(_ animated: Bool) {
NotificationCenter.default.addObserver(self, selector: #selector(self.reachabilityChanged), name: ReachabilityChangedNotification, object: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func reachabilityChanged(notify:Notification){
let reachability2 = notify.object as! Reachability
print("doing notification");
if reachability2.isReachable{
self.labelConnected.text = "connected"
print("connected")
}else{
self.labelConnected.text = "not connected"
print("not Connected")
}
}
Any advice appreciated.
iOS 10 or less?
Simulator is running in iOS10...xCode 8 . Cocoapods with a target of 9.0
We noticed that there's a problem with iOS 10 and this framework. Unfortunately we haven't found the solution yet. Can you investigate too? follow Issue #151 and stay there in touch.
I'm a new swift developer(2 months) but I moved the startNotifier into the controller itself. It would appear it has to be a scope issue where the scope to the notifier started in delegate is destroyed upon first use.
Still not working as it should. It isn't that is isn't calling the notifier just when it is turned back on. It isn't calling the notifier at all after the inital call to it.
I noticed that the Notifier is getting called multple times when I loop back to a controller from another one...once...then twice....just overall very strange behavior. I'm going to remove it from my code until there is a remedy and just check for the reachability if I need to pull data.
I'm running in the simulator with iOS8.3 and Xcode 8, and seeing the same symptom. I get one reachability notification after startup, but no further notifications when going on or offline.
The same code worked prior to an update to Xcode 8 (i.e. under Xcode 7).
My codebase is Objective C, not Swift.
I am seeing the same behaviour, did anyone find a fix for this? Xcode 8 as well though this is a new project so I can not say if it was working in 7. Running on device through Xcode. iOS 9.3.2 is on device. Instead of NSNotifications though our set up is with closures.
run into the same problem running it on an iPad in iOS10
Still not fixed? :(
encounter the same issues too
I made a hacky fix for it. basically when it become unreachable, the self.reachabilityRef breaks internally, so what i did is creating a timer that recreate a reachabilityRef every second until it get back online. It worked for me.
i'm currently using Alamofire's NetworkReachabilityManager
func checkNetworkReachability() {
let mgr = NetworkReachabilityManager()
mgr?.listener = { status in
print("Network Status Changed: \(status)")
kNetworkNotConnected = mgr?.isReachable == true ? false : true
}
mgr?.startListening()
}
Would be awesome if this was eventually fixed...how does anyone use this?
It's not working for me either.
Environment - iPhone 8+ Simulator iOS 11.2 (Xcode 9.2).
Not working here either..
@kohdesmond : great tip, thank you! Copied the one .swift file from https://github.com/Alamofire/Alamofire/blob/master/Source/NetworkReachabilityManager.swift, dropped the mgr to ViewController scope, added code similar to your's, and on every viewDidLoad, i have initial reachability and virtually instant change notifs.. THANK YOU.
My apologies to this repo for the plug elsewhere, but it should be this simple. Cheers!