如何从iOS中的Facebook SDK获取用户信息? [英] How to get user info from Facebook SDK in iOS?

查看:278
本文介绍了如何从iOS中的Facebook SDK获取用户信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How to get user name when user session is open.I tried the session login sample from Facebook sdk samples. if any one knows the solution please help me out. Thanks in advance.

 - (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获取详细信息

After login authentication, you can get details from active FBSession like below

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 属性已被弃用

Update: Instead of id use objectID property, after release of version v3.14.1(May 12, 2014), the id property has been deprecated

NSString *facebookId = user.objectID;

这篇关于如何从iOS中的Facebook SDK获取用户信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆