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.