PFFacebookUtils logIn 有时会创建新用户,有时会登录
Posted
技术标签:
【中文标题】PFFacebookUtils logIn 有时会创建新用户,有时会登录【英文标题】:PFFacebookUtils logIn sometimes creates new user, sometimes logs in 【发布时间】:2014-08-31 19:05:12 【问题描述】:所以我使用 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];
);
);
];
【问题讨论】:
如果您允许匿名登录,请检查您的 parse.com 和 fb 应用设置 我已经在 parse.com 设置中“打开”了一切:匿名登录、facebook 登录、用户和密码、twitter。我不知道你所说的匿名登录的FB设置是什么意思。另外我还没有在 FB 中获得批准(可能是由于这个问题),所以我有几个测试帐户已获得该应用程序的授权 【参考方案1】:如果您已经设置了用户的 email
字段,并且您尝试使用具有相同电子邮件的用户登录 Facebook,Parse 会在数据库中使用 FB 身份验证创建一个新的空用户。您可能想将 FB Authenticated 用户与具有相同电子邮件的现有用户链接?
如果是这样,您可以检查匿名/电子邮件用户是否与 facebook 连接,如果没有,请在连接前删除 email
字段。
检查与 facebook 的连接:[PFFacebookUtils isLinkedWithUser:[PFUser currentUser]]
【讨论】:
以上是关于PFFacebookUtils logIn 有时会创建新用户,有时会登录的主要内容,如果未能解决你的问题,请参考以下文章
PFFacebookUtils 无法从 Facebook 注销
PFFacebookUtils logInWithPermissions 减慢应用程序