2017-08-15 93 views
0

我已經編寫了一個服務的使用,以登錄的用戶:Firebase:登錄失敗時,如何獲取錯誤代碼?

func login(email: String, password: String) -> Bool { 

    var userIsConnected = false 

    Auth.auth().signIn(withEmail: email, password: password) { (user, error) in 

     if error != nil { 

      print(type(of: error)) // Print 'NSError' 
      print(error!) 
     } 

     else { 

      userIsConnected = true 
     } 
    } 
    return userIsConnected 
} 

當我printerror我得到:

錯誤域= FIRAuthErrorDomain 代碼= 17008「的電子郵件地址是嚴重格式化爲 「。的UserInfo = {NSLocalizedDescription =電子郵件地址 格式錯誤,ERROR_NAME = ERROR_INVALID_EMAIL}

我怎樣才能獲得Code值(17008)是能夠做一些定製的行爲?

注意:在之前的FirebaseAuth版本中,我們可以簡單地使用error.code,但在上一個版本中我們不能。

回答

1

錯誤的類型是ErrorError可以鑄造到NSError

if let error = error, (error as NSError).code == 17008 { 
    // do something 
} 
0

TRY error?.localizedDescription

// to show error through alert controller 
    let alertController = UIAlertController(title: "your title", message: error?.localizedDescription, preferredStyle: .alert) 

    let defaultAction = UIAlertAction(title: AlertActionTitle, style: .cancel, handler: nil) 
    alertController.addAction(defaultAction) 
    self.present(alertController, animated: true, completion: nil) 
+0

OP是尋找與它關聯的'code',而不是消息。 – eshirima

相關問題