Search code examples
iosfacebook-graph-apiswift3facebook-sdk-4.0swift5

Converting Graph API from Swift 3 to Swift 5 for Facebook SDK


I am developing an app that allows the user to login using Facebook. The code snippet I have is using Swift 3 though and I can't find a converter online. The Swift 3 code is as follows:

In the example which is in Swift 3, Xcode suggests:

request.start(completion: ((HTTPURLResponse?, GraphRequestResult<GraphRequest>) -> Void)?)

And the programmer then enters (this is the entire function):

func getUserInfo(completion: @escaping (_ : [String: Any]?, _ : Error?) -> Void) {
    
    let request = GraphRequest(graphPath: "me", parameters: ["fields" : "id,email,picture"])

    request.start { response, result in
        
        switch result {
        case .failed(let error):
            completion(nil, error)
        case .success (let graphResponse):
            completion(graphResponse.dictionaryValue, nil)
        }
    }

When I start to type:

request.start

Which gives me this line of code:

request.start(completionHandler: GraphRequestBlock?)

How can I convert this from Swift 3 to Swift 5?

Update after comment

My "HomeAfterLogInViewController.swift" file is as follows:

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()
  }

And this code works but there is one more step I need - explained in the screenshot:

enter image description here


Solution

  •         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()
        }