Facebook登录 - 获取用户数据 [英] Facebook Login - Get User Data

查看:665
本文介绍了Facebook登录 - 获取用户数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这样做,我先使用Parse Facebook SDK,首先将Facebook链接到我的Parse用户然后查询他的数据并保存。



问题是:有时候我没有收到一些像eMail(userData [@email]为nil),first_name,name ..或Profile Pic不下载。



并不是所有的时间(实际上很少见),并且不可能可靠地复制问题,所以我可以解决它。而且通常只有一个缺失的字段不是全部。



所以我在数据库中有漏洞(当用户的电子邮件地址不好, 我的主要关注



这是我如何编写我的方法(我编辑了大多数if(错误)管理来缩短)。



任何想法?



谢谢你们!

  NSArray * permissionsArray = @ [@public_profile,@email,@user_friends]; 

[PFFacebookUtils initializeFacebook];

//登录PFUser使用Facebook
[PFFacebookUtils logInWithPermissions:permissionsArray block:^(PFUser * user,NSError * error){
if(user.isNew){
NSLog(@用户注册并登录!);
[self fbData:self];

} else {
NSLog(@用户已登录!);
[self fbDataAlreadyLog:self];
}
}];

然后我调用我的fbData函数,以便将用户数据填写到Parse: p>

   - (IBAction)fbData:(id)sender {

[PFFacebookUtils initializeFacebook];
FBRequest * request = [FBRequest requestForMe];

//向Facebook发送请求
[request startWithCompletionHandler:^(FBRequestConnection * connection,NSDictionary< FBGraphUser> * userCloud,NSError * error){
if(!error){
//结果是一个带有用户Facebook数据的字典
NSDictionary * userData = userCloud;

NSString * facebookID = userData [@id];

NSURL * pictureURL = [NSURL URLWithString:[NSString stringWithFormat:@https://graph.facebook.com/%@/picture?type=large&return_ssl_resources=1,facebookID]];

PFUser * user = [PFUser currentUser];
if(userData [@name]){
user [@name] = userData [@name];
}
if(userData [@first_name]){
user [@first_name] = userData [@first_name];
}
if(userData [@last_name]){
user [@last_name] = userData [@last_name];
}
if(userData [@id]){
user [@fbID] = userData [@id];
}
if(userData [@gender]){
user [@gender] = userData [@gender];
}
if(userData [@email]){
user.email = userData [@email];
user.username = userData [@email];
}
if(userData [@age_range]){
user [@age_range] = userData [@age_range];
}

SDWebImageManager * manager = [SDWebImageManager sharedManager];
[manager downloadImageWithURL:pictureURL
options:0
progress:^(NSInteger receivedSize,NSInteger expectedSize)
{
//进度跟踪代码
}
完成:^(UIImage * image,NSError *错误,SDImageCacheType cacheType,BOOL完成,NSURL * imageURL)
{
if(image)
{
// do图像
NSData * imageNSData = UIImagePNGRepresentation(image);
PFFile * imageFile = [PFFile fileWithName:@profilePicdata:imageNSData];
user [@image] = imageFile;

[user saveInBackgroundWithBlock];

//一旦保存块完成,我再管理一些东西,然后再转到下一个ViewController

}
}];
}];


解决方案

尝试这样

  NSArray * permissionsArray = @ [@user_about_me,@user_relationships,@user_birthday,@user_location]; 


I'm building an app on iOS using Parse as a backend.

Doing so I use the Parse Facebook SDK in order first to link Facebook to my Parse User and then to query his data and save them.

The Problem is: sometimes I don't get some of the info like eMail (userData[@"email"] is nil), first_name,name .., or the Profile Pic doesn't download.

Not all the time (quite rare actually), and impossible to reliably replicate the issue so I can solve it. And it's often only one missing field not all.

So I got holes in my Database (not cool when it is the user's email address: my main concern)

Here is how I wrote my methods (i edited most of the if(error) management to make it shorter).

Any ideas ?

Thank you guys !

    NSArray *permissionsArray = @[@"public_profile",@"email", @"user_friends"];

    [PFFacebookUtils initializeFacebook];

    // Login PFUser using Facebook
    [PFFacebookUtils logInWithPermissions:permissionsArray block:^(PFUser *user, NSError *error) {
        if (user.isNew) {
            NSLog(@"User with facebook signed up and logged in!");
            [self fbData:self];

        } else {
            NSLog(@"User with facebook logged in!");
            [self fbDataAlreadyLog:self];
        }
    }];

Then I call my "fbData" function in order to fill in the user data into Parse:

-(IBAction)fbData:(id)sender{

[PFFacebookUtils initializeFacebook];
FBRequest *request = [FBRequest requestForMe];

// Send request to Facebook
[request startWithCompletionHandler:^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *userCloud, NSError *error) {
    if (!error) {
        // result is a dictionary with the user's Facebook data
        NSDictionary *userData = userCloud;

        NSString *facebookID = userData[@"id"];

        NSURL *pictureURL = [NSURL URLWithString:[NSString stringWithFormat:@"https://graph.facebook.com/%@/picture?type=large&return_ssl_resources=1", facebookID]];

        PFUser *user = [PFUser currentUser];
        if (userData[@"name"]) {
            user[@"name"] = userData[@"name"];
        }
        if (userData[@"first_name"]) {
            user[@"first_name"] = userData[@"first_name"];
        }
        if (userData[@"last_name"]) {
            user[@"last_name"] = userData[@"last_name"];
        }
        if (userData[@"id"]) {
            user[@"fbID"] = userData[@"id"];
        }
        if (userData[@"gender"]) {
            user[@"gender"] = userData[@"gender"];
        }
        if (userData[@"email"]) {
            user.email = userData[@"email"];
            user.username = userData[@"email"];
        }
        if (userData[@"age_range"]) {
            user[@"age_range"] = userData[@"age_range"];
        }

        SDWebImageManager *manager = [SDWebImageManager sharedManager];
        [manager downloadImageWithURL:pictureURL
                              options:0
                             progress:^(NSInteger receivedSize, NSInteger expectedSize)
         {
             // progression tracking code
         }
                            completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished,NSURL *imageURL)
         {
             if (image)
             {
                 // do something with image
                 NSData *imageNSData = UIImagePNGRepresentation(image);
                 PFFile *imageFile = [PFFile fileWithName:@"profilePic" data:imageNSData];
                 user[@"image"] = imageFile;

                 [user saveInBackgroundWithBlock];

                 //Once the Save Block is done I managed a few more thing before moving on to the next ViewController

             }
         }];
   }];

解决方案

try like this

NSArray *permissionsArray = @[ @"user_about_me", @"user_relationships", @"user_birthday", @"user_location"];

这篇关于Facebook登录 - 获取用户数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆