首页 > 解决方案 > 如果电子邮件和密码在 firebase swift 中无效,如何显示错误消息?

问题描述

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

从 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()
            
            
        })

标签: swiftfirebasefirebase-authentication

解决方案


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

例如(基于此回购

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

推荐阅读