In my Xcode Project I already have a sign in feature that uses a user's username and password they created. Now I want to integrate a Facebook login in the project. I was able to write the simple sign up with Facebook where it will connect the project to their Facebook. After this step I am stumped on 2 problems:
To sign a user up through Facebook, I need to save some form of unique identification so they can user it again when they login. But I do not know what this identification is or where to get it from.
I only know how to log a user in through their username and password I saved in parse to get to their profile. How does this work with Facebook?
Can anyone help me with these two problems?
I have copied my code for what I have for Facebook login:
@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")
}
If you are using Parse, there's a helper library called PFFacebookUtils that handles User linking
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 {
// TODO: guard against nil => result?.token
PFFacebookUtils.logInInBackground(with: result!.token!) { (user, error) in
if let user = user {
if user.isNew {
print("User signed up and logged in through Facebook!")
} else {
print("User logged in through Facebook!")
}
// Get user email using FBSDKGraphRequest
} else {
print("Error while trying to login using Facebook: \(error?.localizedDescription ?? "---")")
}
}
}
}