通过UIAlertController登录/注册

时间:2018-02-09 13:32:37

标签: 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)
}

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:-1)

处理动作处理程序中的事件,例如:

let loginAction = UIAlertAction(title: "SIGN UP", style: .default) { (action) in

    guard let textFields = alertController.textFields else { return } //show error

    let email       = textFields[0].text
    let password    = textFields[1].text
    let cPassword   = textFields[2].text

    print(email, password, cPassword)

}
相关问题