I am creating a basic signup and login page app that goes to the home screen when the user has successfully signed up or logged in. Here is my code:
Here is the where I call the button press function
signupButton.addTarget(self, action: #selector(signupButtonPressed), for: .touchUpInside)
This is the code to create a user and store in the database.
@objc func signupButtonPressed() {
// Check that the text fields aren't empty
if let username = username.text, let password = password.text, let email = email.text, let country = countryTextField.text {
// Create a user
func createUser(withEmail email: String, username: String, password: String, country: String) {
// Sign up user only if all the text fields are filled
Auth.auth() .createUser(withEmail: email, password: password) { (result, error) in
if result != nil {
// User found, go to main page
let mainPage = MainScreen()
self.present(mainPage, animated: true, completion: nil)
} else {
// Error: show error message
print("Failed to sign user up with error: ", error!.localizedDescription)
return
}
guard let userID = result?.user.uid else {return}
let values = ["email": email, "username": username]
Database.database().reference().child("Users").child(userID).updateChildValues(values, withCompletionBlock: {(error, ref) in
if let error = error {
print("Failed to update database values with error: ", error.localizedDescription)
return}})
}
}
}
}
The problem is that, when I press the button, nothing works. Hope someone can help me out, thanks!
@objc func signupButtonPressed() {
if let username = username.text, let password = password.text, let email = email.text, let country = countryTextField.text {
createUser(withEmail: email, username: username, password: password, country: country)
}
}
func createUser(withEmail email: String, username: String, password: String, country: String) {
...
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.