如何使用 Firebase 在 Swift 3 中发送验证邮件?

How can I send a verification email in Swift 3 with Firebase?

当用户尝试在 Swift 3?到目前为止我有这个代码:

@IBAction func CreateAccount(_ sender: AnyObject) {

    let username = UserText.text
    let password = PasswordText.text

    FIRAuth.auth()?.createUser(withEmail: username!, password: password!, completion: { (user, error) in
        if error != nil {

            //error creating account
            let alert = UIAlertController(title: "Error", message: "Error creating account", preferredStyle: .alert)
            alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
            self.present(alert, animated: true, completion: nil)
        } else {
            //success
            let vc = self.storyboard?.instantiateViewController(withIdentifier: "mainVC")
            self.present(vc!, animated: true, completion: nil)
        }
    })
}

我想要的是确认用户使用的是有效的电子邮件。

Firebase documentation on sending a password reset email 所示:

You can send a password reset email to a user with the sendPasswordResetWithEmail:completion: method. For example:

FIRAuth.auth()?.sendPasswordReset(withEmail: userInput) { (error) in
  // ...
}

不知道你是否做到了,但我会用这个:

final FirebaseUser user = mAuth.getCurrentUser();
user.sendEmailVerification()
    .addOnCompleteListener(this, new OnCompleteListener() {
        @Override
        public void onComplete(@NonNull Task task) {
            // Re-enable button
            findViewById(R.id.verify_email_button).setEnabled(true);

            if (task.isSuccessful()) {
                Toast.makeText(EmailPasswordActivity.this,
                         "Verification email sent to " + user.getEmail(),
                         Toast.LENGTH_SHORT).show();
             } else {
                 Log.e(TAG, "sendEmailVerification", task.getException());
                 Toast.makeText(EmailPasswordActivity.this,
                          "Failed to send verification email.",
                          Toast.LENGTH_SHORT).show();
             }
    }