ios - 如何从 iOS 中的 Facebook SDK 获取用户信息?

标签 ios facebook facebook-sdk-3.1

当用户 session 打开时如何获取用户名。我尝试了 Facebook sdk 示例中的 session 登录示例。如果有人知道解决方案,请帮助我。 提前致谢。

 - (IBAction)buttonClickHandler:(id)sender {
        // get the app delegate so that we can access the session property
        SLAppDelegate *appDelegate = [[UIApplication sharedApplication]delegate];

        // this button's job is to flip-flop the session from open to closed
        if (appDelegate.session.isOpen) {
            // if a user logs out explicitly, we delete any cached token information, and next
            // time they run the applicaiton they will be presented with log in UX again; most
            // users will simply close the app or switch away, without logging out; this will
            // cause the implicit cached-token login to occur on next launch of the application
   //hear i need to fetch user name ?


            [appDelegate.session closeAndClearTokenInformation];

        } else {
            if (appDelegate.session.state != FBSessionStateCreated) {
                // Create a new, logged out session.
                appDelegate.session = [[FBSession alloc] init];
            }

            // if the session isn't open, let's open it now and present the login UX to the user
            [appDelegate.session openWithCompletionHandler:^(FBSession *session,
                                                             FBSessionState status,
                                                             NSError *error) {
                // and here we make sure to update our UX according to the new session state
                [self updateView];
            }];
        }
    }

最佳答案

登录验证后,您可以从事件的 FBSession 中获取详细信息,如下所示

if (FBSession.activeSession.isOpen) {

    [[FBRequest requestForMe] startWithCompletionHandler:
     ^(FBRequestConnection *connection,
       NSDictionary<FBGraphUser> *user,
       NSError *error) {
         if (!error) {
             NSString *firstName = user.first_name;
             NSString *lastName = user.last_name;
             NSString *facebookId = user.id;
             NSString *email = [user objectForKey:@"email"];
             NSString *imageUrl = [[NSString alloc] initWithFormat: @"http://graph.facebook.com/%@/picture?type=large", facebookId];
         }
     }];
}

更新:代替 id 使用 objectID 属性,在版本 v3.14.1(2014 年 5 月 12 日)发布后,id 属性已被弃用

NSString *facebookId = user.objectID;

关于ios - 如何从 iOS 中的 Facebook SDK 获取用户信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22111993/

相关文章:

ios - 将 UIImageView 设置为模糊的初始状态

ios - iOS:移除我随机放置的图片

android - 使用 facebook graph api 获取 friend 生日 v2.0

ios - 有没有办法在没有应用程序请求对话框的情况下向 Facebook 好友发送应用程序请求?

ios - Facebook sdk 不从 ios 应用程序发布图片

ios - 在 Swift 2 中检测物理接触

ios - 为什么隐式动画会覆盖我的 CABasicAnimation?

android - Facebook-Notification like Pullable Screen

Facebook read_stream 问题图 api

ios - 如何删除我的应用程序存储的有关 Facebook 的所有内容?