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

Pre*_*ani 3 facebook facebook-graph-api ios

我目前正在开发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);
}
                  }];
}
Run Code Online (Sandbox Code Playgroud)

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

Ash*_*kad 19

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

在您的代码中有nil参数:

使用如下参数更新代码:

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

如果您想使用自定义按钮登录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);
             }
         }];
    }
}
Run Code Online (Sandbox Code Playgroud)

我希望它对你有用.

  • 只是完美的伟大...✌ (2认同)