如何获取Facebook SDK的用户名和电子邮件ID? [英] How to get user name and email id for Facebook SDK?

查看:418
本文介绍了如何获取Facebook SDK的用户名和电子邮件ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的代码

 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 basic_info permissions when opening a session
        [FBSession openActiveSessionWithReadPermissions:@[@"basic_info,email"]
                                           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];
         }];
    }

从此代码我需要获取用户名和邮件ID。如果有人知道解决方案,请提前帮助我。

from this code i need to get user name and mail id. if any one know the solution please help me thanks in advance.

推荐答案

使用以下代码

 FBSession *session = [[FBSession alloc] initWithPermissions:@[@"basic_info", @"email"]];
    [FBSession setActiveSession:session];

    [session openWithBehavior:FBSessionLoginBehaviorForcingWebView
            completionHandler:^(FBSession *session,
                                FBSessionState status,
                                NSError *error) {
                if (FBSession.activeSession.isOpen) {
                    [[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);
                         }
                     }];
                }
            }];

这篇关于如何获取Facebook SDK的用户名和电子邮件ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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