0

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]
                print(result!)
                print(self.dict)
            }
        })
    }
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Calm
  • 51
  • 1
  • 2
  • 7

2 Answers2

1

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)
                            }
                        }
                    }
                    print(result!)
                    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)
        }.resume()
    }
    
    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!

OmniB
  • 51
  • 3
0

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)!)
            }
            else
            {

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

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

                        self.getFBUserData()


                }
            }
        }
    }
    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(dictionary)
                    print("Name : \(dictionary.value(forKey: "name")!)")
                    print("FB ID : \(dictionary.value(forKey: "id")!)")
}

            }else{

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

    }
Amul4608
  • 1,390
  • 14
  • 30