微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐