Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS facebookSDK get user full details

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 .

like image 722
user3703910 Avatar asked Jul 13 '15 12:07

user3703910


People also ask

What is Fbsdk?

The Facebook SDK is a set of software components that developers can include in their mobile app to understand how people use the app, run optimized marketing campaigns and enable Facebook login and social sharing. This course helps you understand the purpose of the Facebook SDK and App Events for Android and iOS.

What is Facebook_client_token?

An access token is an opaque string that identifies a user, app, or Page and can be used by the app to make graph API calls. When someone connects with an app using Facebook Login and approves the request for permissions, the app obtains an access token that provides temporary, secure access to Facebook APIs.


2 Answers

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
like image 197
Ashish Kakkad Avatar answered Oct 18 '22 17:10

Ashish Kakkad


Swift 4

An example in Swift 4 that also shows how to correctly parse out the individual fields from the result:

func fetchFacebookFields() {
    //do login with permissions for email and public profile
    FBSDKLoginManager().logIn(withReadPermissions: ["email","public_profile"], from: nil) {
        (result, error) -> Void in
        //if we have an error display it and abort
        if let error = error {
            log.error(error.localizedDescription)
            return
        }
        //make sure we have a result, otherwise abort
        guard let result = result else { return }
        //if cancelled nothing todo
        if result.isCancelled { return }
        else {
            //login successfull, now request the fields we like to have in this case first name and last name
            FBSDKGraphRequest(graphPath: "me", parameters: ["fields" : "first_name, last_name"]).start() {
                (connection, result, error) in
                //if we have an error display it and abort
                if let error = error {
                    log.error(error.localizedDescription)
                    return
                }
                //parse the fields out of the result
                if
                    let fields = result as? [String:Any],
                    let firstName = fields["first_name"] as? String,
                    let lastName = fields["last_name"] as? String
                {
                    log.debug("firstName -> \(firstName)")
                    log.debug("lastName -> \(lastName)")
                }
            }
        }
    }
}
like image 28
HixField Avatar answered Oct 18 '22 16:10

HixField