I'm learning both Swift and firebase by making an app. In my current firestore, I have a database with collection name Players, and inside of the collection, there is a document with auto-documentID and two fields, such as name and email. Like this. firestore image
I also use Firebase Auth, so whenever a user makes an account I put their email into firestore with field name email.
Here is the code for RegisterViewController where a user register their account with email and password.
import UIKit
import Firebase
class RegisterViewController: UIViewController {
@IBOutlet weak var emailTextField: UITextField!
@IBOutlet weak var passwordTextField: UITextField!
let db = Firestore.firestore()
override func viewDidLoad() {
super.viewDidLoad()
}
//MARK: - register a new account
@IBAction func registerPressed(_ sender: UIButton) {
if let email = emailTextField.text, let password = passwordTextField.text {
Auth.auth().createUser(withEmail: email, password: password) { authResult, error in
if error != nil {
// some codes here
} else {
// Add a new document with a generated ID
var ref: DocumentReference? = nil
ref = self.db.collection(K.FStore.playersCollection).addDocument(data: [K.FStore.emailField: email, K.FStore.nameField: ""]) { (error) in
if let err = error {
print("There was an issue storing data to firestore. \(err)")
} else {
print("Document added with ID: \(ref!.documentID)")
}
}
// pass this data
func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == K.registerToHome {
let homeVC = segue.destination as! HomeViewController
homeVC.documentID = ref?.documentID
}
}
self.performSegue(withIdentifier: K.registerToHome, sender: self)
}
}
}
}
}
I'm planning to check if the user has a name in their document name field in the next HomeViewController, so if the value of the name field in the document is ""
, I want to display an alert with a textfeild, so the user can add their name. However, in the HomeViewController, I need the documentID because I will use this code, db.collection(K.FStore.playersCollection).document(documentID).getDocument
in HomeVC, so I want to pass the user's documentID (in the RegisterVC, I was able to get the value as ref?.documentID
, so I just want to pass the value to HomeVC) to HomeVC. I tried using func prepare(for segue: UIStoryboardSegue, sender: Any?)
, but I was not able to pass the value...
Change your code to :
//Top of the your code define id
:
var id = ""
else {
// Add a new document with a generated ID
var ref: DocumentReference? = nil
ref = self.db.collection(K.FStore.playersCollection).addDocument(data: [K.FStore.emailField: email, K.FStore.nameField: ""]) { (error) in
if let err = error {
print("There was an issue storing data to firestore. \(err)")
} else {
print("Document added with ID: \(ref!.documentID)")
self.id = ref!.documentID
self.performSegue(withIdentifier: K.registerToHome, sender: self)
}
}
}
//Out of registerPressed func :
// pass this data
func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == K.registerToHome {
let homeVC = segue.destination as! HomeViewController
homeVC.documentID = self.id
}
}