Reputation: 683
Hi I am a bit new to iOS development. I want to create a variable that store the UID = user.id from the firebase authentication check. Then I want to use that UID to put it in the url so that I can use that to call our backend. However, I couldn't figure out how to do it...
Xcode 11.6 swift 5
here is my swift code:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
var UID = ""
Auth.auth().addStateDidChangeListener { (auth, user) in
if let user = user {
self.userName.text = user.displayName
//print(user.uid)
UID = user.uid
} else {
self.userName.text = "error"
}
}
self.navigationItem.setHidesBackButton(true, animated: true)
//let url = vehicleManager.dataURL
let url = rewardManager.dataURL
print("UID is: \(UID)")
rewardManager.performRequest(dataURL: url)
claimWebKitView.load(URLRequest(url: URL(string: "https://camo.githubusercontent.com/6b254aa699df7f9464967009129c3017de721b77/68747470733a2f2f7261772e6769746875622e636f6d2f5068696c4a61792f4d50416e64726f696443686172742f6d61737465722f73637265656e73686f74732f7363617474657263686172742e706e67")!))
premiumWebKitView.load(URLRequest(url: URL(string: "https://camo.githubusercontent.com/e29d8d3316203700965cc6cc56e67b779f2845bb/68747470733a2f2f7261772e6769746875622e636f6d2f5068696c4a61792f4d5043686172742f6d61737465722f73637265656e73686f74732f636f6d62696e65645f63686172742e706e67")!))
}
how can I pass the user.uid to my url? thanks
Upvotes: 0
Views: 359
Reputation: 683
after the great help from @Dev App and others, I finally figured it out I think.
So the key is to call my API in side this Firebase authentication method
func getUserName() {
Auth.auth().addStateDidChangeListener { (auth, user) in
if let user = user {
self.userName.text = user.displayName
} else {
self.userName.text = "error"
}
}
}
any data in that closure, such as user.displayname or user.uid is only valid in that closure.
So, I added my callApi function into that firebase authentication function. Something like below solves the problem.
func callApiFunction() {
//some code to call the api
}
func firebaseAuth() {
// some code to do authentication, and get back some data, for example:
func getUserName() {
Auth.auth().addStateDidChangeListener { (auth, user) in
if let user = user {
self.userName.text = user.uid
// you can pass the uid here to the callApiFunction()
callApiFunction()
} else {
self.userName.text = "error"
}
}
}
}
then you can call the func firebaseAuth() in other places, like viewdidLoad() etc.
override func viewDidLoad() {
firebaseAuth()
}
Upvotes: 0
Reputation: 1107
The reason why you are not getting any value in UID
while printing is because the auth.addStateChangedListener
is an async operation that is it takes some time to get your data from firebase and in that time duration your other code gets executed first. I would suggest to use completion handlers for these type of async operations. Here is the link of S.O question from where you can get more details about completion handlers.
Quick Overview :- Your completion handler gets called when the function has completed it's execution and at that part you can do your api calls by checking if you have received the correct userId or not.
Upvotes: 1