Search code examples
swiftfacebookswift3xcode8fbsdk

FBSDKGraphRequestHandler Swift 3 error


I have tried to debug my entire app, and currently I am down to 3 errors, all the same error. I have spent hours trying to debug these last 3 errors on my own, but I haven't been successful. Of course, the 3 errors are the same, and I know once I debug one, I can debug all of them

The error is related to the Facebook SDK, specifically the FB SDK Graph Request Handler.

This is the code

    func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error?) {
            if let error = error {
                print(error.localizedDescription)
                return
            }
            else{
                let credential = FacebookAuthProvider.credential(withAccessToken: FBSDKAccessToken.current().tokenString)

                // If already anon user exists link with new user
                if Auth.auth().currentUser != nil{
                    Auth.auth().currentUser!.link(with: credential) { (user, error) in
                        // ...
                    }
                }

let req = FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "email,first_name, last_name, birthday, gender"], tokenString: FBSDKAccessToken.current().tokenString, version: nil, httpMethod: "GET")

req ? .start(completionHandler: {
            (connection, result, error: NSError!) - > Void in
            if (error == nil) {
                print("result \(result)")
                Auth.auth() ? .signIn(with: credential) {
                    (user, error) in
                    if error != nil {
                        print("Login failed. \(error)")
                    } else {
                        print("Logged in! \(user)")
                        FirebaseUtility.sharedInstance.setUser(user!)

                        let name = String.init(format: "%@ %@", result.value(forKey: "first_name") as!String, result.value(forKey: "last_name") as!String)
                        FirebaseUtility.sharedInstance.editUserValue(name, key: "name")
                        if (result.object(forKey: "gender") != nil) {
                            let gender = result.object(forKey: "gender") as!String
                            FirebaseUtility.sharedInstance.editUserValue(gender.capitalized, key: "gender")
                        }
                        if (result.object(forKey: "email") != nil) {
                            let gender = result.object(forKey: "email") as!String
                            FirebaseUtility.sharedInstance.editUserValue(gender.capitalized, key: "email")
                        }


                        if self.isSignupflow == true {
                            FirebaseUtility.sharedInstance.sendToken()

                            // this user hasn't completed his profile so show him profile page
                            let vc: SignupViewController = SignupViewController(nibName: "SignupViewController", bundle: nil)
                            self.present(vc, animated: true, completion: nil)
                        } else {
                            FirebaseUtility.sharedInstance.isFirstTimeUser {
                                (isFirstTimeUser) in

                                if isFirstTimeUser {
                                    FirebaseUtility.sharedInstance.sendToken()
                                        // this user hasn't completed his profile so show him profile page
                                    let vc: SignupViewController = SignupViewController(nibName: "SignupViewController", bundle: nil)
                                    self.present(vc, animated: true, completion: nil)
                                } else {
                                    // take him into app
                                    //                                self.loginSuccessful()
                                    let vc: RecordViewControllerNew = RecordViewControllerNew(nibName: "RecordViewControllerNew", bundle: nil)
                                    vc.isBackButtonHidden = true
                                    self.present(vc, animated: true, completion: nil)
                                }

                            }



                        }
                    }

                }
            }

The error that occurs is:

Cannot convert value of type '(, _, NSError!) ->Void' to expected argument type 'FBSDKGraphRequestHandler!'

and it occurs on this line of the code

req ? .start(completionHandler: {
                (connection, result, error: NSError!) - > Void

Any help would be appreciated, I know once this error is solved, more errors are going to be created, but that's just how coding works :)

Thank you!


Solution

  • FBSDKGraphRequestHandler having optional Error? as last argument not the NSError!, so change the completion block to req?.start(completionHandler: { (connection, result, error) in from req?.start(completionHandler: { (connection, result, error: NSError!) - > Void will reduce that error.

    Also after changing this you get new warning like.

    Expression of type 'FBSDKGraphRequestConnection?' is unused

    So simply add _ = as prefix of req?.start.

    _ = req?.start(completionHandler: { (connection, result, error) in
        //Add your code here  
    })