Iwan Pieterse
Iwan Pieterse

Reputation: 573

Facebook Graph API: Swift SDK not working: GraphRequest

I've been trying to follow Facebooks documentation exactly but can't understand why my code doesn't work, even though it is exactly as in the docs. I am already signing into Facebook on my app, but I need to get the profile data.

Here is my function.

func fetchFacebookUser() {

        let connection = GraphRequestConnection()

        connection.add(GraphRequest(graphPath: "/me")) { httpResponse, result in
            switch result {
            case .success(let response):
                print("Graph Request Succeeded: \(response)")
            case .failed(let error):
                print("Graph Request Failed: \(error)")
            }
        }
        connection.start()

    }

You'll notice it is exactly like the documentation: https://developers.facebook.com/docs/swift/graph

But I can't even build the project because I get this error in Xcode:

Contextual closure type '(GraphRequestConnection?, Any?, Error?) -> Void' expects 3 arguments, but 2 were used in closure body Insert ',<#arg#> '

It has the 'Fix' button but that just breaks the whole thing and Xcode (red warning) complains that none of the cases in the switch statement exist.

I have searched everywhere, the closest solution I got was this SO post: Facebook GraphRequest for Swift 5 and Facebook SDK 5

But that had no answers :(

I'm sure someone has called the Graph API using Swift? What am I missing here?

Upvotes: 0

Views: 1453

Answers (1)

Abhishek
Abhishek

Reputation: 329

However i implement facebook login my app which works fine.

On button click I called facebook login using LoginManager.

@IBAction func continueWithFacebook(_ sender: Any) {
    let fbLoginManager : LoginManager = LoginManager()
    fbLoginManager.logIn(permissions: ["email"], from: self) { (result, error) -> Void in
        if (error == nil){
            let fbloginresult : LoginManagerLoginResult = result!
            if (result?.isCancelled)!{
                return
            }
            if(fbloginresult.grantedPermissions.contains("email")) {
                self.getFBUserData()
            }
        }
    }
}

After that I get the data of user using GraphAP

func getFBUserData() {
    if((AccessToken.current) != nil){
        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){
                guard let userDict = result as? [String:Any] else {
                    return
                }
                if let picture = userDict["picture"] as? [String:Any] ,
                    let imgData = picture["data"] as? [String:Any] ,
                    let imgUrl = imgData["url"] as? String {
                    self.uURLString = imgUrl
                    /*let url = URL(string: imgUrl)*/
                    print(imgData)
                }
                if let n = userDict["name"] as? String {
                    self.name = n
                }
                if let e = userDict["email"] as? String {
                    self.email = e
                }
                self.socialLogIn()
             }
        })
    }
}

Add in your pod file - pod 'FBSDKCoreKit' and pod 'FBSDKLoginKit'

Try using like this you will get your derided result. Happy Coding

Upvotes: 1

Related Questions