[英]Swift Firebase Authentication - two questions about error handling (I'm not sure how to name these errors)
真的很难为这个问题找到一个合适的标题。 请对我轻松一点。 第一部分是检查帐户是否存在:
Auth.auth().fetchSignInMethods(forEmail: userEmail, completion: {
(providers, error) in
if error != nil {
self.displayAlertMessage(alertTitle: "Unhandled error", alertMessage: "Undefined error #SignUpViewController_0001");
return;
} else if providers != nil {
self.displayAlertMessage(alertTitle: "Error", alertMessage: "This account is not exist.");
return;
}
})
如您所见,我有一个名为 Unhandled error 的消息,其中包含消息 Undefined error。 我不知道如何正确命名它。 有人可以向我解释那部分吗?
第二个是关于获得一个本地化的字符串 - 有什么想法可以让它花哨吗?
Auth.auth().createUser(withEmail: userEmail, password: userPassword) { user, error in if error == nil && user != nil {
self.displayAlertMessage(alertTitle: "Success", alertMessage: "Your account created successfully. We send you a verification email.", dismiss: true);
} else {
self.displayAlertMessage(alertTitle: "Firebase error", alertMessage: "(error!.localizedDescription)");
}
}
感谢您的提示:)
您可以通过以下方式处理Errors
:
Auth.auth().fetchSignInMethods(forEmail: email, completion: { (response, error) in
if let error = error, let errCode = AuthErrorCode(rawValue: error._code)
{
switch errCode {
case .emailAlreadyInUse:
GeneralHelper.sharedInstance.displayAlertMessage(titleStr: LocalizeConstant.CommonTitles.Alert.rawValue.localizedStr(), messageStr: LocalizeConstant.CommonTitles.Continue.rawValue.localizedStr())
case .accountExistsWithDifferentCredential:
GeneralHelper.sharedInstance.displayAlertMessage(titleStr: LocalizeConstant.CommonTitles.Alert.rawValue.localizedStr(), messageStr: LocalizeConstant.CommonTitles.Continue.rawValue.localizedStr())
default:
break
}
return
}
}
在这里,我得到errCode
使用AuthErrorCode
提供Firebase
本身,然后,我在收到错误代码使用合格error._code
。 所以,现在我可以获得AuthErrorCode
的类型。 使用这个我正在制作像.emailAlreadyInUser
、 .accountExistsWithDifferentCredential
等的案例。你可以只输入.
它会显示所有的AuthErrorCodes
。 因此,您可以通过这种方式简单地处理错误代码。
现在,进入问题的第二部分,即getting localized string
。 您可以向Firebase
添加本地化,为此您必须选择语言代码。 Auth.auth().languageCode = "en" //For English
。 但是,我不认为它会导致本地化错误,因为Firebase
支持的语言要多得多。 这主要用于发送localized
电子邮件。
要处理localization
,您必须像我一样创建自己的方法。 您可以看到我调用了一个函数displayAlertMessage
,我在其中传递了titleStr: LocalizeConstant.CommonTitles.Alert.rawValue.localizedStr()
,这是本地化的一部分。
struct LocalizeConstant {
enum CommonTitles: String
{
case Alert = "common_alert"
}
}
该值指定我在本地化文件中给出的key
。 如果您不了解本地化,则必须对其进行 Google 搜索。 假设我有两个Localizable.strings
一个是English
,另一个是French
。 在Localizable.strings(English)
,我写了这样的Alert
:
"common_alert" = "Alert";
而且,在法语中:
"common_alert" = "Alerte!";
所以,这就是我在我的应用程序中手动添加localization
。 但是,要实现这一点,您必须做两件事。 1) 您必须设置您的appLanguage
。 2) 您必须调用一个方法,该方法将从Localizable.strings
文件中定义的这些键中获取值。
为此,我创建了一个localizedStr()
方法。 它是String
的扩展,您可以按如下方式使用它。
extension String{
func localizedStr() -> String
{
var finalRes = ""
if let path = Bundle.main.path(forResource: Constants.appLang, ofType: "lproj") //Constants.appLang is "en" here for "English", but you can set accordingly.
{
if let bundle = Bundle(path: path)
{
finalRes = NSLocalizedString(self, tableName: nil, bundle: bundle, value: " ", comment: " ")
}
}
return finalRes
}
}
现在,此方法localizedStr()
将根据您的应用程序语言为您提供本地化的字符串。 即使Firebase
提供了本地化的错误代码(我认为它没有),也无法获得每种语言的错误描述。 所以这是我想到的最好的方法。 它可能不是世界上最好的方法,但它可以完成任务。
PS:要在整个应用程序中对此进行优化,您可以创建AuthErrorCode
的扩展,也可以创建一个Helper
函数,您只需传递error._code
并返回localized string
。 我已经添加了很长的路,以便您可以以最好的方式理解所有内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.