Facebook登录 - 获取用户数据

时间:2014-12-01 11:08:38

标签: ios facebook login parse-platform

我正在使用Parse作为后端在iOS上构建应用程序。

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

问题是:有时候我没有得到一些信息,比如eMail(userData [@“email”]是nil),first_name,name ..或者Profile Pic没有'下载。

并非所有时间(实际上非​​常罕见),并且无法可靠地复制问题,因此我可以解决它。而且通常只有一个缺失的领域不是全部。

所以我在我的数据库中遇到漏洞(当用户的电子邮件地址不是很酷: 我的主要关注

以下是我编写方法的方法(我编辑了大部分if(错误)管理以使其更短)。

有什么想法吗?

谢谢你们!

    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];
        }
    }];

然后我调用我的“fbData”函数,以便将用户数据填入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

             }
         }];
   }];

1 个答案:

答案 0 :(得分:1)

试试这个

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