-1

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?

Lucas Huang
  • 3,998
  • 3
  • 20
  • 29
  • One suggestion: Parse provides great guideline to show you how to do stuff. It's better for you to look it up yourself before asking question. https://parse.com/docs/ios/guide – Lucas Huang Jul 26 '15 at 18:10
  • true but they are kinda cryptic if you're not sure the proper terminology to use in your question. they are just a website not human beings – Samuel Hoffmann Jul 26 '15 at 18:11

1 Answers1

0

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
}
Manav Gabhawala
  • 997
  • 9
  • 10
  • and i always need the if statement? – Samuel Hoffmann Jul 26 '15 at 17:20
  • The function returns a `PFUser?` which means it is optional. If there is no logged in user or signed in user, then it will return nil. If you are 100% sure, and I mean a 100% (cause otherwise the app will crash) that the user can't get to the code without signing in then you can skip the if and just add an ! so like this: `PFUser.currentUser()!`. If you like this answer please mark it as accepted. – Manav Gabhawala Jul 26 '15 at 17:24
  • yeah i will dude, i have to wait 4 min from asking the question or somthing – Samuel Hoffmann Jul 26 '15 at 17:27
  • dude I'm having a problem with your response – Samuel Hoffmann Jul 26 '15 at 17:34
  • i made a new row in parse and Xcode is saying it doesn't exist but it does. i made a array, and it gives me errors when i say if let user = PFUser.currentUser() { // There is a logged in user. user.sentItems = sentitemsArr } – Samuel Hoffmann Jul 26 '15 at 17:34
  • Are you sure there is a logged in user. You should have logged in the user with `PFUser.logInWithUsernameInBackground:password:` or signed them up using the sign up method. Read the docs here: https://www.parse.com/docs/ios/api/Classes/PFUser.html#//api/name/logInWithUsernameInBackground:password: – Manav Gabhawala Jul 26 '15 at 17:45
  • i made a new row which is an array and i want to access it – Samuel Hoffmann Jul 26 '15 at 17:48
  • If you have a row in Parse that you want to access for *testing purposes only* you can use PFQuery.getUserObjectWithId: and pass in the string with the object id. This is hardcoded and should not be inside your actual app. Read more here: https://www.parse.com/docs/ios/api/Classes/PFQuery.html#//api/name/getUserObjectWithId: – Manav Gabhawala Jul 26 '15 at 17:52
  • dude, i make a parse app, i click on data in the core tab, i add a class i make it a default user, then i click add row once selected the user, i make it an array. i want to access that array – Samuel Hoffmann Jul 26 '15 at 17:56
  • I don't know what you mean by you made a user an array? I don't think you fully understand how PFUser works. Do not setup the user in parse directly, instead add a login/sign up screen in your app and call the login/signup functions from the app this will create the row in Parse directly and cache the user to the disk. Then all you have to do is call `PFUser.currentUser()` – Manav Gabhawala Jul 26 '15 at 18:00
  • http://imgur.com/mlMVill here, look i made sentItems and receivedItems they are arrays i want to access them. – Samuel Hoffmann Jul 26 '15 at 18:06
  • Oh I understand now. For that you have to access it like you would for regular `PFObject`'s using the subscript syntax. So it would be something like this: `if let array = PFUser.currentUser()?["sentItems"] as? [String] { // Now array has an array of strings with the items inside the array from parse. } – Manav Gabhawala Jul 27 '15 at 05:02
  • bro. you're the best. may god bless you – Samuel Hoffmann Jul 27 '15 at 21:59