swift - 如果电子邮件和密码在 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()
})
解决方案
如果您想根据 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)
}
}
}
推荐阅读
- asp.net-core - 当前 UoW 事务成功完成后,如何发布自定义事件?
- google-apps-script - 如何在谷歌电子表格中进行数据自动更新?
- c++ - 如何在 Debian 中使用 C++ 读取/写入 SD 卡中的特定内存地址?
- linux - BASH如何处理变量内的JSON以仅删除文本的特定部分
- mpdf - mpdf:临时文件的权限问题
- android - 展开时如何防止FAB覆盖BottomSheet?
- php - 在 wp-config.php 中获取当前主题或子主题
- latex - Ghostscript 在 LaTeX 下导致编译错误
- python - 数据框附加多索引
- postfix-mta - Postfix 服务器 - 如何阻止不断尝试连接的 IP