2016-09-08 1 views
1

Я пытаюсь обновить электронную почту и полное имя пользователя. Это мой код:Как вы обновляете настройки профиля пользователя с помощью firebase и swift?

функ saveTapped() {

var performSegue = false 

    if updateEmail.text == "" && updateFullName.text == "" { 
     self.cleanUrCodeRohan("Please fill in one or more of the missing text fields that you would like to update.") 
    } 

    if updateEmail.text != "" { 

     let user = FIRAuth.auth()?.currentUser 

     user?.updateEmail(updateEmail.text!) { error in 
      self.ref.child("users").child(self.currentUser).child("email").setValue(self.updateEmail.text!) 

     } 

     let emailUpdateRef = FIRDatabase.database().reference().child(currentUser).child("email") 
     print(emailUpdateRef) 
     emailUpdateRef.setValue(self.updateEmail.text) 

     performSegue = true 
    } 

    if updateFullName.text != "" { 
     let user = FIRAuth.auth()?.currentUser 
     if let user = user { 
      let changeRequest = user.profileChangeRequest() 
      changeRequest.displayName = self.updateFullName.text! 
     } 
     performSegue = true 
    } 

    if performSegue == true { 
     self.navigationController!.popViewControllerAnimated(true) 
    } 
} 

Я могу обновлять электронную почту в соответствии с полномочиями, но не под базой данных. Любая помощь будет оценена по достоинству.

+0

Что вы имеете в виду «в соответствии с полномочиями, но не под базой данных «? У вас есть правила базы данных, в которых у вас есть пользователи: {user: {email: "[email protected]"}}? – user2884707bond

+0

Внутренний пользователь? .updateEmail (updateEmail.text!) {Ошибка в self.ref.child («users»). Child (self.currentUser) .child («email»). SetValue (self.updateEmail.text!) } вы можете проверить, есть ли (ошибка! = Nil) {print («что-то случилось!»} – user2884707bond

+0

нет я имею в виду, что в базе данных не разрешено –

ответ

0

Если JSON дерево что-то вроде этого: -

appName{ 
    users :{ 
     userID :{ 
      email : "..", 
      username : ".." 
     } 
    } 
} 

Используйте этот код для обновления ребенка значение вашего узла-х: -

func saveTapped(){ 

if ((updateEmail.text != "" || updateFullName.text != "") && (updateEmail.text != nil || updateFullName.text != nil)){ 

    let userRef = FIRDatabase.database().reference().child("users").child(FIRAuth.auth()!.currentUser!.uid) 
    if let new_Email = updateEmail.text as? String{ 

     FIRAuth.auth()!.currentUser!.updateEmail(updateEmail.text!) { error in 

      if error == nil{ 
       userRef.updateChildValues(["email" : new_Email ], withCompletionBlock: {(errEM, referenceEM) in 

        if errEM == nil{ 
         print(referenceEM) 
        }else{ 
         print(errEM?.localizedDescription) 
        } 
       }) 
      } 
     }else{ 
      self.cleanUrCodeRohan("Email couldn't be updated in auth") 
     } 
    } 

    if let new_Name = updateFullName.text as? String{ 

     userRef.updateChildValues(["username" : new_Name ], withCompletionBlock: {(errNM, referenceNM) in 

      if errNM == nil{ 
       print(referenceNM) 
      }else{ 
       print(errNM?.localizedDescription) 
      } 
     }) 
    } 

}else{ 

    self.cleanUrCodeRohan("Please fill in one or more of the missing text fields that you would like to update.") 

    } 
} 
+0

спасибо, что сработало –

+0

У меня есть еще один вопрос, как бы обновить url, сохраненный в моей базе данных. Это одно и то же дерево сыновей, которое вы показали над ним, просто имеет другое название, называемое фото-url: «URL PHOTOS». Любая помощь будет оценена по достоинству. Благодаря! –

+0

как я принимаю и +1 этот ответ –