maxisme
maxisme

Reputation: 4245

updating user profile on parse with ios?

I have multiple view controllers.

The first is the sign in page with a username and password text field. Where I sign the user up using Parse like this:

- (IBAction)signup:(id)sender{
    PFUser *user = [PFUser user];
    user.username = _username.text;
    user.password = _password.text;
    [user signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (!error) {

        } else {
            [self dismissModalViewControllerAnimated:YES];
            NSLog(@"already username");
            [self.navigationController popToRootViewControllerAnimated:YES];
            _username.textColor = [UIColor redColor];
            _inuse.text = @"Username is already taken!";
            // Show the errorString somewhere and let the user try again.

        }
    }];
}

Then on the next view controllers I want to add information to the user.

So I tried on the next view controller to do this. Where I have two text fields firstname and lastname:

- (IBAction)signup_name:(id)sender{
    PFUser *user = [PFUser user];
    user[@"firstname"] = _firstname.text;
    user[@"lastname"] = _lastname.text;
    [user saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (!error) {

        } else {
            [self dismissModalViewControllerAnimated:YES];
            NSLog(@"already username");
            [self.navigationController popToRootViewControllerAnimated:YES];
            _username.textColor = [UIColor redColor];
            _inuse.text = @"Username is already taken!";
            // Show the errorString somewhere and let the user try again.

        }
    }];
}

But I am getting the error: 'User cannot be saved unless they are already signed up. Call signUp first.'

I tried adding this to - (IBAction)signup:(id)sender When there isn't an error:

[PFUser logInWithUsernameInBackground:_username.text password:_password.text
                                            block:^(PFUser *user, NSError *error) {
                                                if (user) {
                                                    // Do stuff after successful login.
                                                } else {
                                                    // The login failed. Check error to see why.
                                                }
                                            }];

Upvotes: 0

Views: 1822

Answers (1)

danh
danh

Reputation: 62676

After the signup succeeds, the current user is accessible by [PFUser currentUser], so...

- (IBAction)signup_name:(id)sender{
    PFUser *user = [PFUser currentUser];

Should work.

Upvotes: 1

Related Questions