Reputation: 1
Trying to get a user name from firebase and then displayed on a label. Any help? heres a sample of the code.
import Firebase
class HomeViewController: UIViewController {
@IBOutlet weak var userName: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
func getUserName(_ message:String) {
let uid = Auth.auth().currentUser?.uid
Database.database().reference().child("users").child(uid!).observeSingleEvent(of: .value, with: { (snapshot) in
if let dictionary = snapshot.value as? [String: AnyObject] {
self.userName.text = dictionary["firstname"] as? String
}
})
}
}
Upvotes: 0
Views: 453
Reputation: 2324
You have To try this code...(After Authentication , You have to create display name ("Firstname + Lastname") inside mobile app using below mentioned code )
if let currentUser = Auth.auth().currentUser {
let changeRequest = currentUser.createProfileChangeRequest()
changeRequest.displayName = "Firstname" + "Lastname"
changeRequest.commitChanges(completion: { (error) in
if let error = error {
print("--> firebase user display name error:- ", error)
}
})
}
After completion of this code we got current user displayname.
Upvotes: 0
Reputation: 2324
Swift
let currentUserName = Auth.auth().currentUser?.displayName
print(currentUserName)
Upvotes: 2