2017-04-01 176 views
-3

離開Parse並轉移到Firebase後,我面臨的一個問題是隻能通過電子郵件和密碼進行註冊,我怎樣才能讓Firebase存儲用戶名呢?使用用戶,電子郵件和密碼進行Firebase註冊

+0

見http://stackoverflow.com/q/32151178 –

+0

我認爲關於Android鏈接的問題舉行會談,這是同樣的事情呢? @FrankvanPuffelen – Abdullah

+0

Firebase SDK在不同平臺上的工作原理相同。鏈接答案中的方法也適用於iOS。如果您在解決問題時遇到問題,請分享[重現您卡住的最小完整代碼](http://stackoverflow.com/help/mcve)。 –

回答

2

嘗試......

FIRAuth.auth()?.createUser(withEmail: emailField.text!, password: passwordField.text!, completion: { (user: FIRUser?, error) in 
      if error != nil { 
       print(error) 
       return 

      } else { 
      print("User Created...") } 

      guard let uid = user?.uid else { return } 

      // You can set these values to whatever you want, plus add more! 
      let values = ["email": emailField.text!, "username": usernameField.text!] as [String : Any] 

     // Then pass your values into another function called 'RegisterUserIntoDatabase' 
     // which creates the user in your Firebase Database... 

      self.registerUserIntoDatabase(uid, values: values as [String : AnyObject]) 
     }) 


private func registerUserIntoDatabase(_ uid: String, values: [String: AnyObject]) { 
    // Adding User Info 
    let ref = FIRDatabase.database().reference() 
    let usersReference = ref.child("users").child(uid) 

    usersReference.updateChildValues(values, withCompletionBlock: { (err, ref) in 
     if err != nil { 
      print(err) 
      return 
     } 
     print("Successfully Added a New User to the Database") 
    }) 
} 
+0

fileprivate func?你的意思是私人func?因爲我使用fileprivate得到了一個錯誤。謝謝你幫助爵士。 – Abdullah

+0

沒問題!如果這有助於你,選擇這個答案將不勝感激... –

+0

我只是做了。你應得的!非常感謝! – Abdullah

0
func handleRegister(email:String, password:String, username:String) { 

    //Create User 
    FIRAuth.auth()?.createUser(withEmail: email, password: password, completion: { (user, err) in 

     if err != nil { 
      print(err!.localizedDescription) 
      return 
     } 

     //Success! User Created! 

     if let userID = user?.uid { 

      //Create User Profile 
      let databaseRef = FIRDatabase.database().reference() 
      let usersRef = databaseRef.child("Users").child(userID) 

      let usernameValue = ["username":username] 

      usersRef.updateChildValues(usernameValue, withCompletionBlock: { (err, ref) in 

       if err != nil { 
        print(err!.localizedDescription) 
        return 
       } 

       //Profile created and updated! 
       //You can use perform segue or present controller to go to another controller... 
      }) 

     } 

    }) 

} 
+0

我應該如何調用該函數?謝謝你試圖幫助爵士。 – Abdullah

+0

在註冊/註冊按鈕動作中調用此函數。 –

相關問題