gpt4 book ai didi

ios - Firebase - 闭包内的用户和 currentUser nil

转载 作者:行者123 更新时间:2023-11-30 10:56:50 24 4
gpt4 key购买 nike

我在尝试获取 uid 时遇到了 Firebase 问题变量为 Auth.auth().currentUser?.uid似乎返回零。

我在函数周围放置了各种打印语句(我在下面发布了完整的函数)以查看它们是否被调用 - 如果我在 if let user = user 中放置打印语句,它不会被调用。如果我注释掉if let user = user (及其右括号),打印语句被成功调用,所以我猜 user为零。

现在,如果我将打印放在 guard let uid = Auth.auth().currentUser?.uid else {return} 之后, print 语句再次没有被调用,让我相信 uid为零并且守卫语句正在返回。

我不知道为什么userAuth.auth().currentUser?.uid这里都为零。我如何更改此设置以成功获取我的 uid变量?

这是提供一些上下文的完整函数:

@IBAction func emailSignupNextPressed(_ sender: Any) {
// Make sure text fields aren't empty
guard nameField.text != "", emailField.text != "", passwordField.text != "", confirmPasswordField.text != "" else {return}

if passwordField.text == confirmPasswordField.text {
Auth.auth().createUser(withEmail: emailField.text!, password: passwordField.text!, completion: { (user, error) in

if let error = error {
print(error.localizedDescription)
}

if let user = user {

guard let uid = Auth.auth().currentUser?.uid else {return}

// Use name as Firebase display name for readability
let changeRequest = Auth.auth().currentUser!.createProfileChangeRequest()
changeRequest.displayName = self.nameField.text!
changeRequest.commitChanges(completion: nil)

// Create child node from userStorage "users". Profile image set to user's unique ID
let imageRef = self.userStorage.child("\(uid).jpg")
let data = UIImageJPEGRepresentation(self.selectProfileImageView.image!, 0.5)

// Upload image to Firebase
let uploadTask = imageRef.putData(data!, metadata: nil, completion: { (metadata, err) in
if err != nil {
print(err!.localizedDescription)
}
imageRef.downloadURL(completion: { (url, er) in
if er != nil {
print(er?.localizedDescription as Any)
}
if let url = url {


emailUserPicString = url.absoluteString
print("\n\n\npic:\(emailUserPicString)\n\n\n")

if emailUserPicString == "" {

let alertController = UIAlertController(title: "Profile Picture Error", message: "Don't forget to choose a profile picture!", preferredStyle: UIAlertControllerStyle.alert)

let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: {(alert :UIAlertAction!) in
})
alertController.addAction(okAction)

self.present(alertController, animated: true, completion: nil)

return

} else {
self.performSegue(withIdentifier: "emailToSetup", sender: nil)
}





}
})
})
uploadTask.resume()
}
})
} else {
print("Passwords don't match")
passwordAlert()
}
}

最佳答案

auth().currentUser 仅当用户登录时才会返回 user

您刚刚创建了该用户,因此尚未登录。

您通常可以从传递到 block 中的user获取uid,例如:

let uid = user.uid

如果 user 为空,则帐户创建失败,您应该检查错误。我注意到你正在使用

print(error.localizedDescription)

但您可能想使用:

print(error)

如果错误没有本地化描述。

通常,错误要么意味着网络问题,要么用户已经拥有帐户。

<小时/>

顺便说一句,如果您想让用户登录,您可以使用

Auth.auth().signIn(withEmail: email, password: password) { … }

关于ios - Firebase - 闭包内的用户和 currentUser nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53873999/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com