Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

How can I save PFUser with other class name than "user"

Hi guys,

I want to save PFUser not as "user" in the backend, but as "leverancier". I know that you can save it as PFObject with your custom class name...but would that not cause problems in loging in?

else {
PFUser *newUserLeverancier = [PFUser user];
newUserLeverancier[@"naamLeverancier"] = naamLeverancier;
        newUserLeverancier[@"telefoonLeverancier"]= telefoonLeverancier;
        newUserLeverancier.username = bedrijfsnaamLeverancier;
        newUserLeverancier[@"adresLeverancier"] = adresLeverancier;
        newUserLeverancier[@"postcodeLeverancier"] = postcodeLeverancier;
        newUserLeverancier[@"stadLeverancier"] = stadLeverancier;
        newUserLeverancier.email = emailLeverancier;
        newUserLeverancier.password = wachtwoordLeverancier;

       [newUserLeverancier signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
            if (error) {
               UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Sorry!" message:[error.userInfo objectForKey:@"error"] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
                [alertView show];
            }
           else {
                [self.navigationController popToRootViewControllerAnimated:YES];
            }
        }];
 }

8 Answers

I think (not 100% sure) the only way to do that would be to create a PFObject with that class name. And by doing that you lose all the functionality of the PFUser class which you dont want to do.

Hmm...what do you recommend me than? I actually want to create two types of users , and give them separate functionalities/view controllers inside the app, after logging in from one same loginviewcontroller...how should i tackle this? I cannot wait you guys make a more complicated multi user app with geolocation, messaging, phone calling etc etc :))

I would add a field to your user for whatever type you want them to be. For example if you had admin users and regular users you could have an admin field and set it to true for admin users and false for regular users

//this user needs to be an admin
user[@"admin"] = TRUE;

then when you wanted to conditionally present content based on what type of user the current user was you could do something like

if (self.currentUser[@"admin"]) {

//set up view for admin users

} else {

//set up view for regular users

}

Okay thanks, I think I can proceed with that !

I am kind of stuck here at the loginviewcontroller;

- (void)viewDidLoad
{
    NSString *username = [self.loginField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    NSString *password = [self.passwordField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

    if  ([username length] == 0 ||
         [password length] == 0)
    {

        UIAlertView *alertView = [[ UIAlertView alloc] initWithTitle:@"Oops!" message:@"Voer uw bedrijfsnaam en wachtwoord in" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];

        [alertView show];
    }
    else {
        [PFUser logInWithUsernameInBackground:username password:password block:^(PFUser *user, NSError *error) {

         if (error) {
             UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Sorry!" message:[error.userInfo objectForKey:@"error"] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
             [alertView show];
                    }

         else {

             *I want here that*  newUserGarage[@"type"] = @"garage"  *goes to* inboxgarageviewcontroller> *and the others (* newUserLeverancier[@"type"] = @"leverancier"; *) goes to* 

self.navigationController popToRootViewControllerAnimated:YES]
         }

        }] ;
    }


}

what seems to be the problem?

see above Stone, ive created 2 different signupviewers for leverancier (user1) and garage (user 2).

i succeed to save them on parse.

now at the login, i want that the leverancier goes to his inboxleverancierviewcontroller and that garage goes to his inboxgarageviewcontroller. the segue's of them both are connected with login...i dont know how to code programmatically that with if and again if and again...i went some crazy..:(

ok. I can see where your issue is going to be. If you have this app set up just as the ribbit app is, then what happens is the root view controller (the inbox view controller) first checks to see if the user is logged in, if he is, the inbox view is displayed, if hes not logged in, then the login view is displayed. So the code for presenting the two inboxes doesnt belong in the login view, it belongs in whatever your root view controller is. So leave the following like this in the login view controller

[PFUser logInWithUsernameInBackground:username password:password block:^(PFUser *user, NSError *error) {

         if (error) {
             UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Sorry!" message:[error.userInfo objectForKey:@"error"] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
             [alertView show];
                    }

         else {

//go to the root view controller (the inbox)
self.navigationController popToRootViewControllerAnimated:YES]
         }

        }] ;

and then in your inbox view controller you can do the if else check for the user type after you check if they are logged in

- (void)viewDidLoad
{
    [super viewDidLoad];

    PFUser *currentUser = [PFUser currentUser];

    if (currentUser){

        //the user is logged in. now you need to check the type of the user to determine if you need to segue or not
        if ([currentUser[@"type"] isEqualToString:@"garage"]){

         [self performSegueWithIdentifier:@"showGarageInbox" sender:self];


         } else {

              //user is of the other type.
         }


    } else {

          [self performSegueWithIdentifier:@"showLogin" sender:self];

    }

}

so basically since the inbox view controller is your root view controller, you need to have a segue from that view controller to the othe inbox view controller and perform that in view did load like I did above

stone, I have exactly done like you said.

when I login as type "garage" it goes to inboxleverancier instead of inboxgarage, while the segue is right. It don't check rightly this I think:

([currentUser[@"type"] isEqualToString:@"garage"])

Is this because a wrong in the mainstoryboard connections? Or a typo which I dont see yet that blocks to determine the type...

alt text (picture of the mainstoryboard dont appear here, a bug?)

picture mainstoryboard: https://docs.google.com/document/d/1XOL2IxFZMoYuvQQii2MLwmy6VtkcBHOHrNOWWoeEKBQ/edit?usp=sharing