0

I was in a situation where the router with the wifi was plugged in but the router wasn't connected to the internet (no wifi). Whatever reachability class I was using at the time thought it was connected because wifi was available but it couldn't determine that the wifi itself couldn't get a connection.

I now use Ashley Mills Reachability and it works fine because it can tell wether I'm connected to the internet or not by pinging a host name.

let reachability = Reachability(hostname: "www.google.com")

reachability.whenReachable = { (reachability) in

    // connection is fine remove no connection alert if it's on screen
}
reachability.whenUnreachable = { (reachability) in

    // can't ping Google so alert no connection
}

Firebase has similar feature:

let connectedRef = Database.database().reference(withPath: ".info/connected")
connectedRef.observe(.value, with: { (connected) in

    if let boolean = connected.value as? Bool, boolean == true {

          // connection is fine remove no connection alert if it's on screen
     else {

          // can't ping Firebase so alert no connection
     }
})

The question is can the above Firebase feature tell if my wifi is on (router plugged in) but the wifi itself is not connected to the internet (no wifi) like AshleyMills does?

Lance Samaria
  • 17,576
  • 18
  • 108
  • 256

1 Answers1

0

Really late to the party, but I can confirm the Firebase feature does in fact work properly. It checks for connection to the Firebase Database. So, if your wifi is on but no internet, then Firebase will say there's no internet.

It's remarkably simple. You are testing a connection to Firebase. If it can't reach Firebase, it doesn't matter if there's wifi or cell data or anything else. If it can't reach it, it can't reach it, and it will tell you there's no connection.

Hope that helps someone in the future. Oh, and I've been using this code in my production app for over a year.

Phontaine Judd
  • 428
  • 7
  • 17
  • One more side note. I've noticed that the `".info/connected"` code doesn't fire immediately, a la Ashley Mills 'Reachability' code. Rather, there's a delay. I can't find the Swift documentation to support this, but the Android documentation says: _On Android, Firebase automatically manages connection state to reduce bandwidth and battery usage...Firebase closes the connection after 60 seconds of inactivity_. From my tests, there seems to be roughly a 60-second delay once internet connection is lost before the code fires. Just FYI. – Phontaine Judd Feb 25 '20 at 20:34
  • Hi, I was busy all day and couldn’t respond. Thanks for the answer! I found another post that said there’s a certain way fb’s won’t work. I have to try and remember. It didn’t seem like it was anything you’d have to worry about with the average everyday user. Ashley Mills works really good but there’s a lot to set up, fb is 1-2-3. Apple also has NWConnection but I haven't tried it yet. Look at this blog post, it looks simple https://www.agnosticdev.com/blog-entry/swift-networking/networkconnectivity-swift-package-connectivity-state – Lance Samaria Feb 26 '20 at 04:14
  • I found Ashley Mills really complicated and surprisingly cumbersome. I would think a simple check for connectivity would be more elegant. And Apple's own code is really clunky as well. I looked into NWConnection, but it's for iOS 12 and above, and I have apps that require iOS 10, so that's out, too. I guess I liked the simplicity of the FB code, and I haven't found a problem yet. – Phontaine Judd Feb 26 '20 at 04:21
  • If you can find the potential issue with the `".info/connected"` code, I'd like to read up on that. I'd hate to have a problem in the future. – Phontaine Judd Feb 26 '20 at 04:22
  • Yes Ashley Mills was extremely difficult to get working correctly. Once I find the FB issue I’ll post in the comments. I ran across it the other day I have to see if I can find it again – Lance Samaria Feb 26 '20 at 04:25
  • This isn't the post but I just came across this: https://stackoverflow.com/a/45293448/4833705 – Lance Samaria Feb 26 '20 at 04:31
  • I've read that post. By the illustrious Frank van Puffelen, no less. And what he says is exactly what I was looking for: _"Firebase's `.info/connected` only signals whether your app is connected to its Firebase Database backend. It does not detect general network connectivity."_ My app relies solely on the connection to Firebase, and so that solution works perfectly for me. – Phontaine Judd Feb 26 '20 at 06:38
  • 1
    Lol yeah Frank is that dude! Your needs are specific. I need my app to respond to all situations even when not connecting to the db. For eg a router is plugged in but it’s not connected to the internet. An app will still think it has an internet connection. It happened to before and I was stumped until someone explained it to me. As soon as there is no connection I want my users to immediately know. – Lance Samaria Feb 26 '20 at 06:41
  • All things being equal, though, if your app can connect to Firebase, then it can probably also connect to the other parts of the Internet. – Phontaine Judd Feb 27 '20 at 00:35
  • it makes sense but from what Frank said it looks like that's not the best way to determine the overall connection. I get your point though, if it isn't broken why fix it. Thanks for the answer, it's definitely easier to implement then AshlyMills. Cheers!!! – Lance Samaria Feb 27 '20 at 00:37