在Swift中使用Firebase创建新用户 [英] Creating new user with firebase in Swift

查看:168
本文介绍了在Swift中使用Firebase创建新用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Firebase的新手,一直在尝试为我的应用实施注册页面。目前,我只使用电子邮件功能,只是为了测试事情。我可以创建新用户并将其存储在仪表板的登录和授权选项卡中,但是由于什么原因我无法检索UID。唯一的办法是,如果我关闭了我的应用程序并重新加载它,那么它可以访问UID。有什么建议,为什么这是?

解决方案

您的问题可能是您只创建新的用户,而不是授权。


$ b $ p




ASE_REF.createUser(email,密码:password,withValueCompletionBlock:{(error,result) - >无效

if(error == nil){
BASE_REF.authUser(email,password:password,withCompletionBlock:{ (error,authData) - >无效
if(error == nil){
NSUserDefaults.standardUserDefaults()。setValue(authData.uid,forKey:uid)

print(Account created :))

let userDict = [Name:name !,Major:major !,Email:email!]

let uid = authData.uid

usersRef.childByAppendingPath(uid).setValue(userDict)

self.dismissViewControllerAnimated(true,completion:nil)

} else {
print(error)
}
})

}
$)

BASE_REF是我的firebase参考。创建用户后,检查是否有错误。如果不是,请尝试授权用户。如果在授权中没有错误,您现在可以访问包含uid(authData.uid)的用户的authData。



希望这有助于您。


I am new to Firebase and have been trying to implement a "Sign Up" page for my app. Currently, I am only using the email feature, just to test things out. I am able to create the new user and store it in the "Login & Auth" tab in the dashboard, but for what ever reason I cannot retrieve the UID. The only way is if I close out of my app and reload it, then it can access the UID. Any suggestions on why this is?

解决方案

Your problem might be that you are only creating the new user and not authorizing.

Example of how to do both:

     BASE_REF.createUser(email, password: password, withValueCompletionBlock: { (error, result) -> Void in

            if (error == nil) {
                BASE_REF.authUser(email, password: password, withCompletionBlock: { (error, authData) -> Void in
                    if (error == nil) {
                        NSUserDefaults.standardUserDefaults().setValue(authData.uid, forKey: "uid")

                        print("Account created :)")

                        let userDict = ["Name": name!, "Major": major!, "Email": email!]

                        let uid = authData.uid

                        usersRef.childByAppendingPath(uid).setValue(userDict)

                        self.dismissViewControllerAnimated(true, completion: nil)

                    } else {
                        print(error)
                    }
                })

            }
        })

BASE_REF is my firebase reference. After creating the user, check if there was an error. If not, try to authorize the user. If there was no error in authorizing, you now have access to the user's authData which contains the uid (authData.uid)

Hope this helps.

这篇关于在Swift中使用Firebase创建新用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆