Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Facebook user details with swift and parse

i need to get the Facebook user information details when i login a new user through parse. at present i am logging in the new user below. can't seem to get the user details though. I've seen some code written on objective - c. most of the functions don't work anymore

The Facebook iOS sdks i am running is v4.3.0.

@IBAction func facebookButton(sender: AnyObject) {

    PFFacebookUtils.logInInBackgroundWithReadPermissions(permissions) {
        (user: PFUser?, error: NSError?) -> Void in
        if let user = user {
            if user.isNew {
                println("User signed up and logged in through Facebook!")
            } else {
                println("User logged in through Facebook!")
            }
        } else {
            println("Uh oh. The user cancelled the Facebook login.")
        }
    }
}
like image 844
Mugunthan Balakrishnan Avatar asked Jul 06 '15 11:07

Mugunthan Balakrishnan


1 Answers

To get the user details you have to send a FBSDKGraphRequest after the login request.

This can be done inside the if let user = user {...} block.

    // Create request for user's Facebook data
    let request = FBSDKGraphRequest(graphPath:"me", parameters:nil)

    // Send request to Facebook
    request.startWithCompletionHandler {

        (connection, result, error) in

        if error != nil {
            // Some error checking here
        }
        else if let userData = result as? [String:AnyObject] {

            // Access user data
            let username = userData["name"] as? String

            // ....
        }
    }               
like image 150
freytag Avatar answered Oct 01 '22 05:10

freytag