gpt4 book ai didi

ios - 在新的 Firebase 和 Swift 中处理错误

转载 作者:IT王子 更新时间:2023-10-29 05:16:20 26 4
gpt4 key购买 nike

我正在尝试使用 swift 和 firebase 在 iOS 项目中创建用户按钮时添加错误处理:

这是按钮的代码:

     @IBAction func Register(sender: AnyObject) {

if NameTF.text == "" || EmailTF.text == "" || PasswordTF.text == "" || RePasswordTF == "" || PhoneTF.text == "" || CityTF.text == ""
{
let alert = UIAlertController(title: "عذرًا", message:"يجب عليك ملىء كل الحقول المطلوبة", preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "نعم", style: .Default) { _ in })
self.presentViewController(alert, animated: true){}

} else {

if PasswordTF.text != RePasswordTF.text {
let alert = UIAlertController(title: "عذرًا", message:"كلمتي المرور غير متطابقتين", preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "نعم", style: .Default) { _ in })
self.presentViewController(alert, animated: true){}

} else {


FIRAuth.auth()?.createUserWithEmail(EmailTF.text!, password: PasswordTF.text!, completion: { user, error in
print(error)

if error != nil {

let errorCode = FIRAuthErrorNameKey

switch errorCode {
case "FIRAuthErrorCodeEmailAlreadyInUse":
let alert = UIAlertController(title: "عذرًا", message:"الإيميل مستخدم", preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "نعم", style: .Default) { _ in })
self.presentViewController(alert, animated: true){}

case "FIRAuthErrorCodeUserNotFound":
let alert = UIAlertController(title: "عذرًا", message:"المستخدم غير موجود", preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "نعم", style: .Default) { _ in })
self.presentViewController(alert, animated: true){}

case "FIRAuthErrorCodeInvalidEmail":
let alert = UIAlertController(title: "عذرًا", message:"الإيميل غير صحيح", preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "نعم", style: .Default) { _ in })
self.presentViewController(alert, animated: true){}

case "FIRAuthErrorCodeNetworkError":
let alert = UIAlertController(title: "عذرًا", message:"خطأ في الاتصال بالانترنت", preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "نعم", style: .Default) { _ in })
self.presentViewController(alert, animated: true){}

default:
let alert = UIAlertController(title: "عذرًا", message:"خطأ غير معروف", preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "نعم", style: .Default) { _ in })
self.presentViewController(alert, animated: true){}



}


} else {

FIRAuth.auth()?.signInWithEmail(self.EmailTF.text!, password: self.PasswordTF.text!, completion: { (user: FIRUser?, error: NSError?) in
if let error = error {
print(error.localizedDescription)
} else {

self.ref.child("UserProfile").child(user!.uid).setValue([
"email": self.EmailTF.text!,
"name" : self.NameTF.text!,
"phone": self.PhoneTF.text!,
"city" : self.CityTF.text!,
])
print("Sucess")
// self.performSegueWithIdentifier("SignUp", sender: nil)

}
})

} //else
})

} //Big else


} //Big Big else
}


}//end of

我不确定 switch 语句中错误的语法是否正确!

因为当我在模拟器中测试它时,它总是给我一个未知错误的默认情况!+我在文档中找不到语法: https://firebase.google.com/docs/auth/ios/errors

那么,使用新的 firebase 和 swift 添加错误处理的正确语法是什么!

最佳答案

实际上,我已经为此苦苦挣扎了很长时间,并找到了问题所在。我试过上面答案中发布的代码,error.code 行给了我一个错误。它确实与 error._code 一起工作。换句话说,对保罗的原始回答稍作修改。这是我的最终代码(不过我会针对所有错误对其进行编辑):

if let errCode = AuthErrorCode(rawValue: error!._code) {

switch errCode {
case .errorCodeInvalidEmail:
print("invalid email")
case .errorCodeEmailAlreadyInUse:
print("in use")
default:
print("Create User Error: \(error)")
}
}

关于ios - 在新的 Firebase 和 Swift 中处理错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39054162/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com