Skip to content

Instantly share code, notes, and snippets.

@makthrow
Created March 1, 2016 22:44
Show Gist options
  • Save makthrow/586da4704100f7945281 to your computer and use it in GitHub Desktop.
Save makthrow/586da4704100f7945281 to your computer and use it in GitHub Desktop.
FBSDK get facebook usr info function in swift
func getFBUserInfo() {
let facebookRequest = FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "name, gender, picture, birthday, first_name, last_name"])
facebookRequest.startWithCompletionHandler { (connection: FBSDKGraphRequestConnection!, result, error) -> Void in
if error == nil {
print ("result: \(result)")
var r = result as! NSDictionary
let firstName = r["first_name"]
let gender = r["gender"]
var dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "MM/dd/yyyy"
let birthday = dateFormatter.dateFromString(r["birthday"]! as! String)
let facebookID = r["id"]!
print ("facebookID: \(facebookID)")
let urlString = "https://graph.facebook.com/\(facebookID)/picture?type=large"
// get the URL of a larger picture
let largerPicURL = NSURL(string: urlString)
print (largerPicURL)
let request = NSURLRequest(URL: largerPicURL!)
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue(), completionHandler: {
response, data, error in
// TODO: save largerPic to DB
})
}
else { // facebookRequest.startWithCompletionHandler
print (error)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment