ios - 使用自定义按钮和FBSession登录时如何获取用户数据

标签 ios objective-c facebook facebook-graph-api facebook-ios-sdk

我正在为 FBLogin 使用自定义按钮实现,而不是使用 FBLoginView 类。根据文档,我在按钮的 IBAction 中实现了 FBSession 方法:

// If the session state is any of the two "open" states when the button is clicked
if (FBSession.activeSession.state == FBSessionStateOpen
    || FBSession.activeSession.state == FBSessionStateOpenTokenExtended) {

    // Close the session and remove the access token from the cache
    // The session state handler (in the app delegate) will be called automatically
    [FBSession.activeSession closeAndClearTokenInformation];

    // If the session state is not any of the two "open" states when the button is clicked
} else {
    // Open a session showing the user the login UI
    // You must ALWAYS ask for public_profile permissions when opening a session
    [FBSession openActiveSessionWithReadPermissions:@[@"public_profile"]
                                       allowLoginUI:YES
                                  completionHandler:
     ^(FBSession *session, FBSessionState state, NSError *error) {

         // Retrieve the app delegate
         AppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
         // Call the app delegate's sessionStateChanged:state:error method to handle session state changes
         [appDelegate sessionStateChanged:session state:state error:error];
     }];
}

现在,当我在 FBLoginView Delegate 中获取用户数据时:

-(void)loginViewFetchedUserInfo:(FBLoginView *)loginView user:(id<FBGraphUser>)user{
NSLog(@"user info %@", user);}

用户给了我所有相关数据,例如用户的名字和姓氏、电子邮件 ID 等。我想要来自自定义 UI 实现的类似对象。我怎样才能做到这一点?

最佳答案

[[FBRequest requestForMe] startWithCompletionHandler:
          ^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) {
              if (!error) {
                  NSLog(@"accesstoken %@",[NSString stringWithFormat:@"%@",session.accessTokenData]);
                  NSLog(@"user id %@",user.id);
                  NSLog(@"Email %@",[user objectForKey:@"email"]);
                  NSLog(@"User Name %@",user.username);
              }
          }];

关于ios - 使用自定义按钮和FBSession登录时如何获取用户数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25525715/

相关文章:

php - Facebook api php在我被标记的地方获取 friend 照片

android - Facebook "feed"对话框显示问题

ios - 异步加载 UIView

iOS 8 SpriteKit 框架向后兼容性

ios - 如何从epub文件中获取封面图片?

iphone - 如何更改 UINavigationBar 中的编辑/完成按钮标题

android - 无效的 OAUTH 访问 token ,当使用应用程序访问 token 时,Android

ios - NSMutableArray 值未分配

objective-c - 由于未捕获的异常 'NSUnknownKeyException' 而终止应用程序,原因 : '[<AboutViewController 0x91cc1d0> setValue:forUndefinedKey:]:

ios - 如何从 AppDelegate 更新某些 ViewController 的标签?