PFFacebookUtils。如何区分登录和注册?
Posted
技术标签:
【中文标题】PFFacebookUtils。如何区分登录和注册?【英文标题】:PFFacebookUtils. How to make difference between login and signup? 【发布时间】:2016-03-27 11:11:46 【问题描述】:我正在尝试在我的使用Parse.com
服务的应用程序中提供一些非常简单(如我所想)的功能。我需要的只是允许用户通过 Facebook 创建一个帐户并通过 Facebook 再次登录。
问题是PFFacebookUtils
登录方法不仅通过Facebook登录用户,还创建了一个新的PFUser
。为什么这对我来说是个问题?嗯,当然。我可以通过isNew
字段区分注册和登录,但这并没有真正的帮助。
考虑以下情况 - 用户尝试通过 Facebook 登录(他还没有PFUser
),他登录,创建了一个新用户。我看到该用户是新用户(即该用户之前未注册),我必须拒绝此登录。好吧,我拒绝他,我说“你还没注册,去注册吧”。用户注册(通过相同的登录方法),这次返回相同的PFUser
,这是用户尝试登录时创建的。我看到用户不是新用户,它已经注册,因此我必须再次拒绝该用户,因为该帐户已经存在并且不可能再次创建相同的帐户。
你明白这个问题吗?我是愚蠢的没有意识到如何处理PFFacebookUtils
帐户创建和登录,还是PFFacebookUtils
提供了一个愚蠢的API?你们是怎么做到的?你如何解决我描述的问题。真的,它一定很简单,但我在任何地方都找不到很好的例子
【问题讨论】:
【参考方案1】:我在 swift 中有登录和注册代码,用于检查用户是否是新用户登录和注册。这是我的代码:
登录
let spinningActivity = MBProgressHUD.showHUDAddedTo(self.view, animated: true)
spinningActivity.label.text = "Just a Moment"
spinningActivity.detailsLabel.text = "Logging in"
if reachabilityStatus == kNOTREACHABLE
spinningActivity.hideAnimated(true)
self.displayError("No Internet Connection", message: "Please connect to the internet before continuing")
else
let permissions = ["public_profile"]
PFFacebookUtils.logInInBackgroundWithReadPermissions(permissions) (user:PFUser?, error:NSError?) -> Void in
if error != nil
spinningActivity.hideAnimated(true)
self.displayError("Error", message: error!.localizedDescription)
else if let user = user
if user.isNew
spinningActivity.hideAnimated(true)
PFUser.currentUser()?.deleteInBackground()
self.displayNoticeWithTwoActions("Account Not Found", message: "This Facebook account is not in our system. You have to sign up first.", firstButtonTitle: "Sign Up",closeButtonTitle: "Ok", segue: "dontHaveAccountSegue")
else
spinningActivity.hideAnimated(true)
self.performSegueWithIdentifier("successfulLoginSegue", sender: self)
else
PFUser.currentUser()?.deleteInBackground()
spinningActivity.hideAnimated(true)
self.displayError("Error", message: "Unless you tapped on 'Cancel' or 'Done', something went wrong. Please try again.")
注册
我有一个注册按钮,然后是一个在登录按钮中实现的功能,称为“loadFacebookUserDetails”
let spinningActivity = MBProgressHUD.showHUDAddedTo(self.view, animated: true)
spinningActivity.label.text = "Just a Moment"
spinningActivity.detailsLabel.text = "Loading Details"
if reachabilityStatus == kNOTREACHABLE
spinningActivity.hideAnimated(true)
self.displayError("No Internet Connection", message: "Please connect to the internet before continuing")
else
let permissions = ["public_profile", "email"]
PFFacebookUtils.logInInBackgroundWithReadPermissions(permissions) (user:PFUser?, error:NSError?) -> Void in
if let user = user
if !user.isNew
spinningActivity.hideAnimated(true)
PFUser.logOut()
self.displayNoticeWithTwoActions("Account Found", message: "This Facebook account already in our system. You have to log in first.", firstButtonTitle: "Log In", closeButtonTitle: "Cancel", segue: "haveAccountSegue")
else if error != nil
spinningActivity.hideAnimated(true)
self.displayError("Error", message: error!.localizedDescription)
else if error == nil
spinningActivity.hideAnimated(true)
self.loadFacebookUserDetails()
else
spinningActivity.hideAnimated(true)
self.displayError("Something Went Wrong", message: "Unless you tapped on 'Cancel' or 'Done', something went wrong. Please try again")
func loadFacebookUserDetails()
let spinningActivity = MBProgressHUD.showHUDAddedTo(self.view, animated: true)
spinningActivity.mode = MBProgressHUDMode.AnnularDeterminate
spinningActivity.label.text = "Just a Moment"
spinningActivity.detailsLabel.text = "Loading Details"
let requestPerameters = ["fields": "id, email, first_name, last_name, name"]
let userDetails = FBSDKGraphRequest(graphpath: "me", parameters: requestPerameters)
userDetails.startWithCompletionHandler (connection, result, error:NSError!) -> Void in
if error != nil
spinningActivity.hideAnimated(true)
self.displayError("Error", message: error!.localizedDescription)
PFUser.logOut()
else
let userID:String = result["id"] as! String
let userEmail:String = result["email"] as! String
let userFirstName:String = result["first_name"] as! String
let userLastName:String = result["last_name"] as! String
// Get Facebook Profile Picture
let userProfile = "https://graph.facebook.com/" + userID + "/picture?type=large"
let usernameLink = "https://graph.facebook.com/" + userID
let username = usernameLink.stringByReplacingOccurrencesOfString("https://graph.facebook.com/", withString: "")
let profilePictureUrl = NSURL(string: userProfile)
let profilePictureData = NSData(contentsOfURL: profilePictureUrl!)
if profilePictureData != nil
let profilePictureObject = PFFile(data: profilePictureData!)
PFUser.currentUser()?.setObject(profilePictureObject!, forKey: "profile_picture")
PFUser.currentUser()?.setObject(userFirstName, forKey: "first_name")
PFUser.currentUser()?.setObject(userLastName, forKey: "last_name")
PFUser.currentUser()?.setObject(username, forKey: "facebook_link")
if userEmail == userEmail
PFUser.currentUser()?.email = userEmail
PFUser.currentUser()?.saveInBackgroundWithBlock( (success:Bool, error:NSError?) -> Void in
if error != nil
spinningActivity.hideAnimated(true)
self.displayError("Error", message: error!.localizedDescription)
PFUser.logOut()
else if success == true
if !userID.isEmpty
spinningActivity.hideAnimated(true)
NSUserDefaults.standardUserDefaults().setObject("authData", forKey: "facebookAuth")
NSUserDefaults.standardUserDefaults().synchronize()
self.performSegueWithIdentifier("facebookUserDetailsSegue", sender: self)
else
spinningActivity.hideAnimated(true)
self.displayError("Something Went Wrong", message: "Please try again")
PFUser.logOut()
)
如果您在转换到目标 c 时遇到问题,我敢打赌,您可以在 YouTube 上找到有关如何执行此操作的视频。
【讨论】:
以上是关于PFFacebookUtils。如何区分登录和注册?的主要内容,如果未能解决你的问题,请参考以下文章
用户在使用 PFFacebookUtils 登录时取消选择 Facebook 权限
在相同情况下,PFFacebookUtils 登录结果为无效范围
PFFacebookUtils 登录后台无法将值转换为 PFUserResultBlock
尝试使用 Parse PFFacebookUtils 登录 Facebook 时出现错误 307