Reachability.swift
Reachability.swift copied to clipboard
Inability to fire off Notifications beyond a disconnect...
Basically the below will work till the network is disconnected. Then it will not function correctly. So if I launch emulator when wifi is enabled it tells me it is enabled...when I disconnect then it says disconnected...when I reconnect...nothing.... If I start disconnected..then nothing.....no notifications.
let reachability = Reachability()
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
NotificationCenter.default.addObserver(self, selector: #selector(self.reachabilityChanged(note:)), name: Notification.Name.reachabilityChanged, object: nil)
do{
try reachability?.startNotifier()
}
catch{
print("could not start reachability notifier")
}
}
func reachabilityChanged(note: Notification) {
print("reachabilityChanges")
let reachability = note.object as! Reachability
switch reachability.connection{
case .wifi:
print("Reachable via WiFi")
Constants.networkStatus = true
case .cellular:
print("Reachable via Cellular")
Constants.networkStatus = true
case .none:
print("Network not reachable")
Constants.networkStatus = false
}
}
facing the same issue here. I noticed that If I do anything beyond a print
statement in the switch
statement it stops working just as you said. But with just the prints, it works fine. Vey odd.
Ok, so I changed the let reachability = Reachability()
to var reachability = Reachability()
and now it works. Also look at #216 where the user got to use it correctly when using a physical device (not emulator) and the thread refers to 3 other similar issues with solutions.
Still not working in the emulator and not sure how to get it working on an attached iphone since when I pull up the control center to turn off the wifi it disconnects the app and xcode.
Can you tell me where you declared this let reachability = Reachability()
? Did you declare this inside of a class method or did you declare this in class scope as a class property? You need to hold a strong reference to this so you need to declare it as a class property. Also, which version of Xcode are you using? Xcode 9 and ios 11 should be able to handle this pretty well.
I just tested with my device (Xcode 9, ios 11, iPhone 6s) and it's working perfectly.
Were you testing in the emulator or on your hardware? I drove into work this morning and it appeared to work fine but in the emulator it just doesn't. Below is where I declare the variable to hold the reachability. Is a pain not to have it work in the emulator...
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
struct Constants {
public static var networkStatus:Bool = false
}
var overallreachability = Reachability()
I tested on both and for me it seemed to be working on both. Mine is declared exactly the way you did in AppDelegate
. A few people have reported here that it's not reliable on emulator, not sure if it's a bug or an enhancement but I don't see any PR for it. Either way, for me, it works on emulator about 80% of the time and works fine on a device.
BTW- you might want to remove that Google API key from your response above.
I guess I'll just have to live with it for now.....
Yeah this is definitely an issue on the simulator. I'm trying the var vs let workaround now.
Nope did not work.
@nick-iCars are you trying on a real device or Simulator? I use it the way I said in my production App and it works just fine. Although, in simulator the results are mixed.
@annjawn I'm using the simulator. Any suggestions on how to turn on and off network without leaving the app or using control center on an actual device? when i use control center it runs through the app lifecycle methods in app delegate so it's difficult to confirm i its working with my particular app architecture.
@nick-iCars well, the point is to use control center to test the on and off (Airplane mode). Your app delegate should be able to handle the observer on reachabilityChanged
and give you a status of reachability.connection
. Try this -
In your appDelegate -
var reachability = Reachability()
NotificationCenter.default.addObserver(self, selector: #selector(reachabilityChanged(note:)), name: .reachabilityChanged, object: reachability)
do{
try reachability?.startNotifier()
}catch{
print("could not start reachability notifier")
}
And somewhere in the AppDelegate -
@objc func reachabilityChanged(note: Notification) {
let reachability = note.object as! Reachability
switch reachability.connection {
case .wifi:
somSingleton.shared.isOffLine = false
print("Connection - wifi")
case .cellular:
somSingleton.shared.isOffLine = false
print("Connection - Cellular")
case .none:
somSingleton.shared.isOffLine = true
print("Connection - offline")
}
}
Once you have this in your project, run it on your device and simply turn on/off Airplane mode/wifi from the control center while your app is running (swipe up from the bottom) and see what the print statements show.
Ok I think it is working as expected on device. Any idea what might cause the notification to become deregistered on the simulator?
Not sure. Perhaps it’s because the simulator is actually dependent on the Mac’s WiFi. The other way you could give a try on simulator is using the Network Conditioner tool with 100% loss option, although I haven’t tried it.
I am using Xcode 9.4.1, built on an iPad mini with ios 12.0, it doesn't work
same issue for me
Notifications not fired for me as well :(
Have you tried the sample app in the repo? What happens?
Tried it just now. Getting this error when I am trying to add the observer: The code:- @objc func reachabilityChanged(_ note: Notification) { let reachability = note.object as! Reachability
if reachability.connection != .unavailable { error on this line "Binary operator '!=' cannot be applied to operands of type 'Reachability.Connection' and '_'"
updateLabelColourWhenReachable(reachability)
} else {
updateLabelColourWhenNotReachable(reachability)
}
Thats strange - it compiles and runs fine for me. What version of Xcode are you using?
Its the latest one: Version 10.2.1 (10E1001)
And you're using the sample app in the latest commit? https://github.com/ashleymills/Reachability.swift/tree/v5.0.0-beta1
I have used code snippets from the sample app and its working now. The notification fires successfully. But suppose if I want to know whether the internet is available or not from any other class in my app, what would be the short code to find it?