基于Firebase与Swift 4的两步式注册:邮箱传值与Firebase提交
Instagram风格注册流程实现(Firebase + Swift 4)
没问题,我帮你一步步实现这个Instagram风格的注册流程,核心是解决页面间的邮箱传值,以及最终提交到Firebase的逻辑,下面是具体实现:
1. 第一个页面:获取邮箱并传递到密码页
首先补全你第一个ViewController的代码,这里我们用属性传值的方式把邮箱传递到下一页,这是这种连续跳转场景下最直接的方案:
import UIKit class EmailInputViewController: UIViewController { // 关联Storyboard里的邮箱输入框 @IBOutlet weak var emailTextField: UITextField! override func viewDidLoad() { super.viewDidLoad() // 设置键盘类型为邮箱,提升输入体验 emailTextField.keyboardType = .emailAddress } // 「下一步」按钮点击事件 @IBAction func nextButtonTapped(_ sender: UIButton) { // 先验证邮箱格式和非空 guard let inputEmail = emailTextField.text, !inputEmail.isEmpty, self.isValidEmail(inputEmail) else { // 提示用户输入有效邮箱 let alert = UIAlertController(title: "提示", message: "请输入正确的邮箱地址", preferredStyle: .alert) alert.addAction(UIAlertAction(title: "确定", style: .default)) present(alert, animated: true) return } // 跳转到密码输入页面,传递邮箱 guard let passwordVC = storyboard?.instantiateViewController(withIdentifier: "PasswordInputViewController") as? PasswordInputViewController else { return } // 直接给下一页的属性赋值 passwordVC.userEmail = inputEmail navigationController?.pushViewController(passwordVC, animated: true) } // 简单的邮箱格式校验方法 private func isValidEmail(_ email: String) -> Bool { let emailRegex = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,64}" let predicate = NSPredicate(format:"SELF MATCHES %@", emailRegex) return predicate.evaluate(with: email) } }
2. 第二个页面:接收邮箱并提交到Firebase
接下来是密码输入页面的代码,这里我们接收上一页传递的邮箱,结合密码提交到Firebase Auth完成注册:
import UIKit import FirebaseAuth class PasswordInputViewController: UIViewController { // 用于接收上一页传递的邮箱 var userEmail: String! // 关联Storyboard里的密码输入框 @IBOutlet weak var passwordTextField: UITextField! override func viewDidLoad() { super.viewDidLoad() // 设置密码输入框为安全模式(隐藏输入内容) passwordTextField.isSecureTextEntry = true } // 「注册」按钮点击事件 @IBAction func registerButtonTapped(_ sender: UIButton) { // 验证密码非空且长度符合Firebase要求(至少6位) guard let inputPassword = passwordTextField.text, !inputPassword.isEmpty, inputPassword.count >= 6 else { let alert = UIAlertController(title: "提示", message: "密码长度不能少于6位", preferredStyle: .alert) alert.addAction(UIAlertAction(title: "确定", style: .default)) present(alert, animated: true) return } // 调用Firebase Auth的注册接口 Auth.auth().createUser(withEmail: userEmail, password: inputPassword) { authResult, error in if let err = error { // 注册失败,弹出错误提示(Firebase会返回具体错误原因,比如邮箱已注册) let alert = UIAlertController(title: "注册失败", message: err.localizedDescription, preferredStyle: .alert) alert.addAction(UIAlertAction(title: "确定", style: .default)) self.present(alert, animated: true) return } // 注册成功,跳转到主页或其他页面 let successAlert = UIAlertController(title: "注册成功", message: "欢迎加入!", preferredStyle: .alert) successAlert.addAction(UIAlertAction(title: "去首页", style: .default, handler: { _ in // 这里替换成你的主页ViewController的标识符 guard let homeVC = self.storyboard?.instantiateViewController(withIdentifier: "HomeViewController") else { return } self.navigationController?.pushViewController(homeVC, animated: true) })) self.present(successAlert, animated: true) } } }
几个关键注意点
- 确保你已经在项目中集成了Firebase Auth:通过CocoaPods导入
Firebase/Auth,并将GoogleService-Info.plist文件添加到项目中 - 页面跳转前的校验很重要:既提升用户体验,也避免无效请求发送到Firebase
- Firebase的
createUser(withEmail:password:)方法会自动处理大部分注册逻辑,包括重复邮箱检测、密码强度验证等,错误信息会通过error返回
内容的提问来源于stack exchange,提问作者ozy
相关产品推荐
相关产品推荐

