iOS facebookSDK get user full details

user3703910 picture user3703910 · Jul 13, 2015 · Viewed 27.5k times · Source

Iam using the last FBSDK (using swift)

// MARK: sign in with facebook

func signInWithFacebook()
{
    if (FBSDKAccessToken.currentAccessToken() != nil)
    {
        // User is already logged in, do work such as go to next view controller.
        println("already logged in ")
        self.returnUserData()

        return
    }
    var faceBookLoginManger = FBSDKLoginManager()
    faceBookLoginManger.logInWithReadPermissions(["public_profile", "email", "user_friends"], handler: { (result, error)-> Void in
        //result is FBSDKLoginManagerLoginResult
        if (error != nil)
        {
            println("error is \(error)")
        }
        if (result.isCancelled)
        {
            //handle cancelations
        }
        if result.grantedPermissions.contains("email")
        {
            self.returnUserData()
        }
    })
}

func returnUserData()
{
    let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: nil)
    graphRequest.startWithCompletionHandler({ (connection, result, error) -> Void in

        if ((error) != nil)
        {
            // Process error
            println("Error: \(error)")
        }
        else
        {
            println("the access token is \(FBSDKAccessToken.currentAccessToken().tokenString)")

            var accessToken = FBSDKAccessToken.currentAccessToken().tokenString

            var userID = result.valueForKey("id") as! NSString
            var facebookProfileUrl = "http://graph.facebook.com/\(userID)/picture?type=large"



            println("fetched user: \(result)")


}

when I print the fetched user I get only the id and the name ! , but i requested a permission for email and friends and profile , what's wrong ???

BTW : I moved this project from my macbook to another macbook ( because I formatted mine) it worked very well when it was at the the macbook which I created the project on , but after moving the project (using bitbucket clone) I got this results .

Answer

Ashish Kakkad picture Ashish Kakkad · Jul 13, 2015

As per the new Facebook SDK, you must have to pass the parameters with the FBSDKGraphRequest

if((FBSDKAccessToken.currentAccessToken()) != nil){
    FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, email"]).startWithCompletionHandler({ (connection, result, error) -> Void in
        if (error == nil){
            println(result)
        }
    })
}

Documentations Link : https://developers.facebook.com/docs/facebook-login/permissions/v2.4

User object reference : https://developers.facebook.com/docs/graph-api/reference/user

With public profile you can get gender :

public_profile (Default)

Provides access to a subset of items that are part of a person's public profile. A person's public profile refers to the following properties on the user object by default:

id
name
first_name
last_name
age_range
link
gender
locale
timezone
updated_time
verified