I’m working on migrating an iOS app with Facebook login functionality from Swift 3 to Swift 5. The original code works fine in Swift 3, but I’m having trouble updating the Graph API calls to work with the newer Swift version.
Here’s my current Swift 3 implementation:
func fetchUserProfile(completion: @escaping (_ : [String: Any]?, _ : Error?) -> Void) {
let graphRequest = GraphRequest(graphPath: "me", parameters: ["fields" : "id,email,picture"])
graphRequest.start { response, result in
switch result {
case .failed(let error):
completion(nil, error)
case .success (let graphResponse):
completion(graphResponse.dictionaryValue, nil)
}
}
}
When I try to use the same approach in Swift 5, the autocomplete suggests different method signatures. Instead of the completion handler I used before, it now shows:
graphRequest.start(completionHandler: GraphRequestBlock?)
I also tried using GraphRequestConnection approach:
func retrieveFacebookUserData() {
let profileRequest = GraphRequest.init(graphPath: "me", parameters: ["fields" : "id,name,email,picture.type(large)"])
let requestConnection = GraphRequestConnection()
requestConnection.add(profileRequest, completionHandler: { (connection, response, error) in
if let userData: [String : Any] = response as? [String : Any] {
DispatchQueue.main.async {
if let imageData: [String : Any] = userData["picture"] as? [String : Any] {
if let profileImage : [String: Any] = imageData["data"] as? [String: Any] {
print(profileImage)
print(userData["email"]!)
}
}
}
}
})
requestConnection.start()
}
This second approach works, but I need help understanding the proper way to handle the response data and extract the user information correctly in Swift 5. What’s the recommended approach for Facebook Graph API calls in the latest Swift version?