【问题标题】:Check if Facebook account is linked to PFUser检查 Facebook 帐户是否链接到 PFUser
【发布时间】:2014-09-12 08:03:12
【问题描述】:

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

【问题讨论】:

  • 我的理解是,如果有人已经将登录链接到 Facebook,如果他们随后在另一台设备上使用 Facebook 登录,它将链接到同一个 Parse 用户,因此[PFUser currentUser] 将返回老用户,不是新用户。

标签: ios facebook parse-platform


【解决方案1】:

好的,这就是解决方案。基本上遍历每个用户,看看 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 留下的原始帖子问题的评论
猜你喜欢
  • 2013-04-20
  • 2018-01-13
  • 2014-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多