我还有另一个swift文件,其中存储了Firebase的所有当前用户信息,但是由于某种原因它无法正常工作,因此好像没有任何数据。如果有人可以帮助我找到我在做错的事情,我将非常感激。
class UserService {
static var currentUserProfile:UserProfile?
static func observeUserProfile(_ uid: String, completion: @escaping ((_ userProfile: UserProfile?)-> ()) ) {
let userRef = Database.database().reference().child("users/\(uid)")
userRef.observe(.value) { (snapshot) in
var userProfile:UserProfile?
if let dict = snapshot.value as? [String: Any],
let username = dict["username"] as? String,
let photoUrl = dict["photoUrl"] as? String,
let url = URL(string: photoUrl) {
userProfile = UserProfile(uid: snapshot.key, username: username, photoUrl: url)
}
completion(userProfile)
}
}
}
然后我使用用户名和photoUrl在我的setValue函数中使用它...
func sendDataToDatabaseFound(address: String, breed: String, phone: String, photoUrl: String, timestamp: String) {
//por the time being photoUrl:String - wasn't added because I can't get the downloadURL
// func sendDataToDatabase(address: String, breed: String, phone: String, lostfound: String, photoUrl: String)
var ref: DatabaseReference!
ref = Database.database().reference()
let postsReference = ref.child("posts").child("found")
let newPostId = postsReference.childByAutoId().key
let newPostReference = postsReference.child(newPostId)
// Still need to add the "photo": photoUrl --> newPostReference.setValue(["photoUrl": photoUrl]
let toId = Auth.auth().currentUser?.uid
newPostReference.setValue( ["address": address, "breed": breed, "phone": phone,"photoUrl": photoUrl, "timestamp": timestamp, "author": ["userid": toId, "username": UserService.currentUserProfile?.username, "profilePhotoUrl": UserService.currentUserProfile?.photoUrl.absoluteString]]) { (error, ref) in
if error != nil {
ProgressHUD.showError(error!.localizedDescription)
return
}
答案 0 :(得分:2)
如果查看Auth.auth().currentUser
的代码,您会发现它属于FIRUser
类,在Swift中被称为User
。 FIRUser
是FIRUserInfo
的子类,具有许多特征,包括providerID,uid,displayName,photoURL,email和phoneNumber。
您可以按照访问uid
中的currentUser
的相同方式访问所有这些数据。
if let user = Auth.auth().currentUser {
let providerID: String = user.providerID
let uid: String = user.uid
let displayName: String = user.displayName
let photoURL: String = user.photoURL
let email: String = user.email
}