在我的Xcode项目中,我已经有一个登录功能,它使用用户创建的用户名和密码。现在我想在项目中集成Facebook登录。我能够用Facebook编写简单的注册信息,将项目连接到Facebook。在这一步之后,我对两个问题感到难过:
要通过Facebook签名用户,我需要保存某种形式的唯一身份证明,以便他们在登录时再次使用它。但我不知道这个标识是什么或从何处获取它。
我只知道如何通过用户名和密码登录用户,我保存在解析中以获取他们的个人资料。这如何与Facebook合作?
任何人都可以帮我解决这两个问题吗?
我已复制了我的Facebook登录代码:
@IBOutlet var facebookSignUpButton: FBSDKLoginButton!
var fullnameFB = String()
var emailFB = String()
var isFBSignUp = Bool()
override func viewDidLoad() {
super.viewDidLoad()
signUpWithFacebook()
}
//
// Facebook Sign Up Functions =>
//
func signUpWithFacebook() {
facebookSignUpButton.readPermissions = ["email", "public_profile"]
facebookSignUpButton.delegate = self
self.view.addSubview(facebookSignUpButton)
}
//what to do when logged in
func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error!) {
if error != nil { //if theres an error
print(error)
} else if result.isCancelled { // if user cancels the sign up request
print("user cancelled login")
} else {
if result.grantedPermissions.contains("email") {
if let graphRequest = FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "email, name"]) {
graphRequest.start(completionHandler: { (connection, result, error) in
if error != nil {
print(error?.localizedDescription ?? String())
} else {
if let userDetails = result as? [String: String]{
print(userDetails)
self.fullnameFB = userDetails["name"]!
self.emailFB = userDetails["email"]!
self.isFBSignUp = true
}
self.performSegue(withIdentifier: "SignUpP2VC", sender: nil)
}
})
}
} else {
print("didnt get email")
createAlert(title: "Facebook Sign Up", message: "To signup with Facebook, we need your email address")
}
}
}
//logout function
func loginButtonDidLogOut(_ loginButton: FBSDKLoginButton!) {
print("logged out")
}