ios - 通过 UIAlertController 登录/注册

标签 ios swift uialertcontroller

我正在尝试通过单击登录或注册按钮后弹出的警报来创建登录/注册。我正在尝试将它实现到 Firebase 4 中。到目前为止我已经有了这个,但有点停留在如何实现 Firebase 上。我已经阅读了他们的文档,但我似乎无法让 alertView 正常工作。有什么建议吗?

import UIKit
import Firebase

class welcomeController: UIViewController {

    alertController.addTextFieldWithConfigurationHandler { textField in
        textField.placeholder = "Email"
        textField.keyboardType = .EmailAddress
    }

    alertController.addTextFieldWithConfigurationHandler { textField in
        textField.placeholder = "Password"
        textField.secureTextEntry = true
    }

    alertController.addTextFieldWithConfigurationHandler { textField in
        textField.placeholder = "Password Confirmation"
        textField.secureTextEntry = true
    }
    alertController.addAction(loginAction)
    alertController.addAction(forgotPasswordAction)
    alertController.addAction(cancelAction)
}

最佳答案

请引用以下代码。为了演示登录和注册,我添加了登录和注册操作。为了更好地理解,我在登录和注册操作中提到了重复的代码。此代码仅供您理解。

let alertController = UIAlertController(title: nil, message: "Login/Signup", preferredStyle: .alert)

    alertController.addTextField { (textField) in
        textField.placeholder = "Email"
        textField.keyboardType = .emailAddress
    }

    alertController.addTextField { (textField) in
        textField.placeholder = "Password"
        textField.isSecureTextEntry = true
    }

    alertController.addTextField { (textField) in
        textField.placeholder = "Password Confirmation"
        textField.isSecureTextEntry = true
    }

    let loginAction = UIAlertAction(title: "Login", style: .default) { (_) in
        let emailField = alertController.textFields![0]
        let passwordField = alertController.textFields![1]
        let conformPasswordField = alertController.textFields![2]

        //Perform validation or whatever you do want with the text of textfield

        //Login With Firebase
        Auth.auth().signIn(withEmail: emailField.text!, password: passwordField.text!) { (user, error) in
            // ...
        }

    }

    let signupAction = UIAlertAction(title: "Signup", style: .default) { (_) in
        let emailField = alertController.textFields![0]
        let passwordField = alertController.textFields![1]
        let conformPasswordField = alertController.textFields![2]

        //Perform validation or whatever you do want with the text of textfield

        //SigunUp With Firebase
        Auth.auth().createUser(withEmail: emailField.text!, password: passwordField.text!) { (user, error) in
            // ...
        }
    }

    let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
    alertController.addAction(loginAction)
    alertController.addAction(signupAction)
    alertController.addAction(cancelAction)

    DispatchQueue.main.async {
        self.present(alertController, animated: true, completion: nil)
    }

要了解有关 firebase 身份验证和使用 firebase 的登录/注册过程的更多信息,请引用以下链接: https://firebase.google.com/docs/auth/ios/password-auth

关于ios - 通过 UIAlertController 登录/注册,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48707059/

相关文章:

swift - 在 UIAlertController 给出 _BSMachError 之后呈现 UIActivityViewController

ios - 如何以编程方式点击 Tabbar 项目(不是 selectedIndex)

ios - iPad 仅在设备上扭曲 UIImageView

iOS - 在不卡住 UI 的情况下创建大量 UIImageView

ios - 是否可以为 App Store 上传 iOS6 和 iOS7 的屏幕截图?

objective-c - 按字母顺序对 NSMutableArrays 进行排序

ios - 蓝牙 BLE 设备一连接就断开连接?

swift - 始终将数据保存在第一个 Cell UICollectionView 中

ios - Example-Swift.h 未找到

ios - UIAlertController 按钮位置不一致