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