为 Facebook SDK 将图 API 从 Swift 3 转换为 Swift 5

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

我正在开发一个允许用户使用 Facebook 登录的应用程序。我的代码片段使用的是 Swift 3,但我无法在线找到转换器。 Swift3代码如下:

在Swift3的例子中,Xcode建议:

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

然后程序员输入(这是整个函数):

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

当我开始打字时:

request.start

这给了我这行代码:

request.start(completionHandler: GraphRequestBlock?)

如何将它从 Swift 3 转换为 Swift 5?

评论后更新

我的“HomeAfterLogInViewController.swift”文件如下:

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

此代码有效,但我还需要一个步骤 - 在屏幕截图中进行了解释:

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

希望对您有所帮助!

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

试试下面的代码来获取用户的信息。

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