Search code examples
swiftparse-platformpfuser

how to send a pfUser information to the next view controller


I got login and sign up with Parse working but then I call a segue and go to the next view controller and I don't know how to access the information of that PFUser.

Simply if I want another screen to say "welcome"+the username but I don't know how to access that string. How do I get the information of the PFUser I just logged in, in the new view controller? Do I have to add some code with a prepare for segue method?


Solution

  • No you don't have to send any data to the new view controller. Parse allows you to access the logged in/newly signed up user using:

    [PFUser currentUser]; // Objc
    PFUser.currentUser(); // Swift
    

    These return a nullable PFUser object which is nil if there is no logged in/signed up user.

    Here's how you can access the firstName on the PFUser assuming that you have a key of firstName as a string in the _User table in Parse.

    if let user = PFUser.currentUser() {
         // There is a logged in user.
         let userName = user.username
         let name = user["firstName"] as? String
    }