使用FBSDKLoginkit获取用户姓名和个人资料照片

3
我想使用FBSDKLoginKit获取用户名和个人资料图片。如何才能实现呢?以下是我的代码。

pod 'FBSDKLoginKit', '~> 4.7'

   - (IBAction)fbLoginActionClicked:(id)sender
    {
        FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];

        [login
         logInWithReadPermissions: @[@"public_profile"]
         fromViewController:self
         handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
             if (error) {
                 NSLog(@"Process error");
             }
             else if (result.isCancelled)
             {
                 NSLog(@"Cancelled");
             }
             else
             {
                 NSLog(@"Logged in");
             }
         }];
    }

1
请参考此链接https://dev59.com/k10a5IYBdhLWcg3wfIp-#30275960,可能对您有所帮助。 - Dharmesh Dhorajiya
3个回答

2
    if ([FBSDKAccessToken currentAccessToken]) {
    [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:@{ @"fields" : @"id,name,picture.width(100).height(100)"}]startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
        if (!error) {
            NSString *nameOfLoginUser = [result valueForKey:@"name"];
            NSString *imageStringOfLoginUser = [[[result valueForKey:@"picture"] valueForKey:@"data"] valueForKey:@"url"];
            NSURL *url = [[NSURL alloc] initWithURL: imageStringOfLoginUser];
            [self.imageView setImageWithURL:url placeholderImage: nil];
        }
    }];
}

你好,你能回答这个问题吗?https://dev59.com/hpHea4cB1Zd3GeqPmj9u。如果你能帮我就太感谢了。 - Aarti Oza

1
使用以下代码类似于 FBSDKGraphRequest 来获取 Facebook 用户的用户名和个人资料图片。
一旦您已登录,请添加以下代码:
NSLog(@"Logged in");
             if ([result.grantedPermissions containsObject:@"public_profile"]) {
                 if ([FBSDKAccessToken currentAccessToken]) {
                     NSMutableDictionary* parameters = [NSMutableDictionary dictionary];
                     [parameters setValue:@"name,picture.type(large)" forKey:@"fields"];

                     [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:parameters]
                      startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
                          if (!error)
                          {
                              NSLog(@"fetched user:%@", result);


                              NSString *name = [[result valueForKey:@"name"] lowercaseString];
                              NSString *username = [name stringByReplacingOccurrencesOfString:@" " withString:@""];
                              NSArray *picture_arr = [result objectForKey:@"picture"];
                              NSArray *data_arr = [picture_arr valueForKey:@"data"];
                              NSString *profile_pic = [data_arr valueForKey:@"url"];

}

 }]; 

你好,你能回答这个问题吗?https://dev59.com/hpHea4cB1Zd3GeqPmj9u。如果你能帮我就太感谢了。 - Aarti Oza

1
- (IBAction)fbLoginActionClicked:(id)sender
{
    FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];

    [login
     logInWithReadPermissions: @[@"public_profile"]
     fromViewController:self
     handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
         if (error) {
             NSLog(@"Process error");
         }
         else if (result.isCancelled)
         {
             NSLog(@"Cancelled");
         }
         else
         {

             NSLog(@"Logged in %@ %@", result.token.userID, result.token.tokenString);
             [self fetchUserInfo];
         }
     }];
}

-(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, birthday, bio ,location ,friends ,hometown , friendlists"}]
         startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
             if (!error)
             {
                 NSLog(@"resultis:%@",result);
             }
             else
             {
                 NSLog(@"Error %@",error);
             }
         }];

    }

}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接