2

我正在开发一个 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];
}

}

4

3 回答 3

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 添加了此方法。希望对您有所帮助。

于 2012-09-15T13:18:41.933 回答
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;
         }
     }];
}
于 2013-02-08T19:37:33.293 回答
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!");
  }
}];
于 2014-12-05T02:49:07.230 回答