PFFacebookUtils logIn有时会创建新用户,有时会登录

时间:2014-08-31 19:05:13

标签: ios parse-platform facebook-ios-sdk

所以我使用的是parse.com,自动匿名用户,以及使用facebook(iOS)登录/注册。问题在于,有时它会正确登录,并且其他时间(大多数情况下)会创建一个看似匿名的新用户。新用户(匿名 - >非匿名和Facebook链接)和用户登录(匿名 - >登录Facebook已经链接)都会发生这种情况。

我使用的是与parse.com文档中提供的基本相同的代码。任何帮助,将不胜感激。这是我点击facebook按钮时的代码。

NSArray *permissions = @[@"public_profile", @"email", @"user_friends", @"publish_actions"];
[SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeGradient];
[PFFacebookUtils logInWithPermissions:permissions block:^(PFUser *user, NSError *error) {
    [SVProgressHUD dismiss];
    [SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeGradient];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSError *error = nil;
        //try refreshing the user in case that is the problem (its not, sometimes?).
        [user refresh:&error];
        if (error) {
            NSLog(@"ERROR. %@", error.localizedDescription);
        }
        NSLog(@"%@", user);

        dispatch_async(dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];

            if (!user) {
                NSString *errorMessage = nil;
                if (!error) {
                    NSLog(@"User cancelled the Facebook login.");
                    errorMessage = @"User cancelled the Facebook login.";
                } else {
                    NSLog(@"Uh oh. An error occurred: %@", error);
                    errorMessage = [error localizedDescription];
                }
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Log In Error"
                                                                message:errorMessage
                                                               delegate:nil
                                                      cancelButtonTitle:@"OK"
                                                      otherButtonTitles:nil];
                [alert show];
            } else {
                if (user.isNew) {
                    NSLog(@"User for Facebook! (new user)");

                    [SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeGradient];

                    FBRequest *request = [FBRequest requestForMe];
                    [request startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
                        [SVProgressHUD dismiss];
                        if (!error) {
                            NSDictionary *userData = (NSDictionary *)result;
                            NSString *name = userData[@"name"];
                            user[@"displayName"] = name;
                            user[@"facebookName"] = name;
                            user[@"facebookId"] = [result objectForKey:@"id"];

                            [SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeGradient];

                            [user saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
                                [SVProgressHUD dismiss];

                                if (error) {
                                    NSLog(@"%@", error.description);
                                }
                                [self showProfileSetupView];

                                [Global checkAndUpdateTrophyWithIdentifier:TROPHYConnectWithFacebook inViewController:self.navigationController];
                            }];
                            NSLog(@"User signed up and logged in through Facebook!");

                        } else {
                            if ([error.userInfo[FBErrorParsedJSONResponseKey][@"body"][@"error"][@"type"] isEqualToString:@"OAuthException"]) {
                                NSLog(@"The facebook session was invalidated");
                            }
                            else {
                                NSLog(@"Some other error: %@", error);
                            }
                        }
                    }];
                } else {
                    NSLog(@"User logged in through Facebook!");

                    if (!user[@"displayName"] || !user[@"facebookName"] || !user[@"facebookId"]) {

                        [SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeGradient];

                        FBRequest *request = [FBRequest requestForMe];
                        [request startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
                            [SVProgressHUD dismiss];

                            if (!error) {
                                NSDictionary *userData = (NSDictionary *)result;
                                NSString *name = userData[@"name"];
                                user[@"displayName"] = name;
                                user[@"facebookName"] = name;
                                user[@"facebookId"] = [result objectForKey:@"id"];

                                [user saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
                                    if (error) {
                                        NSLog(@"%@", error.description);
                                    }
                                }];
                                [self.delegate userSignUpViewControllerDidLogIn:self];

                            } else {
                                if ([error.userInfo[FBErrorParsedJSONResponseKey][@"body"][@"error"][@"type"] isEqualToString:@"OAuthException"]) {
                                    NSLog(@"The facebook session was invalidated");
                                }
                                else {
                                    NSLog(@"Some other error: %@", error);
                                }
                            }


                        }];
                    }
                    else {
                        [self.delegate userSignUpViewControllerDidLogIn:self];
                    }
                }
            }

        });
    });

}];

1 个答案:

答案 0 :(得分:0)

如果您已经设置了用户的email字段,并且您尝试使用具有相同电子邮件的用户进行Facebook登录,则Parse会使用FB身份验证在数据库中创建一个新的空用户。您可能想要将FB Authenticated用户与具有相同电子邮件的现有用户链接?

如果是这种情况,您可以检查匿名/电子邮件用户是否与Facebook连接,如果不是,请在连接前删除email字段。

检查与Facebook的关联:[PFFacebookUtils isLinkedWithUser:[PFUser currentUser]]