OStack程序员社区-中国程序员成长平台

标题: iOS Facebook 获取用户邮箱 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 10:51
标题: iOS Facebook 获取用户邮箱

我尝试了 2000 多种方法来获取用户的电子邮件。我无法从 Facebook SDK 的图形 API 中获取它。它不包含电子邮件属性。我还尝试将电子邮件属性手动添加到 FB 框架中,但没有任何反应。是否可以下载第一个兼容 iOS 7 的 FB SDK?它仍然具有电子邮件属性,不是吗?或者有没有其他方法可以获得真实的电子邮件,我必须与他们合作。我不需要 [email protected]

感谢您的建议。

编辑

NSArray *permissions = @[@"email", @"public_profile"];
    [PFFacebookUtils logInWithPermissions:permissions block:^(PFUser *user, NSError *error) {
        if (!user) {
            if (!error) {
                NSLog(@"The user cancelled the Facebook login.");
            } else {
                NSLog(@"An error occurred: %@", error.localizedDescription);
            }


            if ([delegate respondsToSelectorselector(commsDidLogin]) {
                [delegate commsDidLogin:NO];
            }
        } else {
            if (user.isNew) {
                NSLog(@"User signed up and logged in through Facebook!");
            } else {
                NSLog(@"User logged in through Facebook!");
            }

            [FBRequestConnection startForMeWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
                if (!error) {
                    PFUser *usere = [PFUser currentUser];
                    [usere setObject:[result objectForKey"first_name"] forKey"name"];
                    [usere setObject:[result objectForKey"last_name"] forKey"surname"];
                 //   [usere setObject:[result objectForKey"email"] forKey"mail"];
                    [usere saveEventually];
                     NSLog(@"user info: %@", result);
                        }
            }];

                }
                if ([delegate respondsToSelectorselector(commsDidLogin]) {
                    [delegate commsDidLogin:YES];
                }
            }];
        }



Best Answer-推荐答案


我没有图形 API 的代码,

但是对于新的 facebook sdk 版本 3,我有相应的代码。

-(void)openFbSession
{
    [[self appDelegate].session closeAndClearTokenInformation];

    NSArray *permissions =     [NSArray arrayWithObjects"email",@"user_location",@"user_birthday",@"user_hometown",nil];
    [self appDelegate].session = [[FBSession alloc] initWithPermissions:permissions];

    [[self appDelegate].session openWithCompletionHandler:^(FBSession *session,
                                                            FBSessionState status,
                                                            NSError *error) {
        if(!error)
        {
            NSLog(@"success");
            [self myFbInfo];
        }
        else
        {
            NSLog(@"failure");
        }

    }];
}

对于所有信息,myFbInfo 方法是

-(void)myFbInfo
{
    [FBSession setActiveSession:[self appDelegate].session];

    [[FBRequest requestForMe] startWithCompletionHandler:^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *FBuser, NSError *error) {
        if (error) {
            // Handle error
        }

        else {
            //NSString *userName = [FBuser name];
            //NSString *userImageURL = [NSString stringWithFormat"https://graph.facebook.com/%@/picture?type=large", [FBuser id]];
            NSLog(@"Name : %@",[FBuser name]);
            NSLog(@"first name : %@",[FBuser first_name]);
            NSLog(@"Last name : %@",[FBuser last_name]);
            NSLog(@"ID : %@",[FBuser id]);
            NSLog(@"username : %@",[FBuser username]);
            NSLog(@"Email : %@",[FBuser objectForKey:@"email"]);

            NSLog(@"user all info : %@",FBuser);

              }
    }];

}

编辑


在 appdelegate.h 中

@property (strong, nonatomic) FBSession *session;

在 appdelegate.m 中

- (BOOL)application: (UIApplication *)application openURL: (NSURL *)url sourceApplication: (NSString *)sourceApplication annotation: (id)annotation
{
    //NSLog(@"FB or Linkedin clicked");
    return [self.session handleOpenURL:url];
}


- (void)applicationDidBecomeActiveUIApplication *)application
{
    [FBSession.activeSession handleDidBecomeActive];
}

- (void)applicationWillTerminateUIApplication *)application
{
    [self.session close];
}

关于iOS Facebook 获取用户邮箱,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24890798/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4