2017-06-02 38 views
0

測試使用案例:火力認證監聽器和網絡連接

我的方案,我初始化我的應用程序在Xcode,登錄到火力和運行我的應用程序 成功。然後我在Xcode中停止調試器,然後在我的MAC上登錄 「關閉Wifi」。然後我再次在Xcode中初始化我的App。

在調試器中,我看到我的代碼初始化一個身份驗證偵聽器,並基於之前緩存的經過身份驗證的用戶信息的值進行初始化。

我還在控制檯日誌中看到以下異常。

2017-06-02 09:29:21.281 MusicPoll [7053] [Firebase/Core] [I-NET901017]遇到網絡錯誤。代碼,錯誤:-1009,錯誤域= NSURLErrorDomain代碼= -1009「Internet連接似乎處於脫機狀態。」的UserInfo = {NSUnderlyingError = 0x60800005f7d0 {錯誤域= kCFErrorDomainCFNetwork代碼= -1009 「(空)」 的UserInfo = {_ kCFStreamErrorCodeKey = 50,_kCFStreamErrorDomainKey = 1}},NSErrorFailingURLStringKey = https://play.googleapis.com/log,NSErrorFailingURLKey = https://play.googleapis.com/log,_kCFStreamErrorDomainKey = 1,_kCFStreamErrorCodeKey = 50,NSLocalizedDescription =因特網連接似乎不在線。}

由於我沒有連接到網絡,我想檢測此 條件,並要求用戶檢查他/她的網絡連接並重試。

我的問題是我應該使用哪種Firebase方法來檢查網絡連接並可能獲取錯誤。 (我無法找到可能在聽者的回調被 返回錯誤代碼。)

我的代碼:

...

fileprivate VAR authListener:FIRAuthStateDidChangeListenerHandle!

FUIAuth.defaultAuthUI()?提供商= [FUIGoogleAuth()]

authListener = FIRAuth.auth()?addStateDidChangeListener {[弱自](AUTH:FIRAuth,用戶:FIRUser)在

 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))") 
    } 

回答

0

您可以使用Firebase的FIRDatabase.database()。reference(withPath:「.info/connected」)方法檢查用戶互聯網連接的狀態。此方法將觀察網絡連接的任何更改。這裏是一個例子:

//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 
     } 
    }) 
+0

謝謝你的留言。我能夠進行您描述的網絡連接檢查。 –