0
-(void)loginViewFetchedUserInfo:(FBLoginView *)loginView user:(id<FBGraphUser>)user ////////////////////////////// fetch logged in user information
{
    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];
                 NSLog(@" f0000000 %@",imageUrl);
                  NSLog(@" email =====   %@",email);
             }
         }];
    }



   NSString *email_id= [user objectForKey:@"email"];
    NSLog(@" fetch yo ----->%@",user.name); /////

     NSLog(@" object    ----->%@",email_id);


}

输出 - - - - - -

f0000000 http://graph.facebook.com/******/picture?type=large
email =====   (null)

f0000000 http://graph.facebook.com/*****/picture?type=large
email =====   (null)

我收到的电子邮件为空。如何获取用户或当前登录用户的电子邮件。我是 ios 新手,不知道该怎么做。

4

1 回答 1

1

我真的应该只是解释而不是发布一堆代码,但有时查看代码是了解一切如何协同工作的最佳方式。以下是我在我的应用程序中进行 Facebook 登录的方式:

// FBSample logic
// The user has initiated a login, so call the openSession method.

FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
[login logInWithReadPermissions:@[@"public_profile", @"email", @"user_friends"] handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
if (error) {
        // Process error
        NSLog(@"ERROR SIGNING IN");
        [self loginFailed];
    } else if (result.isCancelled) {
        // Handle cancellations
        NSLog(@"login cancelled");
        [self loginFailed];
    } else {
        NSLog(@"here 1");
        // If you ask for multiple permissions at once, you
        // should check if specific permissions missing
        if ([result.grantedPermissions containsObject:@"email"]) {
            // Do work
            NSLog(@"here 2");

            NSUserDefaults *fbstatus = [NSUserDefaults standardUserDefaults];
            [fbstatus setValue:@"NO" forKey:@"FBStatus"];
            [fbstatus synchronize];

            [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:@{@"fields": @"id, name, first_name, last_name, gender, picture, email, birthday, about, address, age_range, bio, currency, devices, education, favorite_athletes, favorite_teams, hometown, inspirational_people, interested_in, location, political, sports, security_settings, relationship_status, quotes, timezone, website, work, cover, family, photos, videos, friendlists, groups, permissions"}]
             startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error)
             {

                 if (!error)
                 {
                     NSLog(@"here 3");
                     NSDictionary *userData = (NSDictionary *)result;
                     // userData contains your FBSDKGraphRequest results
                     // do whatever you need to do now that you have Facebook data
                 } else {
                     NSLog(@"ERROR GETTING FB DATA");
                 }

             }];
        }
    }}];

该 NSDictionary *userData 将包含您被授予权限的所有 Facebook 信息。希望这可以帮助。并且不要只是复制和粘贴,通读它,以便您了解它是如何工作的!

于 2015-09-15T14:05:34.013 回答