[英]Swift Firebase Authentication - two questions about error handling (I'm not sure how to name these errors)
It's really hard to find a proper title for this question.真的很难为这个问题找到一个合适的标题。 Please be easy on me.请对我轻松一点。 The first part is a check to see if an account exists:第一部分是检查帐户是否存在:
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;
}
})
As you can see, I have something named Unhandled error with message Undefined error.如您所见,我有一个名为 Unhandled error 的消息,其中包含消息 Undefined error。 I don't know how to name it properly.我不知道如何正确命名它。 Can somebody explain that part to me?有人可以向我解释那部分吗?
The second one is about getting a localized string - any ideas to make it fancy?第二个是关于获得一个本地化的字符串 - 有什么想法可以让它花哨吗?
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)");
}
}
Thanks for tips :)感谢您的提示:)
You can handle the Errors
this way:您可以通过以下方式处理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
}
}
Here I am getting the errCode
using AuthErrorCode
provided by Firebase
itself and then, I am passing in the received error code using error._code
.在这里,我得到errCode
使用AuthErrorCode
提供Firebase
本身,然后,我在收到错误代码使用合格error._code
。 So, now I can get the type of AuthErrorCode
.所以,现在我可以获得AuthErrorCode
的类型。 Using this I am making cases like .emailAlreadyInUser
, .accountExistsWithDifferentCredential
etc. You can just type .
使用这个我正在制作像.emailAlreadyInUser
、 .accountExistsWithDifferentCredential
等的案例。你可以只输入.
and it will show you all the AuthErrorCodes
.它会显示所有的AuthErrorCodes
。 So, you can simply handle the error codes in this way.因此,您可以通过这种方式简单地处理错误代码。
Now, coming to the second part of the question, ie getting localized string
.现在,进入问题的第二部分,即getting localized string
。 You can add localization to Firebase
, for that you have to select the language code.您可以向Firebase
添加本地化,为此您必须选择语言代码。 Auth.auth().languageCode = "en" //For English
. Auth.auth().languageCode = "en" //For English
。 But, I do not think that it gives localized errors as there are many more languages than what Firebase
supports.但是,我不认为它会导致本地化错误,因为Firebase
支持的语言要多得多。 This mainly for sending localized
emails.这主要用于发送localized
电子邮件。
To handle the localization
, you have to create your own method as I did.要处理localization
,您必须像我一样创建自己的方法。 You can see that I have called a function displayAlertMessage
in which I am passing the titleStr: LocalizeConstant.CommonTitles.Alert.rawValue.localizedStr()
, which is a part of localization.您可以看到我调用了一个函数displayAlertMessage
,我在其中传递了titleStr: LocalizeConstant.CommonTitles.Alert.rawValue.localizedStr()
,这是本地化的一部分。
struct LocalizeConstant {
enum CommonTitles: String
{
case Alert = "common_alert"
}
}
This value designates to the key
given by me in the localization file.该值指定我在本地化文件中给出的key
。 If you do not know about localization, you have to do a Google search on it.如果您不了解本地化,则必须对其进行 Google 搜索。 Let's say I have two Localizable.strings
one is in English
and the other one is in French
.假设我有两个Localizable.strings
一个是English
,另一个是French
。 In Localizable.strings(English)
, I've written Alert
like this:在Localizable.strings(English)
,我写了这样的Alert
:
"common_alert" = "Alert";
And, In French:而且,在法语中:
"common_alert" = "Alerte!";
So, this is how I have manually added localization
in my app.所以,这就是我在我的应用程序中手动添加localization
。 But, to achieve this you have to do two things.但是,要实现这一点,您必须做两件事。 1) You have to set up your appLanguage
. 1) 您必须设置您的appLanguage
。 2) You have to call a method which will fetch the values from these keys defined in the Localizable.strings
file. 2) 您必须调用一个方法,该方法将从Localizable.strings
文件中定义的这些键中获取值。
To do this, I have created a method localizedStr()
.为此,我创建了一个localizedStr()
方法。 It is an extension to String
and you can use it as follows.它是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
}
}
Now, this method localizedStr()
will give you a localized string according to your app language.现在,此方法localizedStr()
将根据您的应用程序语言为您提供本地化的字符串。 Even, if Firebase
provides localized error codes(which I think it does not), it is impossible to get the error description in each language.即使Firebase
提供了本地化的错误代码(我认为它没有),也无法获得每种语言的错误描述。 So this is the best way I came up with.所以这是我想到的最好的方法。 It may not be the best method out there in the world, but it does the task.它可能不是世界上最好的方法,但它可以完成任务。
PS: To optimize this throughout the app, either you can create an extension to AuthErrorCode
or you can create a Helper
function where you will just pass the error._code
and it will return the localized string
. PS:要在整个应用程序中对此进行优化,您可以创建AuthErrorCode
的扩展,也可以创建一个Helper
函数,您只需传递error._code
并返回localized string
。 I've added the long way so that you can understand everything in the best way.我已经添加了很长的路,以便您可以以最好的方式理解所有内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.