簡體   English   中英

Swift Firebase Authentication - 關於錯誤處理的兩個問題(我不知道如何命名這些錯誤)

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM