检查Facebook帐户是否与PFUser相关联

时间:2014-07-21 17:50:14

标签: ios facebook parse-platform

在Parse文档中,它表示您可以检查用户是否链接到Facebook帐户,但有没有办法可以反过来?例如,假设用户单击按钮以使用Facebook登录,因为[PFUser currentUser]为零(因为用户尚未登录),Parse将为该用户创建一个新帐户,因为您不能使用[PFFacebookUtils isLinkedWithUser: [PFUser currentUser]]。似乎链接帐户的唯一方法是在用户定期登录后(即输入用户名和密码),但这确实很不方便。如果用户已经手动创建了一个帐户(通过填写我提供的注册表单)然后决定链接他们的Facebook帐户怎么办?那么无论如何都要检查Facebook帐户是否与用户链接而不是针对特定用户进行检查?

1 个答案:

答案 0 :(得分:0)

好的,这是解决方案。基本上遍历每个用户,看看Facebook帐户是否与之相关联。基于此,链接或创建一个新帐户。这是代码:

PFQuery *userQuery = [PFUser query];
    [userQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        NSUInteger count = [objects count];
        BOOL linked = NO;
        for(NSUInteger i = 0; i < count; i++){

            PFUser *checkUser = objects[i];
            if([PFFacebookUtils isLinkedWithUser:checkUser]){
                linked = YES;
                NSLog(@"profile is linked to a user");
                break;

            }
        }
        if(linked == YES){
            [PFFacebookUtils logInWithPermissions:_permissions block:^(PFUser *user, NSError *error) {
                if(!user){
                    NSLog(@"facebook cancelled");


                }
                else{

                    _type = @"twitter";
                    [self performSegueWithIdentifier:@"showMain" sender:self];                }


            }
             ];
        }
        else{
            CCAlertView *ask = [[CCAlertView alloc]initWithTitle:@"Are you already registered" message:nil];
            [ask addButtonWithTitle:@"No" block:^{

            [PFFacebookUtils logInWithPermissions:_permissions block:^(PFUser *user, NSError *error) {
                if(!user){
                    NSLog(@"Facebook cancelled");
                }
                else if(user.isNew){

                    _type = @"facebook";
                    [self performSegueWithIdentifier:@"showMain" sender:self];                }
                else{
                    [self performSegueWithIdentifier:@"showMain" sender:self];                  }

            }];

            }];
            [ask addButtonWithTitle:@"Yes" block:^{
                CCAlertView *log = [[CCAlertView alloc]initWithTitle:@"Enter username and password" message:nil];
                [log setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput];
                UITextField *un = [log textFieldAtIndex:0];
                UITextField *pw = [log textFieldAtIndex:1];
                [log addButtonWithTitle:@"Cancel" block:nil];
                [log addButtonWithTitle:@"Enter" block:^{
                    [PFUser logInWithUsernameInBackground:[un text] password:[pw text] block:^(PFUser *user, NSError *error) {
                        if(user){
                            [PFFacebookUtils linkUser:user permissions:_permissions block:^(BOOL succeeded, NSError *error) {
                                if(succeeded){
                                    NSLog(@"linked");
                                    [self performSegueWithIdentifier:@"showMain" sender:self];                                  }
                                else{
                                    NSLog(@"couldnt link");
                                }
                            }];
                        }
                        else{
                            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Oops!" message:[error userInfo] delegate:self cancelButtonTitle:nil otherButtonTitles:@"okay", nil];
                            [alert show];
                        }
                    }];

                }];
                [log show];
            }];
            [ask show];
        }

    }];