Skip to content

Instantly share code, notes, and snippets.

@zontan
Created September 23, 2019 18:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zontan/c9d0e38e109f697894cd858c99947425 to your computer and use it in GitHub Desktop.
Save zontan/c9d0e38e109f697894cd858c99947425 to your computer and use it in GitHub Desktop.
@IBAction func didTapSave(_ sender: Any) {
guard let user = Auth.auth().currentUser else {
showErrorAlert(error: nil)
return
}
let newName = nameTextField.text
let changeRequest = user.createProfileChangeRequest()
changeRequest.displayName = newName
changeRequest.commitChanges { (error) in
if let error = error {
self.showErrorAlert(error: error)
} else {
let ref = Database.database().reference()
ref.child("users/\(user.uid)/username").setValue(newName?.lowercased())
ref.child("users/\(user.uid)/displayname").setValue(newName)
let alert = UIAlertController(title: "Success", message: "Your changes have been saved.", preferredStyle: .alert)
self.present(alert, animated: true)
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1, execute: {
alert.dismiss(animated: true, completion: {
self.navigationController?.popViewController(animated: true)
})
})
}
}
}
func showErrorAlert(error: Error?) {
if let error = error {
print(error.localizedDescription)
}
let alert = UIAlertController(title: "Error", message: "Something went wrong. Please try again.", preferredStyle: .alert)
self.present(alert, animated: true)
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1, execute: {
alert.dismiss(animated: true)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment