Parse + Facebook Questions - ios

I am trying to implement Parse + Facebook
Here is what I would like to do:
User Log In with Facebook
Login Authorized
A new User created from Facebook properties (gender, age, name, etc)
Below is my code which logs in to Facebook using PFFacebookUtils. The code successfully created a User on my Parse, but I don't have those details of the User from Facebook which I want to use.
I would like to get the user's name, gender, hometown, etc on Facebook.
How do I achieve that??
var permissionArray = ["public_profile","user_friends","email","user_birthday", "user_work_history", "user_education_history", "user_hometown", "user_location", "user_likes"]; on Facebook
PFFacebookUtils.logInInBackgroundWithReadPermissions(permissionArray) { (user: PFUser?, error: NSError?) -> Void in
if user != nil {
if user!.isNew {
println("New User");
}
else {
println("Old User");
}
println(user?.username);
}
else {
println("Login Cancel")
}
}

You need to request the users "me" data, and then you can use that to populate your user object. So, in your if user!.isNew { you'll need to do something along the lines of...
let fbDetailsRequest = FBRequest.requestForMe()
fbDetailsRequest.startWithCompletionHandler { connection, result, error in
if error != nil {
// Handle the error
}
if let graph = result as? FBGraphObject {
// Now you can fill out the data on your user object
user!.setObject(graph.objectForKey("email")!, forKey: "email")
user!.setObject(graph.objectForKey("gender")!, forKey: "gender")
// Don't forget to save afterwards...
user!.saveInBackgroundWithBlock(nil)
}
}
To see the fields you can grab, check out https://developers.facebook.com/docs/graph-api/reference/user If you need anything other than "core" items, you might have to have extra permissions.

Related

Firebase unlink email/password auth from user on iOS

I'm trying to unlink email/password authentication from a user in Swift on iOS. I've read the documentation and managed to link and unlink Facebook authentication without a problem. However, after linking email/password credentials successfully, the providerData object is nil. The providerID is "Firebase" but when I pass that to the unlink code the following error is thrown:
Error Domain=FIRAuthErrorDomain Code=17016 "User was not linked to an account with the given provider." UserInfo={NSLocalizedDescription=User was not linked to an account with the given provider., error_name=ERROR_NO_SUCH_PROVIDER}
The unlink code I'm using is:
let providerId = (FIRAuth.auth()?.currentUser?.providerID)!
print("Trying to unlink:",providerId) // providerId = "Firebase"
FIRAuth.auth()?.currentUser?.unlinkFromProvider(providerId) { user, error in
if let error = error {
print("Unlink error:", error)
} else {
// Provider unlinked from account successfully
print("Unlinked...user.uid:", user!.uid, "Anonymous?:", user!.anonymous)
}
}
Reading the docs and having got it working for Facebook, I expected the providerData array to be populated with something after email authentication. So is my linking code wrong (it doesn't throw an error and appears to work fine)?
My linking code:
let credential = FIREmailPasswordAuthProvider.credentialWithEmail(email, password: password)
FIRAuth.auth()?.currentUser!.linkWithCredential(credential) { (user, error) in
if user != nil && error == nil {
// Success
self.success?(user: user!)
dispatch_async(dispatch_get_main_queue(), {
self.dismissViewControllerAnimated(true, completion: nil)
if type == "new" {
print("New user logged in...")
}
if type == "existing" {
print("Existing user logged in...")
}
})
} else {
print("Login error:",error)
self.showOKAlertWithTitle("Login Error", message: error!.localizedDescription)
}
}
Any pointers of how I can modify my approach would be great.
To get the profile information retrieved from the sign-in providers linked to a user, use the providerData property.
if let user = FIRAuth.auth()?.currentUser {
for profile in user.providerData {
// Id of the provider (ex: facebook.com)
let providerID = profile.providerID
}
} else {
// No user is signed in.
}
Calling FIRAuth.auth()?.currentUser?.providerID will result to "firebase".

PFUser currentUser returns nil after Facebook Login with Parse SDK

I'm currently developing an iOS App that allows login via Facebook/Parse. The login flow goes like this:
login button is touched and Facebook login window opens;
user logs in and gives permission on Facebook;
app retrieves Facebook accessToken information;
app performs [PFFacebookUtils logInInBackGroundWithAccessToken:(accessToken) ^block{}] to make the Parse login authentication;
app goes to a view and tries to perform a query to fetch data associated with that user from Parse.
The problem is: after logInInBackgroundWithAccessToken is performed, [PFUser currentUser] returns nil, and I get a "Cannot do a comparison query for type: (null)" error.
Ok! Solved it!
After digging into the code a little further I realized that the
[PFFacebookUtils logInInBackgroundWithAccessToken:(accessToken) block:^(nullable__block){}];
callback wasn't being executed for unknown reasons.
To solve this I added a check into the view's -viewDidLoad method to see if there was a valid [FBSDKAccessToken currentAccessToken] but NOT a valid [PFUser currentUser], and if so I forced the method to be executed.
If you need a more thorough explanation, don't hesitate to leave your comment! Thanks!
if ((currentUser != nil)) {
if (PFFacebookUtils.isLinkedWithUser(currentUser!)) {
proccessCurrent(currentUser!)
} else {
PFFacebookUtils.linkUserInBackground(currentUser!, withReadPermissions: self.facebookReadPermissions, block: {(success, error) in
if (success) {
self.proccessCurrent(currentUser!)
} else {
self.presentWalkThroughViewController()
}
})
}
} else {
//var currentToken: FBSDKAccessToken = FBSDKAccessToken.currentAccessToken()
if (FBSDKAccessToken.currentAccessToken() == nil) {
self.presentWalkThroughViewController()
} else {
var currentToken: FBSDKAccessToken = FBSDKAccessToken.currentAccessToken()
PFFacebookUtils.logInInBackgroundWithAccessToken(currentToken, block: {(user, error) in
if (error == nil) {
user?.pinInBackgroundWithBlock({(success, error) in
if (success) {
self.proccessCurrent(user!)
} else {
self.presentWalkThroughViewController()
}
})
} else {
self.presentWalkThroughViewController()
}
})
}
}
The methods processCurrentUser, continue to process my program execution and presentWalkThroughViewController presents the login screens. Is this a safe way to handle skipping the login screen on facebook once a user is already logged in?

XCode - Swift - Facebook Authentication with Parse

I have Facebook authentication integrated with parse using the following code:
PFFacebookUtils.logInInBackgroundWithReadPermissions(permissions) {
(user: PFUser?, error: NSError?) -> Void in
if let user = user {
if user.isNew {
let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: nil)
graphRequest.startWithCompletionHandler { (connection, result, error) -> Void in
user["email"] = result.valueForKey("email") as! String
user["firstName"] = result.valueForKey("first_name") as! String
user["lastName"] = result.valueForKey("last_name") as! String
user.saveInBackgroundWithBlock { (success: Bool, error: NSError?) -> Void in
// goto app
}
}
} else {
// goto app
}
} else {
// Canceled
}
}
So I use Parse's PFFacebookUtils to login the user, then add the extra fields email, firstName and lastName based on the user's Facebook info, then I log the user in. Just wondering if this is the right way to go about adding those additionals fields, and I was also wondering if there was a way to stop an email confirmation from going out in this scenario? I have a register via email option so I need email confirmation, but just not for Facebook login.
Yes as far as I know that is the correct way to set new field. Maybe you want to check for nil in the optionals instead of explicitly unwrapping like that?
And no I think every time you set the email on a PFUser it will send the authentication email if you have that option on in Parse.
One option to save the email without notifying them is to create your own emailId field and set it to that so that they don't get an email asking for authentication and when signing up a new user with email you can also set that field to be the same emailId (it is kind of messy cause you have the same value repeated) but its a workaround worth trying.

PFUser not unwrapped - swift

I'm beginning to learn swift with parse and i've run into this error:
"Value of optional type 'PFUser?' not unwrapped; did you mean to use '!' or '?'
I can't seem to get it to work...
PFFacebookUtils.logInWithPermissions(["public_profile",
"user_about_me", "user_birthday"], block: {
user, error in
if user == nil {
println("the user canceled fb login")
//add uialert
return
}
//new user
else if user.isNew {
println("user singed up through FB")
//get information from fb then save to parse
FBRequestConnection.startWithGraphPath("/me?fields=picture,first_name,birthday,gender", completionHandler: {
connection, result, error in
//print results
println(result)
//result dictionary about user
var r = result as NSDictionary
//prnt dictionary
println(NSDictionary)
//match parse column with what fb sends
user["firstName"] = r["first_name"]
user["gender"] = r["gender"]
//r = result, then key into using picture. Then key into url using the data
let pictureURL = ((r["picture"] as NSDictionary)["data"] as NSDictionary) ["url"] as String
Instead of using if user == nil {... you should really use
if let user = user {
// Login succeeded...
}
else {
// Login failed
}
The variable user will then be unwrapped inside the if let and you can continue using user the same way you are.
Here is explanation: What is an "unwrapped value" in Swift?
PFFacebookUtils.logInWithPermissions(["public_profile",
"user_about_me", "user_birthday"], block: {
user, error in
if user == nil {
println("the user canceled fb login")
//add uialert
return
}
//new user
else if user!.isNew {
println("user singed up through FB")
//get information from fb then save to parse
FBRequestConnection.startWithGraphPath("/me?fields=picture,first_name,birthday,gender", completionHandler: {
connection, result, error in
//print results
println(result)
//result dictionary about user
var r = result as NSDictionary
//prnt dictionary
println(NSDictionary)
//match parse column with what fb sends
user["firstName"] = r["first_name"]
user["gender"] = r["gender"]
//r = result, then key into using picture. Then key into url using the data
let pictureURL = ((r["picture"] as NSDictionary)["data"] as NSDictionary) ["url"] as String

Using Swift, Parse and the iOS FacebookSDK, how do I obtain user's name and email at login?

So I've been banging my head on this for a couple days now, and I'm hoping someone here could shed some light on this for me!
So I'm using Parse in my Swift project, and am looking to leverage the Facebook integration to make logging in and signing up a user pretty effortless.
I have managed to get as far as logging in a user, but the part where I'm stuck is that I don't know how to access the data that I requested with permissions.
In my AppDelegate Swift file, I have this block of code..
// INITIALISE FACEBOOK
let permissions = ["email", "public_profile"]
PFFacebookUtils.initializeFacebook()
PFFacebookUtils.logInWithPermissions(permissions, {
(user: PFUser!, error: NSError!) -> Void in
if user == nil {
if (error == nil) {
println("User cancelled Facebook login")
} else {
println("FB Login Error \n(error)")
}
} else if user.isNew {
println("User has signed in through Facebook!")
} else {
println("User logged in through Facebook!")
}
})
Everything is all well and dandy, and I get the "User has signed in through Facebook!" message.
According to the Parse documentation, this then creates a new User object in my database.
In my database, I see that there is indeed a new user, but the only fields that are populated are..
ObjectID - random string characters
username - random string characters
authData - a facebook type ID.
Not the username or email.
From what I have gathered, I need to now further leverage the FacebookSDK and GraphUser, but I really dont know exactly how.. or at least not in Swift.
The Parse documentation says
"The Facebook iOS SDK provides a number of helper classes for interacting with Facebook's API. Generally, you will use the FBRequest class to interact with Facebook on behalf of your logged-in user. You can read more about the Facebook SDK here.
Our library manages the user's FBSession object for you. You can simply call [PFFacebookUtils session] to access the session instance, which can then be passed to FBRequests."
But I really dont know what to type or where to type it. :(
It feels like I'm really close with this, but I'm just hitting blanks..
If someone could be kind enough to shed some light into this (how to access the details I requested permission for, i.e. full name and email) I would be incredibly grateful!
Thank you.. :)
You may try this (for Swift):
// Obtain User Data via Facebook Login
func returnUserData()
{
let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: ["fields":"name, email"])
graphRequest.startWithCompletionHandler({ (connection, result, error) -> Void in
if ((error) != nil)
{
// Process error
}
else
{
let userName: String = result.valueForKey("name") as! String
let Email: String = result.valueForKey("email") as! String
println(userName)
println(Email)
}
})
}
According to this: https://parse.com/questions/how-can-i-get-the-facebook-id-of-a-user-logged-in-with-facebook
You get the Facebook ID with this Objective-C
// After logging in with Facebook
[FBRequestConnection
startForMeWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (!error) {
NSString *facebookId = [result objectForKey:#"id"];
}
}];
Which is this in Swift (not tested)
FBRequestConnection.startForMeWithCompletionHandler { connection, result, error in
if (!error) {
let facebookId = result["id"]
// use facebookID
}
}

Resources