gpt4 book ai didi

ios - PFFacebookUtils。如何区分登录和注册?

转载 作者:行者123 更新时间:2023-11-29 01:07:03 25 4
gpt4 key购买 nike

我试图在使用 Parse.com 服务的应用程序中提供一些非常简单(正如我所想)的功能。我需要的只是允许用户通过 Facebook 创建帐户并通过 Facebook 再次登录。

问题是 PFFacebookUtils 登录方法不仅通过 Facebook 登录用户,而且还创建了一个新的 PFUser。为什么这对我来说是个问题?嗯,当然。我可以通过 isNew 字段区分注册和加入,但这并没有真正的帮助。

考虑以下情况 - 用户尝试通过 Facebook 登录(他还没有 PFUser),他登录后,创建了一个新用户。我看到该用户是新用户(即该用户之前未注册),我必须拒绝此登录。好吧,我拒绝他,我说“你还没注册,去注册吧”。用户注册(通过相同的登录方法),这次返回相同的 PFUser ,这是在用户尝试登录时创建的。我看到用户不是新用户,它已经注册了因此我必须再次拒绝该用户,因为该帐户已经存在并且不可能再次创建相同的帐户。

你明白问题所在了吗?我是白痴没有意识到如何处理 PFFFacebookUtils 帐户创建和登录,还是 PFFFacebookUtils 提供了一个白痴 API?你们是怎么做到的?您如何解决我所描述的问题。真的,它一定很简单,但我在任何地方都找不到好的例子

最佳答案

我在 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()

}

})

}

}

}

如果您在转换为 objective-c 时遇到问题,我打赌您可以找到有关如何执行此操作的 YouTube 视频。

关于ios - PFFacebookUtils。如何区分登录和注册?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36246670/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com