Search code examples
swiftfacebook-ios-sdk

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.")
        }
    }
}

Solution

  • 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
    
                // ....
            }
        }