【问题标题】:how to display an error message if email and password is not valid in firebase swift?如果电子邮件和密码在 firebase swift 中无效,如何显示错误消息?
【发布时间】:2021-09-19 12:45:50
【问题描述】:

尝试快速开发登录页面,然后我想验证某些条件以返回错误。

从 print(Error.localizeDescription) 我得到了一些类似的回报

  • 密码无效或用户没有密码。
  • 没有与此标识符对应的用户记录。

如何根据 Error.localizeDescription 验证该条件?

Auth.auth().signIn(withEmail: email, password: pass, completion: {[weak self] result, Error in
            guard let StrongSelf = self else{
                return
            }
            
            if let Error = Error {
                print(Error.localizedDescription)
            }
            
            guard Error == nil else{
                // in here there is validation of return error (e.g. wrong password, account not exist)
                self?.reasonLabel.isHidden=false
                self?.reasonLabel.text=Error
                return
            }
            
            self!.checkinfo()
            
            
        })

【问题讨论】:

  • 嘿劳维斯。这里有更新吗? ??????

标签: swift firebase firebase-authentication


【解决方案1】:

首先,您不应该将任何属性声明为大写,如果需要,请使用小写或骆驼。 其次,如果您使用 [weak self] 并设置 guard let 条件,则使用 strong 一个来防止保留循环并尽量不要使用 force unwrap。

    Auth.auth().signIn(withEmail: email, password: pass, completion: {[weak self] result, error in
        guard let self = self else { return }

        if let error = error {
            print(error.localizedDescription)
            let alert = UIAlertController.configureAlertController(error.localizedDescription)
            self.present(alert, animated: true)
            return
        }
        // your logic

       /* or 
        if let error = error {
            print(error.localizedDescription)
            UIAlertController. showAlert(error.localizedDescription)
        }else{
         // your logic
        }
       */

       /* or
       
       guard let result = result else {
       UIAlertController. showAlert(error?.localizedDescription ?? "unknown error")
       return
       }

       // your logic
       */

       /* 
        or some else, but avoid force unwrap
       */
    })

请注意,带有错误检查的if let 语句不会阻止执行整个代码,并且将执行self!.checkinfo(),if let error.. 末尾的return 将告诉您的方法他需要停止

 extension UIAlertController {
     static func configureAlertController(with title: String = "Attention", message: String) -> (UIAlertController){
         let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
         let action = UIAlertAction(title: "ОК",  style: .default) {(action) in}
         alertController.addAction(action)
    
         return alertController
     }
 }

【讨论】:

  • 有什么原因会弹出关于 UIAlertController has no member configureAlertController
  • 不确定您的意思,为 UIAlertController 创建一个扩展并创建您的自定义方法并将其命名为 configureAlertController - 这将是您的成员。如果你不需要 UIAlertController,但需要 self.reasonLabel.isHidden = false self.reasonLabel.text = error.localizedDescription 来代替 alert
  • @lauwispremium 这部分代码不会触发编译错误? self?.reasonLabel.text=Error 应该是 self?.reasonLabel.text=Error.localizedDescription - 它是 String
【解决方案2】:

如果您想根据 Firebase 身份验证引发的错误采取特定操作,那么解析 Error.localizeDescription 并不是最好的方法。而是再次检查您在完成处理程序中获得的NSError 对象的错误code,并再次检查handling errors 上的文档中显示的值。

例如(基于此repo:

switch error {
  case let .some(error as NSError)
    where UInt(error.code) == FUIAuthErrorCode.userCancelledSignIn.rawValue:
    print("User cancelled sign-in")
  case .none:
    if let user = authDataResult?.user {
      signed(in: user)
    }
  }
}

【讨论】:

    猜你喜欢
    • 2020-04-06
    • 2012-04-23
    • 2021-04-20
    • 2018-05-18
    • 2018-02-09
    • 2014-07-14
    • 2016-06-27
    • 2014-04-14
    • 1970-01-01
    相关资源
    最近更新 更多