在iOS中从FacebookSDK获取用户的个人信息

时间:2015-09-20 10:52:42

标签: ios objective-c facebook-sdk-4.x

我正在尝试从Facebook API获取信息。参数是几个目录中的组。要从Facebook SDK使用以下代码获取数据:

FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]
                           initWithGraphPath:@"me"
                                  parameters:@{ @"fields" : @"name, birthday"}
                                  HTTPMethod:@"GET"];[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection,
                                  id result,
                                  NSError *error) {

];

目录名称数据的名称'名称'和'生日'是"字段"。但我希望从其他目录(边缘,参数)中获取更多数据,例如名字,姓氏,电子邮件,身份证,关于等等。我如何编写代码来实现这一目标?

2 个答案:

答案 0 :(得分:1)

把这段代码

FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
[login logInWithReadPermissions:@[@"email"] handler:^(FBSDKLoginManagerLoginResult *result, NSError *error)
     {
         if (error)
         {
             // Process error
             NSLog(@"error is :%@",error);
         }
         else if (result.isCancelled)
         {
             // Handle cancellations
             NSLog(@"error is :%@",error);
         }
         else
         {
             if ([result.grantedPermissions containsObject:@"email"])
             {
                 [self fetchUserInfo];
             }
         }
    }];

您可以通过以下方式获取Facebook用户信息

-(void)fetchUserInfo
{
    if ([FBSDKAccessToken currentAccessToken])
    {
        [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:@{@"fields": @"id,name,link,first_name, last_name, picture.type(large), email, birthday, bio ,location ,friends ,hometown , friendlists"}]
         startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
             if (!error)
             {

                 NSString *photostring=[[[result valueForKey:@"picture"] objectForKey:@"data"] valueForKey:@"url"];
                 photostring = [photostring stringByReplacingOccurrencesOfString:@"&" withString:@"%26"];

                 NSLog(@"all data here is:%@",result);
                 NSLog(@"username is :%@",[result valueForKey:@"name"]);
                 NSLog(@"PhotoUrl is :%@",photostring);
                 NSLog(@"mail id is :%@",[result valueForKey:@"email"]);
             }
         }];
    }
}

祝你的项目好运

答案 1 :(得分:0)

首先,您必须将此代码放在登录过程开始的地方,如下所示

FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];

[login logInWithReadPermissions:@[@"public_profile", @"email"] handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {

    if (error)
    {

     // There is an error here.

    }
    else
    {
        if(result.token)   // This means if There is current access token.
        {    
            // Token created successfully and you are ready to get profile info
            [self getFacebookProfileInfo];
        }        
    }
}];

如果登录成功,请实施此方法以获取用户的公开个人资料

-(void)getFacebookProfileInfos { 

FBSDKGraphRequest *requestMe = [[FBSDKGraphRequest alloc]initWithGraphPath:@"me" parameters:nil];

FBSDKGraphRequestConnection *connection = [[FBSDKGraphRequestConnection alloc] init];

[connection addRequest:requestMe completionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {

      if(result)
      {            
        if ([result objectForKey:@"email"]) {

          NSLog(@"Email: %@",[result objectForKey:@"email"]);

        }
        if ([result objectForKey:@"first_name"]) {

          NSLog(@"First Name : %@",[result objectForKey:@"first_name"]);

        }
         if ([result objectForKey:@"dob"]) {

          NSLog(@"Date of birth : %@",[result objectForKey:@"dob"]);

        }
        if ([result objectForKey:@"id"]) {

          NSLog(@"User id : %@",[result objectForKey:@"id"]);

        }

      }

 }];

[connection start];
}

您也可以点击此链接get facbook user information

或者您可以获得Facebook用户信息

if (FBSession.activeSession.isOpen) {

    [[FBRequest requestForMe] startWithCompletionHandler:
     ^(FBRequestConnection *connection,
       NSDictionary<FBGraphUser> *user,
       NSError *error) {
         if (!error) {
             NSString *firstName = user.first_name;
             NSString *lastName = user.last_name;
              NSString *bateOfBirth = user.bate_Of_Birth;
             NSString *facebookId = user.id;
             NSString *email = [user objectForKey:@"email"];
             NSString *imageUrl = [[NSString alloc] initWithFormat: @"http://graph.facebook.com/%@/picture?type=large", facebookId];
         }
     }];
}