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

时间:2015-10-30 12:56:40

标签: ios fbsdk fbsdkloginkit

我想获取用户名和个人资料图片usig 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");
             }
         }];
    }

3 个答案:

答案 0 :(得分: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];
        }
    }];
}

答案 1 :(得分: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"];

}

 }]; 

答案 2 :(得分: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);
             }
         }];

    }

}