skip to Main Content

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

3

Answers


  1. GraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, picture.type(large), email"]).start(completionHandler: { (connection, result, error) -> Void in
                    if error == nil{
                        //everything works print the user data
                        if let userInfo = result as? [String: Any] {
                            if let email = userInfo["email"] as? String {
                                let firstName = userInfo["first_name"] as? String
                                let lastName = userInfo["last_name"] as? String
                                var profilePicUrl: String? = nil
                                if let fbUserId = userInfo["id"] as? String {
                                    profilePicUrl = "http://graph.facebook.com/(fbUserId)/picture?type=large"
                                }
                                //Do your operations here.
                            }
                        }
                    }
                })
    

    Hope that will help!

    Login or Signup to reply.
  2.         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()
        }
    
    Login or Signup to reply.
  3. Try the below code to get the information of the user.

    let params = ["fields":"email, id, name, first_name, last_name,gender"]//, user_gender, user_birthday"]
    
    let request = GraphRequest(graphPath: "me", parameters: params, accessToken: AccessToken.current, httpMethod: .GET, apiVersion: FacebookCore.GraphAPIVersion.defaultVersion)
            request.start { (response, result) in
                switch result {
                case .success(let value):
                    print(value.dictionaryValue!)
                    var parsedData = value.dictionaryValue as Dictionary<String, AnyObject>?
    
                    if let firstName = parsedData?["first_name"] {
                        print("First Name: (firstName)")
                    }
    
                    if let lastName = parsedData?["last_name"] {
                        print("Last Name: (lastName)")
                    }
    
                    if let email = parsedData?["email"] {
                        print("Email: (email as! String)")
                    }
    
                    if let id = parsedData?["id"] {
                        let faceBookId = id as! String
                        print("Facebook Id: (faceBookId)")
    
                        //you can get profile picture URL here.
                        let pictureURL = "https://graph.facebook.com/" + "(faceBookId)/" + "picture?type=large&return_ssl_resources=1"
                        print("Profile Picture URL: (pictureURL)")
                    }
    
                case .failed(let error):
                    print(error.localizedDescription)
                }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search