Reachability.swift icon indicating copy to clipboard operation
Reachability.swift copied to clipboard

Inability to fire off Notifications beyond a disconnect...

Open bostonareahuman opened this issue 7 years ago • 24 comments

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
            
        }
    }

bostonareahuman avatar Jan 29 '18 02:01 bostonareahuman

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.

annjawn avatar Jan 29 '18 06:01 annjawn

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.

annjawn avatar Jan 29 '18 06:01 annjawn

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.

bostonareahuman avatar Jan 29 '18 13:01 bostonareahuman

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.

annjawn avatar Jan 29 '18 17:01 annjawn

I just tested with my device (Xcode 9, ios 11, iPhone 6s) and it's working perfectly.

annjawn avatar Jan 29 '18 17:01 annjawn

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()

bostonareahuman avatar Jan 29 '18 17:01 bostonareahuman

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.

annjawn avatar Jan 29 '18 18:01 annjawn

I guess I'll just have to live with it for now.....

bostonareahuman avatar Jan 29 '18 18:01 bostonareahuman

Yeah this is definitely an issue on the simulator. I'm trying the var vs let workaround now.

nick-iCars avatar May 09 '18 18:05 nick-iCars

Nope did not work.

nick-iCars avatar May 09 '18 18:05 nick-iCars

@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 avatar May 09 '18 18:05 annjawn

@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 avatar May 09 '18 18:05 nick-iCars

@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.

annjawn avatar May 09 '18 18:05 annjawn

Ok I think it is working as expected on device. Any idea what might cause the notification to become deregistered on the simulator?

nick-iCars avatar May 09 '18 19:05 nick-iCars

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.

annjawn avatar May 09 '18 19:05 annjawn

I am using Xcode 9.4.1, built on an iPad mini with ios 12.0, it doesn't work

wiwi3 avatar Jan 17 '19 17:01 wiwi3

same issue for me

nenzitre avatar Jan 21 '19 07:01 nenzitre

Notifications not fired for me as well :(

Md-Rais avatar Jun 25 '19 12:06 Md-Rais

Have you tried the sample app in the repo? What happens?

ashleymills avatar Jun 25 '19 12:06 ashleymills

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)
    }

Md-Rais avatar Jun 25 '19 12:06 Md-Rais

Thats strange - it compiles and runs fine for me. What version of Xcode are you using?

ashleymills avatar Jun 25 '19 12:06 ashleymills

Its the latest one: Version 10.2.1 (10E1001)

Md-Rais avatar Jun 25 '19 12:06 Md-Rais

And you're using the sample app in the latest commit? https://github.com/ashleymills/Reachability.swift/tree/v5.0.0-beta1

ashleymills avatar Jun 25 '19 13:06 ashleymills

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?

Md-Rais avatar Jun 25 '19 14:06 Md-Rais