我正在尝试通过单击登录或注册按钮后弹出的警报来创建登录/注册。我正在尝试将它实现到 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/