RC07JNR
RC07JNR

Reputation: 595

Showing a Facebook User Picture using the URL (Swift 5 | Xcode 10)

I am new to Swift 5 and currently developing an app that I need the Facebook login SDK utilized. The user can log in successfully and the code returns the email and URL to the user's Profile Picture. I want the profile picture to be displayed on the storyboard, however.

My storyboard is called: "HomeAfterLogInViewController.swift" and look like this:

enter image description here

The code in this file is:

import Foundation
import FacebookCore
import FacebookLogin

class HomeAfterLogInViewController: UIViewController
{
    override func viewDidLoad()
    {
        super.viewDidLoad()

        getFacebookProfileInfo()
    }
}

func getFacebookProfileInfo()
{
    let requestMe = GraphRequest.init(graphPath: "me", parameters: ["fields" : "id,name,email,picture.type(large)"])

    let connection = GraphRequestConnection()

    connection.add(requestMe, completionHandler:{ (connectn, userresult, error) in

        if let dictData: [String : Any] = userresult as? [String : Any]
        {
            DispatchQueue.main.async
                {
                    if let pictureData: [String : Any] = dictData["picture"] as? [String : Any]
                    {
                        if let data : [String: Any] = pictureData["data"] as? [String: Any]
                        {
                            print(data)
                            print(dictData["email"]!)     
                      }
                  }
              }
          }
      })
      connection.start()
  }

I found this code online:

leturl = NSURL(data)  // holds url from facebook
let data = NSData(contentsOfURL: url)

imageURL.image = UIImage(data: data)

But this gives me this error:

enter image description here

How can I write the required code?

Upvotes: 0

Views: 1755

Answers (2)

HardikS
HardikS

Reputation: 714

Try the below code.

func getFacebookProfileInfo() {
    let requestMe = GraphRequest.init(graphPath: "me", parameters: ["fields" : "id,name,email,picture.type(large)"])

    let connection = GraphRequestConnection()

    connection.add(requestMe, completionHandler:{ (connectn, userresult, error) in

        if let dictData: [String : Any] = userresult as? [String : Any]
        {
            DispatchQueue.main.async
                {
                    if let pictureData: [String : Any] = dictData["picture"] as? [String : Any]
                    {
                        if let data : [String: Any] = pictureData["data"] as? [String: Any]
                        {
                            print(data)
                            print(dictData["email"]!)     

                            if let pictureURL = pictureData["url"] as? String { //image url of your image

                                   if let url = URL(string: pictureURL) {

                                       if let data = try? Data(contentsOf: url) { //here you get image data from url

                                          //generate image from data and assign it to your profile imageview
                                          imageView.image = UIImage(data: data) 
                                       }
                                   }
                            }
                      }
                  }
              }
          }
      })
      connection.start()
  }

I hope it will help you to get the image from the URL.

Upvotes: 3

Chetan Hedamba
Chetan Hedamba

Reputation: 291

 if let pictureData: [String : Any] = dictData["picture"] as? [String : Any]
 {
     if let data : [String: Any] = pictureData["data"] as? [String: Any]
      {
           //Picture Url 
           print(data["url"] as! String)
       }
 }

Upvotes: 0

Related Questions