【问题标题】:get Facebook user profile data after getting access token in iOS 5在 iOS 5 中获取访问令牌后获取 Facebook 用户个人资料数据
【发布时间】:2012-09-08 08:35:42
【问题描述】:

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

}

【问题讨论】:

  • 当我用 if(objappdel.session.isOpen) 替换上面代码中的 if 条件时,代码执行但给出错误代码 400。并记录空值。 session 是以下类型的属性:@property (strong, nonatomic) FBSession *session;

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


【解决方案1】:

在你的 .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添加的。希望对你有帮助。

【讨论】:

  • 谢谢 Murali..知道如何获取用户的 profile_image url
  • 您可以通过将用户 id 传递给 FBProfilePictureView 类来获取您的个人资料图片。为此,我们必须将 IBOutlet FBProfilePictureView *profilePic 的出口作为 UIImageView。现在在上面的方法中,loginViewFetchedUserInfo:,传递用户 id 就像self.profilePic.profileID = user.id;现在你可以看到你的头像了。
【解决方案2】:

这是获取用户头像的方法:

//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;
         }
     }];
}

【讨论】:

    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-29
      • 1970-01-01
      • 2021-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-30
      相关资源
      最近更新 更多