Skip to content

Instantly share code, notes, and snippets.

@LamourBt
Created July 15, 2016 04:00
Show Gist options
  • Save LamourBt/130e5d2479893fef8d83b45ede7655f2 to your computer and use it in GitHub Desktop.
Save LamourBt/130e5d2479893fef8d83b45ede7655f2 to your computer and use it in GitHub Desktop.
Facebook Login Swift 2.0 and Parse-Server (latest version)
// Mark: -Facebook
func loginWithFaceBookInBackground(sender:UIButton) {
let permissions = ["public_profile", "email"]
PFFacebookUtils.logInInBackgroundWithReadPermissions(permissions) { (_user:PFUser?, _error:NSError?) in
if _error == nil {
if let possibleUser = _user{
print("\(possibleUser.sessionToken) , \(possibleUser)")
if possibleUser.isNew {
self.accessingFacebookCredentialsToSaveToParse()
} else {
print("existing user")
}
}
} else {
print("fb loginPermission .. error \(_error?.localizedDescription)")
}
}
} // end of function
func accessingFacebookCredentialsToSaveToParse(){
let requestPermissions = ["fields":"id,name,email"]
let graphRequest = FBSDKGraphRequest(graphPath: "me", parameters: requestPermissions)
graphRequest.startWithCompletionHandler({ (_connection:FBSDKGraphRequestConnection!, results:AnyObject!, err:NSError!) in
if err == nil {
let id = results.valueForKey("id") as? String
let name = results.valueForKey("name") as? String
let email = results.valueForKey("email") as? String
if let facebookID = id, let username = name , let userEmail = email {
print("\(facebookID), \(username), \(userEmail)")
let userObject = PFUser.currentUser()
userObject?.setObject(userEmail, forKey: "email")
userObject?.setObject(facebookID, forKey: "_id")
userObject?.setObject(username, forKey: "username")
userObject?.saveInBackgroundWithBlock({ (success:Bool, error:NSError?) in
if error == nil {
print("PFUser was saved")
// then display UI
} else {
print("was not able to saved the user to parse \(error?.localizedDescription)")
}
})
}
} else {
print("graph request error \(err.localizedDescription)")
}})
} // end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment