Fetching the Facebook user data

suggest change

After the user signs in to Facebook in your app, fetch the data you requested with FBButton.readPermissions. This sample uses the older Facebook SDK and Swift 2 (currentAccessToken(), startWithCompletionHandler, AnyObject, and NSError).

Swift:

enum FacebookParametersField : String
{
    case FIELDS_KEY = "fields"
    case FIELDS_VALUE = "id, email, picture, first_name, last_name"
}


if FBSDKAccessToken.currentAccessToken() != nil
{
//  Getting user facebook data
FBSDKGraphRequest(graphPath: "me", 
                 parameters: [FacebookParametersField.FIELDS_KEY.rawValue : FacebookParametersField.FIELDS_VALUE.rawValue])
.startWithCompletionHandler({ (graphConnection : FBSDKGraphRequestConnection!, result : AnyObject!, error : NSError!) -> Void in
    
        if error == nil
        {
            print("Facebook Graph phase")

            let email = result["email"]
            let facebookToken = FBSDKAccessToken.currentAccessToken().tokenString
            let userFacebookId = result["id"]
            let firstName = result["first_name"]
            let lastName = result["last_name"]
            
            if let result = result as? Dictionary<String, AnyObject>
            {
                if let picture = result["picture"] as? Dictionary<String,AnyObject>
                {
                    if let data = picture["data"] as? Dictionary <String,AnyObject>
                    {
                        if let url = data["url"] as? String
                        {
                            //  Profile picture URL
                            let profilePictureURL = url
                        }
                    }
                }
            }
        }
    })
}

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents