首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过UIAlertController登录/注册

通过UIAlertController登录/注册
EN

Stack Overflow用户
提问于 2018-02-09 13:32:36
回答 2查看 1.4K关注 0票数 1

我试图创建一个登录/注册通过警报弹出一旦登录或注册按钮被点击。我正在尝试将它实现到Firebase 4中。到目前为止,我已经完成了这一任务,但是我还在研究如何实现Firebase。我读过他们的文档,但我似乎无法让alertView正常工作。有什么建议吗?

代码语言:javascript
复制
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)
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-02-09 14:16:29

请参考以下代码。为了演示登录和注册,我增加了登录和注册的操作。为了更好地理解,我已经在登录和注册操作中提到了重复的代码。这段代码只是为了你的理解。

代码语言:javascript
复制
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身份验证和登录/注册过程的更多信息,请参考以下链接:https://firebase.google.com/docs/auth/ios/password-auth

票数 3
EN

Stack Overflow用户

发布于 2018-02-09 13:48:54

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

代码语言:javascript
复制
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)

}
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48707059

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档