2

I recently started working on an iOS app powered by firebase. I was wondering if using firebase's special location /.info/connected is a good way to detect if the user has internet connection or not. I know this would be usually done using reachability apis provided by apple. Heres what I have in mind: In the app delegate I will setup something like this

func application(application: UIApplication, didFinishLaunchingWithOptions
    launchOptions: [NSObject: AnyObject]?) -> Bool {
          //Configure some stuff
          let connectedRef =
          Firebase(url:"https://serengeti.firebaseio.com/.info/connected")
          connectedRef.observeEventType(.Value, withBlock: { snapshot in
            let connected = snapshot.value as? Bool
            let userDefaults = NSUserDefaults.standardUserDefaults()
            if connected != nil && connected! {
                //User is connected
                userDefaults.setBool(true, forKey: "connected")
            } else {
                //User not connected
                userDefaults.setBool(false, forKey: "connected")
            }
            userDefaults.synchronize() //Force value into NSUserDefaults
    }

Then in my function where I need to check for connectivity (for example when they click on a facebook signup button) I look for the NSUserDefaults entry

func authWithFacebook() {
    let userDefaults = NSUserDefaults.standardUserDefaults()
    if userDefaults.boolForKey("connected") == false {
        //Segue to a popup saying internet connection is required  
    } else {
      //Continue with signup
    }
}

Is this a valid and robust way to check for internet connection rather than using reachability apis. If not, in what way would it be lacking.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
kau
  • 372
  • 1
  • 3
  • 14
  • 1
    I think the question here is defining what is meant by an internet connection. Are you looking to see if Firebase is connected to the Firebase server or the device to the internet in general. Suppose the device is behind a firewall that has network traffic blocked so maybe a browser works but nothing else. What if we have one of those rare occasions that the Firebase server is offline? – Jay Feb 04 '16 at 18:37
  • @Jay what I meant was checking if device has internet connection in general. But for the user to be able to login at all, connection to firebase would be necessary as I'm using Firebase's `authWithOAuthProvider` to do facebook login. So in my specific case looks like I might be able to get away with doing it this way? – kau Feb 04 '16 at 21:17

2 Answers2

8

As Jay says: Firebase's .info/connected detects if the user is connected to the Firebase servers.

There are many reasons why the user might be connection to the internet, but not be able to reach Firebase's servers. Firebase downtime might be one of those, but there could also be all kinds of problems between the device's first connection to the internet and the Firebase servers.

But unless you're replacing iOS's "you're on the internet" icon, your users are likely more interested in if the app is connected to its back-end. If you use Firebase as the back-end for your app, .info/connected is perfect for detecting that.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • My primary purpose here is to prompt users that network is unavailable when they try to login for the first time. I'm using firebase to do login and store other info anyway. So as you said, it might be sufficient for me to check .info/connected. I would also use it to automatically sync local storage data to firebase servers if user regains connection after a temporarily loss and has made some pending updates. – kau Feb 04 '16 at 21:13
  • 2
    The .info/connected is the way to go for sure. We do that in our app and it works very well. You'll probably want to also keep a flag within the app (we use KVO) so if during the course of using the app it goes offline, the user will know. – Jay Feb 04 '16 at 22:49
4

You're using Firebase, so using their API to see whether you have a connection to them is the right way to go. In fact, in Apple's recommendation is to always try to make a connection to your web service first, and then use Reachability to diagnose any failure:

Important: The SCNetworkReachability API is not intended for use as a preflight mechanism for determining network connectivity. You determine network connectivity by attempting to connect. If the connection fails, consult the SCNetworkReachability API to help diagnose the cause of the failure.

https://developer.apple.com/library/ios/documentation/NetworkingInternetWeb/Conceptual/NetworkingOverview/WhyNetworkingIsHard/WhyNetworkingIsHard.html#//apple_ref/doc/uid/TP40010220-CH13-SW2

Ashley Mills
  • 50,474
  • 16
  • 129
  • 160