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

前端 未结 5 1917
执笔经年
执笔经年 2020-12-04 20:07

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

NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys         


        
相关标签:
5条回答
  • 2020-12-04 20:19
    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 FBGraphUser doesn'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.

    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];
     }];
    
    0 讨论(0)
  • 2020-12-04 20:22

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

    - (FBRequest*)requestWithMethodName:(NSString *)methodName
                              andParams:(NSMutableDictionary *)params
                          andHttpMethod:(NSString *)httpMethod
                            andDelegate:(id <FBRequestDelegate>)delegate;
    
    0 讨论(0)
  • 2020-12-04 20:24

    from this we can get facebook user's basic info and email 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
    
        }];
    
    0 讨论(0)
  • 2020-12-04 20:34

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

    0 讨论(0)
  • 2020-12-04 20:38

    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.

    0 讨论(0)
提交回复
热议问题