我试图使用firebase在ios上创建一个简单的身份验证屏幕。我是通过在youtube上观看教程来实现的。我正在关注所有事情,但当我按下按钮登录我的用户时,var的值变为等于nil。因此,它无法进入下一个视图。有人知道如何解决这个问题吗?
@IBAction func signInButtonTapped(_ sender: UIButton) {
// TODO: Do some form validation on email and password
if let email = emailTextField.text, let pass = passwordTextField.text {
// Check if it's sign in or register
if isSignIn {
// Sign in the user with Firebase
Auth.auth().signIn(withEmail: email, password: pass, completion: { (user, error) in
// Check that user isn't' nil
if let u = user {
// user is found, go to homescreen
self.performSegue(withIdentifier: "goToHome", sender: self)
}
else {
// Error: check error and show message
}
})
} else {
// Register the user with Firebase
Auth.auth().createUser(withEmail: email, password: pass, completion: { (user, error) in
// Check that user isn't nil
if let u = user {
// User is found, go to homescreen
print("\(email) \(pass)")
self.performSegue(withIdentifier: "goToHome", sender: self)
} else {
// Error: check error and show message
print(error)
}
})
}
}
}
答案 0 :(得分:1)
我试过你的问题它工作正常,再次检查这个答案@bag
import UIKit
import Firebase
import FirebaseAuth
class ViewController: UIViewController {
@IBOutlet weak var emailTextField: UITextField!
@IBOutlet weak var passwordTextField: UITextField!
@IBOutlet weak var signInButton: UIButton!
var isSignIn:Bool = true
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func segmentcontroltapped(_ sender: UISegmentedControl) {
isSignIn = !isSignIn
if isSignIn {
signInButton.setTitle("signin", for: .normal)
}else{
signInButton.setTitle("Register", for: .normal)
}
}
@IBAction func sigInButtonTapped(_ sender: UIButton) {
if let email = emailTextField.text, let pass = passwordTextField.text {
// Check if it's sign in or register
if isSignIn {
// Sign in the user with Firebase
Auth.auth().signIn(withEmail: email, password: pass, completion: { (user, error) in
// Check that user isn't' nil
if let u = user {
print("\(email) \(pass)")
}
else {
print(error)
}
})
} else {
// Register the user with Firebase
Auth.auth().createUser(withEmail: email, password: pass, completion: { (user, error) in
// Check that user isn't nil
if let u = user {
// User is found, go to homescreen
print("\(email) \(pass)")
// self.performSegue(withIdentifier: "goToHome", sender: self)
} else {
// Error: check error and show message
print(error)
}
})
}
}
}
}