Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/96.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在Swift中使用firebase创建新用户_Ios_Swift_Firebase - Fatal编程技术网

Ios 在Swift中使用firebase创建新用户

Ios 在Swift中使用firebase创建新用户,ios,swift,firebase,Ios,Swift,Firebase,我是Firebase的新手,一直在尝试为我的应用程序实现“注册”页面。目前,我只使用电子邮件功能,只是为了测试一下。我可以创建新用户并将其存储在仪表板的“登录和验证”选项卡中,但无论如何我都无法检索UID。唯一的方法是,如果我关闭我的应用程序并重新加载它,那么它就可以访问UID。有什么建议吗?如何创建用户的示例: Auth.auth().createUser(withEmail: email, password: password, completion: { (result, erro

我是Firebase的新手,一直在尝试为我的应用程序实现“注册”页面。目前,我只使用电子邮件功能,只是为了测试一下。我可以创建新用户并将其存储在仪表板的“登录和验证”选项卡中,但无论如何我都无法检索UID。唯一的方法是,如果我关闭我的应用程序并重新加载它,那么它就可以访问UID。有什么建议吗?

如何创建用户的示例:

    Auth.auth().createUser(withEmail: email, password: password, completion: { (result, error) -> Void in
        if (error == nil) {
                UserDefaults.standardUserDefaults().setValue(result.uid, forKey: "uid")

                print("Account created :)")

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

                let uid = result!.uid

                self.dismissViewControllerAnimated(true, completion: nil)
        }

        else{
                print(error)
        }
    })

希望这有帮助。

在Firebase中创建用户非常简单,只需使用cocoa pods将Firebase添加到您的项目中,然后创建注册屏幕。你需要电子邮件和密码。现在首先导入Firebase

import Firebase
然后在viewDidLoad()方法中配置firebase

FIRApp.configure()
现在从textfields获取电子邮件和密码,并使用以下代码进行注册

FIRAuth.auth()?.createUserWithEmail(email!, password: password!, completion: { (user: FIRUser?, error) in
   if error == nil {
       //registration successful
   }else{
       //registration failure
   }
})
就这样


来源:Swift 4.1 FireBase用户创建/登录步骤:-

步骤1:-将新的firebase身份验证吊舱注册到您的吊舱文件:

pod 'Firebase/Auth'
pod install
import FirebaseAuth
 Auth.auth().createUser(withEmail: (txtEmail.text ?? ""), password: (txtPass.text ?? "")) { (result, error) in
            if let _eror = error {
                //something bad happning
                print(_eror.localizedDescription )
            }else{
                //user registered successfully
                print(result)
            }
         }
  Auth.auth().signIn(withEmail: (txtEmail.text ?? ""), password: (txtPass.text ?? "")) { (result, error) in
            if let _eror = error{
                print(_eror.localizedDescription)
            }else{
                if let _res = result{
                    print(_res)
                }
            }
        }
第2步:-打开终端并使用相关项目目录路径安装pod:

pod 'Firebase/Auth'
pod install
import FirebaseAuth
 Auth.auth().createUser(withEmail: (txtEmail.text ?? ""), password: (txtPass.text ?? "")) { (result, error) in
            if let _eror = error {
                //something bad happning
                print(_eror.localizedDescription )
            }else{
                //user registered successfully
                print(result)
            }
         }
  Auth.auth().signIn(withEmail: (txtEmail.text ?? ""), password: (txtPass.text ?? "")) { (result, error) in
            if let _eror = error{
                print(_eror.localizedDescription)
            }else{
                if let _res = result{
                    print(_res)
                }
            }
        }
步骤3:-在UIViewController中导入firebase身份验证库,您可以在任何需要的地方:

pod 'Firebase/Auth'
pod install
import FirebaseAuth
 Auth.auth().createUser(withEmail: (txtEmail.text ?? ""), password: (txtPass.text ?? "")) { (result, error) in
            if let _eror = error {
                //something bad happning
                print(_eror.localizedDescription )
            }else{
                //user registered successfully
                print(result)
            }
         }
  Auth.auth().signIn(withEmail: (txtEmail.text ?? ""), password: (txtPass.text ?? "")) { (result, error) in
            if let _eror = error{
                print(_eror.localizedDescription)
            }else{
                if let _res = result{
                    print(_res)
                }
            }
        }
第4步:-在您的注册UIButton操作中编写以下代码片段:

pod 'Firebase/Auth'
pod install
import FirebaseAuth
 Auth.auth().createUser(withEmail: (txtEmail.text ?? ""), password: (txtPass.text ?? "")) { (result, error) in
            if let _eror = error {
                //something bad happning
                print(_eror.localizedDescription )
            }else{
                //user registered successfully
                print(result)
            }
         }
  Auth.auth().signIn(withEmail: (txtEmail.text ?? ""), password: (txtPass.text ?? "")) { (result, error) in
            if let _eror = error{
                print(_eror.localizedDescription)
            }else{
                if let _res = result{
                    print(_res)
                }
            }
        }
步骤5:-如果您想在注册后登录,请在登录按钮上使用此代码片段:

pod 'Firebase/Auth'
pod install
import FirebaseAuth
 Auth.auth().createUser(withEmail: (txtEmail.text ?? ""), password: (txtPass.text ?? "")) { (result, error) in
            if let _eror = error {
                //something bad happning
                print(_eror.localizedDescription )
            }else{
                //user registered successfully
                print(result)
            }
         }
  Auth.auth().signIn(withEmail: (txtEmail.text ?? ""), password: (txtPass.text ?? "")) { (result, error) in
            if let _eror = error{
                print(_eror.localizedDescription)
            }else{
                if let _res = result{
                    print(_res)
                }
            }
        }

快乐编码;)

在尝试任何代码之前,请确保密码长度至少为6个字符。打印错误如果可能,我会在每个函数上打印错误。注册并登录只是为了跟踪您正在做的事情

@IBAction func signUp(_ sender: Any) {

Auth.auth().createUser(withEmail: self.usernameTextField.text!, password: self.passwordTextField.text!) {(user, error) in
    if user != nil {
        print("User Has SignUp")
    }
    if error != nil {
        print(":(",error)
    }
}
   Auth.auth().signIn(withEmail: self.usernameTextField.text!, password: self.passwordTextField.text!) {(user, error) in
        if user != nil {
            print("User Has Sign In")
        }
        if error != nil {
            print(":(",error)
        }
}

欢迎来到Stackoverflow。请发布代码片段和您当前拥有的任何Firebase数据结构。请只发文本,不发图片。然后完善你的问题,让我们知道你在问什么;你的代码有错误吗?结果如何?此外,您可能需要查看Firebase网站上的用户身份验证页面,它将引导您完成身份验证。我找到了一个解决方案。无论出于什么原因,我都无法从我的注册页面获取用户UID。我刚刚切换到另一个视图控制器,现在我可以获取UID。