从Facebook获取用户的电子邮件

时间:2014-05-26 17:05:32

标签: facebook facebook-graph-api ios7 parse-platform facebook-login

我刚刚创建了一个新的应用程序,我已经设置了Facebook登录,我已经获得了电子邮件许可,但我似乎不会收到用户的电子邮件,我想我可能会遗漏一些东西在我的代码中,这就是我所拥有的:

// Set permissions required from the facebook user account
    NSArray *permissionsArray = @[ @"user_about_me", @"user_relationships", @"user_likes", @"user_location", @"email", @"basic_info", @"user_activities", @"user_interests", @"user_birthday"];

// Login PFUser using facebook
[PFFacebookUtils logInWithPermissions:permissionsArray block:^(PFUser *user, NSError *error) {


    if (!user) {
        if (!error) {
            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 {
            NSLog(@"Uh oh. An error occurred: %@", error);
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Log In Error" message:[error description] delegate:nil cancelButtonTitle:nil otherButtonTitles:@"Dismiss", nil];
            [alert show];
        }
    } else if (user.isNew) {
        user[@"school"] = @"";
        NSLog(@"User with facebook signed up and logged in!");

    }
    else  {

           NSLog(@"User with facebook logged in!");
        } 

    }
}];

如果你能告诉我我缺少什么/为什么我没有看到我的用户发送电子邮件。我真的很感激。顺便说一下,我使用Parse.com作为后端。

1 个答案:

答案 0 :(得分:0)

logInWithPermissions:方法在创建新用户时默认不保存电子邮件属性。如果要保存Facebook用户的电子邮件,在验证用户已经过身份验证后,您需要调用FBRequestConnection类的startForMeWithCompletionHandler:方法。您的代码在最后一部分应该如下所示:

NSLog(@"User with facebook logged in!");
[FBRequestConnection startForMeWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
        if (!error) {
            [[PFUser currentUser] setObject:[result objectForKey:@"email"]
                                     forKey:@"email"];
            [[PFUser currentUser] saveInBackground];
        }
    }];