[英]“How to retrieve an url from Firebase Database BEFORE load WebView in Swift?”
[英]How to retrieve a variable from a Firebase Database in Swift
我試圖簡單地讀入結構如下所述的數據庫。 我正在嘗試讀取用戶的“ userType”,並在下面的if語句中使用它。 任何幫助表示贊賞!
SWIFT代碼:
// Create firebase reference and link to database
var dataRef : DatabaseReference?
dataRef = Database.database().reference()
let userID = Auth.auth().currentUser!.uid // Get the User's ID
// Gather user's type (Customer or Company)
/*Use this space to gather the user's type into some variable named currUserType*/
if (currUserType == "Customer"){
self.performSegue(withIdentifier: "LoginToCustomer", sender: self)
print("User: " + userID + " has been signed in!")
}
else if (currUserType == "Company"){
self.performSegue(withIdentifier: "LoginToHost", sender: self)
}
else{
self.showMessage(alertTitle: "Error",
alertMessage: "Please report the following error with a description of what lead to to the error.",
actionTitle: "Dismiss")
}
數據庫結構:
"Users" : {
"ZFH0lFe1fIb5bwSO2Q95ektD33L2" : {
"email" : "cust@test.com",
"userType" : "Customer"
}
讓dbRef = Database.database()。reference()。child(“ Users”)
class Users {var email:String? var userType:字符串?
init(email: String, userType: String) {
self.email = email
self.userType = userType
}
}
func getUsersData(handler: @escaping (_ usersArray: [Users]) -> ()) {
var usersArray = [Users]()
dbRef.observe(.value) { (datasnapshot) in
guard let usersnapshot = datasnapshot.children.allObjects as? [DataSnapshot] else { return }
for user in usersnapshot {
let email = user.childSnapshot(forPath: "email").value as! String
let userType = user.childSnapshot(forPath: "userType").value as! String
let userObj = Users(email: email, userType: userType)
usersArray.append(userObj)
}
handler(usersArray)
}
}
刷新https://firebase.google.com/docs/database/ios/read-and-write#reading_and_writing_data
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.