检查Facebook账号是否已链接到PFUser

3
在 Parse 的文档中,它说你可以检查用户是否链接到 Facebook 帐户,但是否有任何方法来反过来做呢?例如,假设用户点击按钮以使用 Facebook 登录,由于[PFUser currentUser]是空的(因为用户尚未登录),Parse 将为该用户创建一个新帐户,因为您无法使用[PFFacebookUtils isLinkedWithUser: [PFUser currentUser]]。似乎唯一的方法是在用户常规登录后链接帐户(即输入他们的用户名和密码),但这真的很麻烦。如果用户已经手动创建了一个帐户(通过填写我提供的注册表格),然后决定链接他们的 Facebook 帐户怎么办?所以有没有办法通用地检查 Facebook 帐户是否链接到用户,而不是针对特定用户进行检查?

1
据我了解,如果有人已经将登录链接到Facebook,如果他们在另一台设备上使用Facebook登录,则会链接到同一个Parse用户,因此[PFUser currentUser]将返回旧用户,而不是新用户。 - Timothy Walters
1个回答

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

    }];

我认为应该避免这里概述的方法,正如@timothy-walters在原始帖子问题中留下的评论所述。 - Mason Lee

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接