我看到通过Facebook代码登录的解析看起来像这样.
PFFacebookUtils.logInInBackgroundWithReadPermissions(permissions) { (user: PFUser?,error: NSError?) -> Void in if let user = user { if user.isNew { println("User signed up and logged in through Facebook!") let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graPHPath: "me",parameters: nil) graphRequest.startWithCompletionHandler({ (connection,result,error) -> Void in if ((error) != nil) { println("Error: \(error)") } else { let userEmail : Nsstring = result.valueForKey("email") as! Nsstring println("User Email is: \(userEmail)") user["email"] = userEmail } }) } else { println("User logged in through Facebook!") } } else { println("Uh oh. The user cancelled the Facebook login.") } }
注销方法如下所示
PFFacebookUtils.unlinkUserInBackground(PFUser.currentUser()!) { (succeeded: Bool,error: NSError?) -> Void in if succeeded { FBSDKAccesstoken.setCurrentAccesstoken(nil) FBSDKProfile.setCurrentProfile(nil) PFUser.logout() } else { println("Error") } }
当用户第一次登录时,我看到解析在user表中创建了一行,其中authData指向Facebook.注销后,此authData将被删除.
问题是当用户再次登录时,解析会创建另一行并链接指向Facebook的authData,有没有办法避免这种情况.我想使用之前创建的同一行并链接到Facebook登录,而不是每次用户注销和登录时创建多行.
解决方法
您需要将用户链接到Facebook帐户,下面的代码为您执行以下链接:
if !PFFacebookUtils.isLinkedWithUser(user) { PFFacebookUtils.linkUserInBackground(user,withReadPermissions: nil,{ (succeeded: Bool?,error: NSError?) -> Void in if succeeded { println("Woohoo,the user is linked with Facebook!") } }) }
The steps that happen when linking are very similar to log in. The difference is that on successful login,the existing PFUser is updated with the Facebook information. Future logins via Facebook will Now log in the user to their existing account.