ios Facebook SDK 3.0:如何接收用户的电子邮件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11843968/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 19:37:45  来源:igfitidea点击:

Facebook SDK 3.0: how to receive user's e-mail?

iosfacebooksdk

提问by znq

In the "old" FB iOS SDK I could receive user information via the following:

在“旧”FB iOS SDK 中,我可以通过以下方式接收用户信息:

NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                   @"SELECT uid, name, email, pic FROM user WHERE uid=me()", @"query",
                                   nil];

    JBFacebookManager *fbManager = [JBFacebookManager getInstance];
    fbManager.requestDelegate = self;

    [fbManager.facebook requestWithMethodName:@"fql.query"
                                     andParams:params
                                 andHttpMethod:@"POST"
                                   andDelegate:self];

How can I do this with the new FB iOS SDK 3.0? Do I need to use FBRequestor FBOpenGraphActionor FBGraphObjector a combination of those or something completely different?

如何使用新的 FB iOS SDK 3.0 做到这一点?我是否需要使用FBRequestFBOpenGraphActionFBGraphObject或这些或完全不同的东西的组合?

回答by znq

if (FBSession.activeSession.isOpen) {
    [[FBRequest requestForMe] startWithCompletionHandler:^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) {
         if (!error) {
             self.nameLabel.text = user.name;
             self.emailLabel.text = [user objectForKey:@"email"];
         }
     }];
}

Because FBGraphUserdoesn't have an email @property, we can't access the information like with the name (dot-syntax), however the NSDictionary still has the email kv-pair and we can access it like we would do with a normal NSDictionary.

因为FBGraphUser没有 email @property,我们不能像使用名称(点语法)那样访问信息,但是 NSDictionary 仍然有 email kv-pair,我们可以像使用普通 NSDictionary 一样访问它。

Don't forget to ask for the email permission though:

不要忘记请求电子邮件许可:

NSArray *permissions = [[NSArray alloc] initWithObjects:@"email", nil];
[FBSession sessionOpenWithPermissions:permissions completionHandler:
 ^(FBSession *session, FBSessionState state, NSError *error) {
     [self facebookSessionStateChanged:session state:state error:error];
 }];

回答by Zorayr

Once you have access to (id<FBGraphUser>)user, you could simply use, user[@"email"].

一旦您可以访问(id<FBGraphUser>)user,您就可以简单地使用 , user[@"email"]

回答by jasondinh

Did you read the source code of the 3.0 SDK? There is a method that I think is identical:

你读过3.0 SDK的源代码了吗?有一种方法我认为是相同的:

- (FBRequest*)requestWithMethodName:(NSString *)methodName
                          andParams:(NSMutableDictionary *)params
                      andHttpMethod:(NSString *)httpMethod
                        andDelegate:(id <FBRequestDelegate>)delegate;

回答by Vineesh TP

from this we can get facebook user's basic info and email id.

从中我们可以获得facebook用户的基本信息和电子邮件ID。

[FBSession openActiveSessionWithReadPermissions:@[@"basic_info",@"email"] allowLoginUI:YES completionHandler:^(FBSession *session,FBSessionState status,NSError *error){
        if(error)
        {
            NSLog(@"Facebook Error : %@",error.localizedDescription);
        }
        else{

        }
        // Respond to session state changes,
        // ex: updating the view

    }];

回答by Ashish

The easiest method for getting the info after logging in is :

登录后获取信息的最简单方法是:

-(IBAction)loginWithFacebook:(id)sender{
FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
[login
 logInWithReadPermissions: @[@"public_profile",@"email"]
 fromViewController:self
 handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
     if (error) {
         NSLog(@"Process error");
     } else if (result.isCancelled) {
         NSLog(@"Cancelled");
     } else {
         NSLog(@"Logged in");
         [self getFacebookProfileInfos];

     }
  }];
}

-(void)getFacebookProfileInfos {
NSDictionary *parameters = @ {@"fields": @"id, name, first_name, last_name, picture.type(large), email"};
if ([FBSDKAccessToken currentAccessToken]) {
    [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:parameters]
     startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
         if (!error) {
             NSLog(@"fetched user:%@", result);
         }
     }];
}

}

}

You will get all the info the result.

您将获得结果的所有信息。