TheM00s3
TheM00s3

Reputation: 3711

Setting the username to Facebook name with Parse API.

Currently here is my code in my LogInViewController that contains the handler for Facebook signup process and code that I was hoping would setup the username and email address to their Facebook Name and Email address.

- (IBAction)fbButtonPressed:(id)sender
{
    NSArray *permissions =@[@"public_profile", @"email", @"user_friends"];

    [PFFacebookUtils logInWithPermissions:permissions block:^(PFUser *user, NSError *error) {
        if (!user) {
            NSLog(@"Uh oh. The user cancelled the Facebook login.");
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Log In Error" message:@"Uh oh. The user cancelled the Facebook login." delegate:nil cancelButtonTitle:nil otherButtonTitles:@"Dismiss", nil];
            [alert show];
        } else if (user.isNew) {
            NSLog(@"User signed up and logged in through Facebook!");
            FBRequest *request = [FBRequest requestForMe];
            [request startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
                if (!error){
                    NSDictionary *userData = (NSDictionary *)result;

                    NSString *facebookID= userData[@"ID"];

                    NSURL *pictureURL = [NSURL URLWithString:[NSString stringWithFormat:@"https://graph.facebook.com/%@/picture?type=large&return_ssl_resources=1", facebookID]];


                    NSMutableDictionary *userProfile = [NSMutableDictionary dictionaryWithCapacity:7];

                    if (facebookID){
                        userProfile[@"facebookID"] = facebookID;
                    }

                    if (userData[@"name"]) {

                        user.username = userProfile[@"name"];
                    }

                    if (userData[@"email"]){
                        user.email = userData[@"emai"];
                    }


                    [user saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
                        if (error){
                            NSLog(@"ERROR: %@ %@", error, [error userInfo]);

                        }else{
                            [self.navigationController popToRootViewControllerAnimated:YES];
                        }
                    }];



                }
            }];


        } else {
            NSLog(@"User logged in through Facebook!");
            [self.navigationController popToRootViewControllerAnimated:YES];
        }
    }];
}

Upvotes: 1

Views: 1156

Answers (2)

wataru
wataru

Reputation: 1080

Try this.

PFUser *user = [PFUser currentUser];
FBRequest *request = [FBRequest requestForMe];
[request startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
    NSLog(@"test");
    if(!error) {
        NSDictionary *userData = (NSDictionary *)result;
        NSString *name = userData[@"name"];
        user.username = name;
        NSLog(@"user:%@", name);
        [user saveEventually];
    } else {
        NSLog(@"An error occurred: %@", error.localizedDescription);
    }
}];

Upvotes: 0

Marius Waldal
Marius Waldal

Reputation: 9942

You have a typo in

user.email = userData[@"emai"];

Also, for the name, you try getting the name from userProfile:

if (userData[@"name"]) {
  user.username = userProfile[@"name"];
}

which should be

if (userData[@"name"]) {
  user.username = userData[@"name"];
}

Upvotes: 1

Related Questions