【问题标题】:cannot fetch email from facebook,ios.Get null always无法从 facebook、ios 获取电子邮件。始终获取 null
【发布时间】:2015-09-15 13:53:40
【问题描述】:
-(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 新手,不知道该怎么做。

【问题讨论】:

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


    【解决方案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 信息。希望这可以帮助。不要只是复制和粘贴,请通读它,以便了解它是如何工作的!

    【讨论】:

    • 对不起,我没听懂。我没有得到用户数据字典
    • 您对 userData 字典有哪些不了解的地方? userData NSDictionary 包含来自您的 FBSDKGraphRequest 的结果,您是否检查了控制台的 NSLog 以确保调用“here 3”?
    • 是的,我在控制台中检查了它。我将代码粘贴到登录用户中
    • 您是否完成了将 Facebook 添加到您的应用的所有其他步骤?您必须在 developer.facebook.com 上注册,然后在 info.plist 中添加一些内容 - 您是否完成了这些步骤?
    • 这对我有用,只有在我删除了不合理数量的参数后。我只使用了@{@"fields": @"id, name, first_name, last_name, email, birthday"},它终于奏效了。感谢非常易读的代码 sn-p。抱歉,您花了 2 年时间才获得支持,但您值得拥有!
    猜你喜欢
    • 2015-11-07
    • 2018-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-10
    • 1970-01-01
    相关资源
    最近更新 更多