sách gpt4 ăn đã đi

ios - 为什么 Auth.auth().currentUser?.uid 返回一个 nil 值?

In lại 作者:搜寻专家 更新时间:2023-11-01 06:55:58 26 4
mua khóa gpt4 giày nike

我想将用户存储在他们的 Firebase 生成的 uid 下。当我尝试使用此代码时:

  func showCredentials() {
let credential = FacebookAuthProvider.credential(withAccessToken: FBSDKAccessToken.current().tokenString)
Auth.auth().signInAndRetrieveData(with: credential) { (AuthDataResult, error) in
if error != nil {
print("Something went wrong with our Facebook User: ", error)
trở lại
}
print("Successfuly logged in with our Facebook User: ", AuthDataResult)
self.performSegue(withIdentifier: "FacebookSegue", sender: self)
}
FBSDKGraphRequest(graphPath: "/me", parameters: ["fields": "id, name, email"]).start {
(connection, graphResult, error) in
if error != nil {
print("Failed to Start Graph Request:", error)
trở lại
}
print(graphResult)
let facebookDetail = graphResult as! NSDictionary
let userID = facebookDetail["id"]
let fullName = facebookDetail["name"]
let email = facebookDetail["email"]
let providerName = "Facebook"
let userValues = (["Email": email, "Full Name": fullName, "UID": userID, "Provider":providerName])
let databaseRef = Database.database().reference()
let key = databaseRef.child("node").childByAutoId().key ?? ""
let childUpdates = ["/Users/\(userID))/": userValues]
databaseRef.updateChildValues(childUpdates)
print(userID!)
print(fullName!)
let firebaseID = Auth.auth().currentUser?.uid
print(firebaseID!)
}
}

我得到的错误是:线程 1: fatal error :在我尝试打印 firebaseID 的行上展开可选值时意外发现 nil。为什么Auth.auth().currentUser?.uid 的值为nil,如何获取该值?

câu trả lời hay nhất

你同时发起了2个异步请求

Auth.auth().signInAndRetrieveDataFBSDKGraphRequest(graphPath: "/me"

第二个可能首先完成导致崩溃,因为身份验证尚未完成,因此您需要嵌套它们以确保在身份验证登录完成后访问用户

func showCredentials() {
let credential = FacebookAuthProvider.credential(withAccessToken: FBSDKAccessToken.current().tokenString)
Auth.auth().signInAndRetrieveData(with: credential) { (AuthDataResult, error) in
if error != nil {
print("Something went wrong with our Facebook User: ", error)
trở lại
}
FBSDKGraphRequest(graphPath: "/me", parameters: ["fields": "id, name, email"]).start {
(connection, graphResult, error) in
if error != nil {
print("Failed to Start Graph Request:", error)
trở lại
}
print(graphResult)
let facebookDetail = graphResult as! NSDictionary
let userID = facebookDetail["id"]
let fullName = facebookDetail["name"]
let email = facebookDetail["email"]
let providerName = "Facebook"
let userValues = (["Email": email, "Full Name": fullName, "UID": userID, "Provider":providerName])
let databaseRef = Database.database().reference()
let key = databaseRef.child("node").childByAutoId().key ?? ""
let childUpdates = ["/Users/\(userID))/": userValues]
databaseRef.updateChildValues(childUpdates)
print(userID!)
print(fullName!)
let firebaseID = Auth.auth().currentUser?.uid
print(firebaseID!)

DispatchQueue.main.async {
print("Successfuly logged in with our Facebook User: ", AuthDataResult)
self.performSegue(withIdentifier: "FacebookSegue", sender: self)
}

}


}

}

关于ios - 为什么 Auth.auth().currentUser?.uid 返回一个 nil 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53347132/

26 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress