没有使用Facebook 4.4.0 SDK获取电子邮件和公开个人资料

时间:2015-07-09 08:58:13

标签: ios facebook facebook-graph-api

我目前正在开发Facebook SDK 4.4.0。在4.4.0版本之前,在4.3.0中我们收到了带有以下代码的电子邮件public_profile

NSArray *arrFBPermission = [[NSArray alloc]initWithObjects:@"email",@"public_profile", nil];
    [loginUsingFB logInWithReadPermissions:arrFBPermission handler:^(FBSDKLoginManagerLoginResult *result, NSError *error){ 
[[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:nil] startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error){
                      if (!error){
                          NSString *fnm = [result valueForKey:@"first_name"];
                          NSString *lnm = [result valueForKey:@"last_name"];
                      }
else{
     Nslog("%@", error localizedDescription);
}
                  }];
}

现在在新的SDK(即4.4.0)中,我没有得到这些值。为什么?我想要来自Facebook的电子邮件,first_name,last_name,id。

1 个答案:

答案 0 :(得分:19)

使用Facebook 4.4 SDK会有轻微的变化。 您必须使用您想要的Facebook帐户FBSDKGraphRequest请求参数。

在您的代码中,参数中有nil

使用如下参数更新代码:

[[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:@{@"fields": @"id, name, link, first_name, last_name, picture.type(large), email, birthday, bio ,location , friends ,hometown , friendlists"}]

如果您想使用自定义按钮登录Facebook,则可以使用以下完整代码:

- (IBAction)btnFacebookPressed:(id)sender {
    FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
    login.loginBehavior = FBSDKLoginBehaviorBrowser;
    [login logInWithReadPermissions:@[@"email"] handler:^(FBSDKLoginManagerLoginResult *result, NSError *error)
     {
         if (error)
         {
             // Process error
         }
         else if (result.isCancelled)
         {
             // Handle cancellations
         }
         else
         {
             if ([result.grantedPermissions containsObject:@"email"])
             {
                 NSLog(@"result is:%@",result);
                 [self fetchUserInfo];
                 [login logOut]; // Only If you don't want to save the session for current app
             }
         }
     }];
}
-(void)fetchUserInfo
{
    if ([FBSDKAccessToken currentAccessToken])
    {
        NSLog(@"Token is available : %@",[[FBSDKAccessToken currentAccessToken]tokenString]);

        [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:@{@"fields": @"id, name, link, first_name, last_name, picture.type(large), email"}]
         startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
             if (!error)
             {
                 NSLog(@"resultis:%@",result);

             }
             else
             {
                 NSLog(@"Error %@",error);
             }
         }];
    }
}

我希望它对你有用。