2016-09-16 135 views
2

我不熟悉新的Firebase。我如何创建新用户?下面我注册的代码和auth新用户。如果我需要在Firebase Database的“客户”下创建这个新用户,我需要添加哪些代码?谢谢!Firebase如何创建用户?

FIRAuth.auth()?.createUserWithEmail(email, password: password, completion: { (user, err) in 

    if err != nil { 

     self.showAlert("Can't Register", msg: "Please enter email and password") 

     } else { 

      NSUserDefaults.standardUserDefaults().setValue(user?.uid, forKey: "uid") 

      FIRAuth.auth()?.signInWithEmail(email, password: password, completion: { (user, error) in 

     }) 

     self.performSegueWithIdentifier("toSecondVC", sender: self) 
    } 

    }) 

回答

3
FIRAuth.auth()?.createUserWithEmail(email, password: password, completion: { (user, err) in 

if err != nil { 

    self.showAlert("Can't Register", msg: "Please enter email and password") 

    } else { 

     NSUserDefaults.standardUserDefaults().setValue(user?.uid, forKey: "uid") 

    FIRDatabase.database().reference().child("Customers").setValue([user!.uid : "true"]) 

    //Or if you want to save the users Details too:- 
    /* 

     FIRDatabase.database().reference().child("Customers").setValue([user!.uid : ["email" : email,"password" : password]]) 

    */ 
    self.performSegueWithIdentifier("toSecondVC", sender: self) 
    } 
}) 

而且可能我建议阅读本:以上Firebase iOS - Save Data

+0

嗨达罗毗荼人,谢谢!效果很好!星期五快乐! – developermike

0

的答案是正确的,但我建议做这种方式:

var values = [Any : Any] // This is a dictionary where you can store user details 
values["userUID"] = // user's uid 
values["usersName"] = // user's name 
// etc. 

let customerRef = databaseReference.child("Customers") // You could also add a child with the user's UID in order to identify each user 

customerRef.updateChildValues(values, withCompletionBlock: { (error, ref) in 

    if error != nil { 

     // display error.code and error.localizedDescription to user if needed 

    } else if ref != [] { 

     // Success! 
     // If needed, save the user to NSUserDefaults and perfrom a segue to another ViewController 

    } else { 

     // There was an error, but not sure what happened. Let the user know how they can fix the problem (maybe add the details to the database later) 

    } 
}) 
相关问题