Home From FBSDKGraphRequest to Storyboard
Reply: 2

From FBSDKGraphRequest to Storyboard

Calm Published in 2017-12-04 19:02:49Z

i'm total beginner in coding, but i have a big question :)

I dont know how to get information to display on Main Storyboard form FBSDKGraphRequest.

What i should do next to get picture to Storyboard? I hope someone can help me :)

Using Swift 3, Xcode 8

Facebook login is working and code is:

    func getFBUserData(){
    if((FBSDKAccessToken.current()) != nil){
        FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, picture.type(large), email"]).start(completionHandler: { (connection, result, error) -> Void in
            if (error == nil){
                self.dict = result as? [String : AnyObject]
OmniB Reply to 2017-12-04 21:12:50Z

I see it as four steps -- my apologies to you if you already know some of this.

  1. Drag an UIImageView from the object library in interface builder to a view in your storyboard.

  2. Connect the UIImageView to your code as an IBOutlet and name it. Do this by control dragging from your new UIImageView to your code, and then in the popup specify an outlet and name the UIImageView. In my case I named it 'facebookPicture'

  3. Get the URL for the image from the FaceBook result. The following sample code drills down into the result dictionary step by step. There are many ways to shorten this.

    @IBOutlet var facebookPicture: UIImageView!
    func getFBUserID(){
     if((FBSDKAccessToken.current()) != nil) {
        FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, picture.type(large), email"]).start(
            completionHandler: {
                [weak self] (connection, result, error) -> Void in
                guard let strongSelf = self else { return }
                if let error = error {
                    print("Failed to download FB user with error:. \(error)")
                if (error == nil) {
                    let resultDictSwift = result as! Dictionary<String, Any>
                    if let picture = resultDictSwift["picture"] as? Dictionary<String, Any> {
                        if let pictureData = picture["data"] as? Dictionary<String, Any> {
                            if let pictureURL = NSURL(string: (pictureData["url"] as? String)! ) {
                                strongSelf.downloadImage(url: pictureURL as URL)
                    let FBID = resultDictSwift["id"]
                    strongSelf.facebookID = FBID as! String?
                    print("User FB id: \(strongSelf.facebookID!)")
  4. Download the image from the URL location. The following does this asynchronously and uses a previous stack overflow answer here When the download is complete, it sets the result of the download to be the image in your UIImageView

    func getDataFromUrl(url: URL, completion: @escaping (Data?, URLResponse?, Error?) -> ()) {
    URLSession.shared.dataTask(with: url) { data, response, error in
        completion(data, response, error)
    func downloadImage(url: URL) {
        print("Download Started")
        getDataFromUrl(url: url) { data, response, error in
        guard let data = data, error == nil else { return }
        print(response?.suggestedFilename ?? url.lastPathComponent)
        print("Download Finished")
        DispatchQueue.main.async() {
            self.facebookPicture.image = UIImage(data: data)

I hope this helps!

Rob-4608 Reply to 2017-12-05 13:26:04Z

In Swift 3

@IBAction func btnFacebookTapped(_ sender: UIButton)

        let loginManager = FBSDKLoginManager()
        loginManager.logIn(withReadPermissions: ["user_about_me", "email" , "user_birthday","user_hometown"], from: self) { (loginResult, error) in
            if error != nil
                self.showalert(strMessage: (error?.localizedDescription)!)

                if loginResult?.grantedPermissions == nil
                    self.showalert(strMessage: "Login Permissions not granted")

                if (loginResult?.grantedPermissions.contains("email"))!


    func getFBUserData()
        FBSDKGraphRequest.init(graphPath: "me?fields=id,name,email,first_name,last_name,cover,picture.type(large),gender,birthday,hometown", parameters: nil).start(completionHandler: { (connection , result , error ) in

            if(error == nil){

                DispatchQueue.main.async {
                    let dictionary = result as! NSDictionary

                    print("Name : \(dictionary.value(forKey: "name")!)")
                    print("FB ID : \(dictionary.value(forKey: "id")!)")


                self.showalert(strMessage: "Somthig Went Wrong..!")

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.303637 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO