ios – Parse:Facebook用户取消链接并再次登录创建两个用户表条目

我看到通过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!")
    }
  })
}

From Parse.com documentation:

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.

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...