无法从Facebook获取电子邮件,ios.Get null始终

时间:2015-09-15 13:53:41

标签: ios objective-c facebook facebook-graph-api ios8.4

-(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)

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

1 个答案:

答案 0 :(得分: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信息。希望这可以帮助。不要只是复制和粘贴,阅读它,以便了解它是如何工作的!