ios - 处理电子邮件不存在错误 - 使用 swift 的 iOS 项目

标签 ios xcode swift email firebase

这是我使用 xcode 和 swift 为我的 iOS 应用程序重置密码按钮:

//ResetPssword Button
@IBAction func ResetPassword(sender: AnyObject) {

    if validateEmail(EmailTextField.text!) ==  false {
        print("Enter a Valid Email Address")
        let VaildMessage = "Enter an Email Address"
        //Empty TextField Alert Message
        self.disaplayErrorMessage(VaildMessage)
   }

    //Reset
    else {

        ref.resetPasswordForUser(EmailTextField.text) { (ErrorType) -> Void in
            if ErrorType != nil {

                let error = ErrorType
                print("There was an error processing the request \(error.description)")
                let errorMessage:String = "The Email You Entered is not Exist"
                //Error Alert Message
                self.disaplayErrorMessage(errorMessage)

            } else {

                print("Password Reset Sent Successfully")
              if let Email = self.EmailTextField.text {
                let successMessage = "Email Message was Sent to You at \(Email)"

                //Success Alert Message
                    self.disaplayErrorMessage(successMessage) }

            }

    } //reset

    } //Big Else

 } //Button


//Display Alert Message With Confirmation
func disaplayErrorMessage(theMessage:String)
{
    //Display alert message with confirmation.
    let myAlert = UIAlertController(title: "Alert", message: theMessage, preferredStyle: UIAlertControllerStyle.Alert);

    let OkAction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default) {
        action in
        self.dismissViewControllerAnimated(true, completion: nil);
    }
    myAlert.addAction(OkAction);
    self.presentViewController(myAlert, animated: true, completion: nil)
}


   //Validate Email Function
func validateEmail(candidate: String) -> Bool {
    let emailRegex = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,6}"
    return NSPredicate(format: "SELF MATCHES %@", emailRegex).evaluateWithObject(candidate)
}

我想犯一个错误,当 firebase 中不存在电子邮件时向用户发出警报。 我不知道该怎么做。 我做了什么,当错误 != nil 时我给了用户一个警告 这是正确的方法吗?

最佳答案

firebase 网站有完整的错误代码列表

User Authentication

预定义的错误代码提供了有关您在验证或处理用户时可能收到的几乎所有错误的信息。

    ref.resetPassword({
         email: EmailTextField.text!
    }, function(error) {

        if (error != nil) {

            if let errorCode = FAuthenticationError(rawValue: error.code) {
                switch (errorCode) {
                case .UserDoesNotExist:
                    println("Handle invalid user")
                case .InvalidEmail:
                    println("Handle invalid email")
                case .InvalidPassword:
                    println("Handle invalid password")
                default:
                    println("Handle default situation")
                }
            }
        } else {
            // Success!
        }
    }

关于ios - 处理电子邮件不存在错误 - 使用 swift 的 iOS 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35957273/

相关文章:

ios - ConvertRect 没有按预期工作

ios - XMPPFramework - 'didReceiveMessage' 在收到消息时调用两次

ios - 以编程方式创建 uiTabBarController

ios - 在 native iOS 应用程序中集成响应式 html 网页并使用 Apache Cordova 进行通信

xcode - 在 Xcode 下进入应用程序运行

ios - 带条件的 Swift 过滤器

ios - 如何解决 Type of expression is ambiguous without more context for an audio recorder in swift 2

iphone - 停止闲置一段时间后,IOS音频将无法工作

ios - 有没有一种快速扫描 iOS 7 应用程序以获取 iOS 8 API 的方法?

ios - 如何在 Swift 中更改 UILabel 的字体大小?