0
votes

Test Use Case:

In my scenario, I initialize my App in Xcode, login to firebase and run my app successfully. I then stop the debugger in Xcode, and then "turn Wifi off" on my MAC. I then initialize my App again in Xcode.

In the debugger, I see my code initialize an authentication listener and initialize based on the previously cached value of authenticated user information.

I also see the following exception in the console log.

2017-06-02 09:29:21.281 MusicPoll[7053] [Firebase/Core][I-NET901017] Encounter network error. Code, error: -1009, Error Domain=NSURLErrorDomain Code=-1009 "The Internet connection appears to be offline." UserInfo={NSUnderlyingError=0x60800005f7d0 {Error Domain=kCFErrorDomainCFNetwork Code=-1009 "(null)" UserInfo={_kCFStreamErrorCodeKey=50, _kCFStreamErrorDomainKey=1}}, NSErrorFailingURLStringKey=https://play.googleapis.com/log, NSErrorFailingURLKey=https://play.googleapis.com/log, _kCFStreamErrorDomainKey=1, _kCFStreamErrorCodeKey=50, NSLocalizedDescription=The Internet connection appears to be offline.}

Since I am not connected to the network, I would like to detect this condition and ask the user to check his/her network connection and try again.

My question is which Firebase method should I used to check network connectivity and perhaps obtain an error. (I am unable to find an error code that might be returned in the listener's callback.)

My Code:

...

fileprivate var authListener : FIRAuthStateDidChangeListenerHandle!

FUIAuth.defaultAuthUI()?.providers = [FUIGoogleAuth()]

authListener = FIRAuth.auth()?.addStateDidChangeListener { [weak self] (auth: FIRAuth, user: FIRUser?) in

        guard let strongSelf = self else { return }

        if let activeUser = user {

            strongSelf.hasUserBeenAuthenticated = true
            strongSelf.user = activeUser

        } else {

            strongSelf.hasUserBeenAuthenticated = false
            strongSelf.user = nil
        }

        print("\nFirebaseMgr[setupAuthorization]: hasUserBeenAuthenticated = \(strongSelf.hasUserBeenAuthenticated), user = \(String(describing: strongSelf.user))")
   }
1

1 Answers

0
votes

You can check the status of the user's internet connection using Firebase's FIRDatabase.database().reference(withPath: ".info/connected") method. This method will observe any changes in network connectivity. Here is an example:

    //this is a strong reference to the internet connection handle
    var internetConnectionHandle: UInt!


    //I have created the observer for internet connectivity in viewWillAppear
    override func viewWillAppear(_ animated:Bool) {
         let connectedRef = FIRDatabase.database().reference(withPath: ".info/connected")

    internetConnectionHandle = connectedRef.observe(.value, with: { snapshot in

        if let _ = snapshot.value as? Bool {

            //use is connected to the internet. 


        }
        else {

           //user is not connected to the internet. Ask the user to check his/her network connection and try again
        }
    })