在iOS 5中获取访问令牌后获取Facebook用户配置文件数据

时间:2012-09-15 11:42:01

标签: iphone objective-c facebook facebook-graph-api ios5

我正在开发iOS 5应用程序,我需要在成功登录后获取用户个人资料数据。

现在,当用户成功登录时,我将获得一个访问令牌。

现在我正试图让Facebook使用该访问令牌登录用户个人资料数据。

我试图关注Facebook开发者网站上提供的示例应用。

我没有使用FBconnect类型的资源文件。我从示例应用程序添加“facebookSDK.framework”。

请帮帮我。

谢谢,

- (void)populateUserDetails {

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

// First request uploads the photo.
FBRequest *request1 = [FBRequest requestForMe];


if (FBSession.activeSession.isOpen) {

    [connection addRequest:request1
         completionHandler:
     ^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) {
         if (!error) {

             NSLog(@"fb user name = %@",user.name);
             NSLog(@"fb user name = %@",user.id);
         }

         NSLog(@"fb2 user name = %@",user.name);
         NSLog(@"fb2 user name = %@",user.id);
     }
            //batchEntryName:@"photopost"
     ];

    [connection start];
}

}

3 个答案:

答案 0 :(得分:12)

你的.h

中的

#import<FacebookSDK/FacebookSDK.h>

现在在你的.m文件中只需调用以下方法来获取用户数据

-(void)loginViewFetchedUserInfo:(FBLoginView *)loginView user:(id<FBGraphUser>)user{
    NSLog(@"usr_id::%@",user.id);
    NSLog(@"usr_first_name::%@",user.first_name);
    NSLog(@"usr_middle_name::%@",user.middle_name);
    NSLog(@"usr_last_nmae::%@",user.last_name);
    NSLog(@"usr_Username::%@",user.username);
    NSLog(@"usr_b_day::%@",user.birthday);

  }

上面的方法在FBLoginView.h中定义,你可以在那里看到。

我添加的这个方法根据最新的facebookSDK.framework.Hope它会帮助你。

答案 1 :(得分:4)

这是获取用户个人资料图片的方法:

//in your app delegate file configure "FBProfilePictureView" class
- (void)applicationDidFinishLaunching:(UIApplication *)application {    
    [FBProfilePictureView class];
}

//in your header file
IBOutlet FBProfilePictureView   *userPictureView;


@property (strong, nonatomic) FBProfilePictureView     *userPictureView;

//here "userPictureView" is uiview not uiimageview

//in your .m file 

-(void)setProfilePicture {
    [FBSession.activeSession closeAndClearTokenInformation];

    NSArray *permissions = [NSArray arrayWithObjects:@"email", nil];

    [FBSession openActiveSessionWithReadPermissions:permissions
                                       allowLoginUI:YES
                                  completionHandler:
     ^(FBSession *session,
       FBSessionState state, NSError *error) {
         NSLog(@"\nfb sdk error = %@", error);
         switch (state) {
             case FBSessionStateOpen:
                 [[FBRequest requestForMe] startWithCompletionHandler:
                  ^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) {
                      if (!error) {

                          self.userPictureView.profileID = [user objectForKey:@"id"];

                      } 
                  }];
                 break;
             case FBSessionStateClosed:
                 //need to handle
                 break;
             case FBSessionStateClosedLoginFailed:
                 //need to handle
                 break;
             default:
                 break;
         }
     }];
}

答案 2 :(得分:1)

如果您编写完成处理程序以将结果视为FBGraphObject,我发现它最有效:

[[FBRequest requestForMe] startWithCompletionHandler:^(FBRequestConnection *connection, FBGraphObject *result, NSError *error) {
  if (result && !error) {
    name = result[@"name"];
    email = result[@"email"];
    facebookId = result[@"id"];
  } else {
    NSLog(@"Error!");
  }
}];