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

Iphone 在iOS 5中获取访问令牌后获取Facebook用户配置文件数据,iphone,objective-c,facebook,facebook-graph-api,ios5,Iphone,Objective C,Facebook,Facebook Graph Api,Ios5,我正在开发一个iOS 5应用程序,在成功登录后需要获取用户配置文件数据 现在,当用户成功登录时,我将获得一个访问令牌 现在,我正在尝试使用该访问令牌获取Facebook登录的用户配置文件数据 我试图遵循Facebook开发者网站上提供的示例应用程序 我没有使用FBconnect类型的资源文件。我从示例应用程序中添加了“facebookSDK.framework” 请帮帮我 谢谢 - (void)populateUserDetails { FBRequestConnection *connect

我正在开发一个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];
}
-(无效)populateUserDetails{
FBRequestConnection*连接=[[FBRequestConnection alloc]init];
//第一个请求上传照片。
FBRequest*request1=[FBRequestRequestForme];
if(FBSession.activeSession.isOpen){
[连接添加请求:请求1
completionHandler:
^(FBRequestConnection*连接,NSDictionary*用户,NSError*错误){
如果(!错误){
NSLog(@“fb用户名=%@”,user.name);
NSLog(@“fb用户名=%@”,user.id);
}
NSLog(@“fb2用户名=%@”,user.name);
NSLog(@“fb2用户名=%@”,user.id);
}
//batchEntryName:@“photopost”
];
[连接启动];
}
}在您的.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);

  }
-(void)loginView蚀刻教育信息:(FBLoginView*)loginView用户:(id)用户{
NSLog(@“usr_id::%@”,user.id);
NSLog(@“usr_名字::%@”,user.first_名字);
NSLog(@“usr_中间_名称::%@”,user.middle_名称);
NSLog(@“usr\u last\u nmae::%@”,user.last\u name);
NSLog(@“usr_用户名::%@”,user.Username);
NSLog(@“usr_b_day::%@”,user.birth);
}
上面的方法是在FBLoginView.h中定义的,您可以在那里看到


此方法是我根据最新的facebookSDK.framework添加的。希望对您有所帮助。

这是获取用户配置文件图片的方法:

//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;
         }
     }];
}
//在应用程序委托文件中配置“FBProfilePictureView”类
-(void)ApplicationIDFinishLaunching:(UIApplication*)应用程序{
[FBProfilePictureView类];
}
//在头文件中
IBOutlet FBProfilePictureView*用户PictureView;
@属性(强,非原子)FBProfilePictureView*userPictureView;
//这里的“userPictureView”是uiview而不是uiimageview
//在你的.m文件中
-(无效)设置配置文件图片{
[FBSession.activeSession closeAndClearTokenInformation];
NSArray*权限=[NSArray arrayWithObjects:@“电子邮件”,无];
[FBSession openActiveSessionWithReadPermissions:权限
阿洛洛洛吉努伊:是的
completionHandler:
^(第*届会议,
FBSessionState状态,N错误*错误){
NSLog(@“\nfb sdk错误=%@”,错误);
开关(状态){
案例FBSessionStateOpen:
[[FBRequest requestForMe]startWithCompletionHandler:
^(FBRequestConnection*连接,NSDictionary*用户,NSError*错误){
如果(!错误){
self.userPictureView.profileID=[user objectForKey:@“id]”;
} 
}];
打破
案件审理结束:
//需要处理
打破
案例FBSessionStateClosedLogin失败:
//需要处理
打破
违约:
打破
}
}];
}

我发现如果编写完成处理程序将结果作为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!");
  }
}];

当我用if(objappdel.session.isOpen)替换上面代码中的if条件时,代码执行,但给出错误代码400。和日志空值。会话是以下类型的属性:@property(强,非原子)FBSession*会话;谢谢Murali.你知道如何获取用户的个人资料图片url吗?你可以通过将用户id传递给FBProfilePictureView类来获取你的个人资料图片。为此,我们必须将一个插座作为IBOutlet FBProfilePictureView*profilePic传递给UIImageView。现在在上面的方法中,loginViewFetchedUserInfo:,传递用户id,如self.profilePic.profileID=user.id;现在您可以看到您的个人资料照片。