iosswiftfacebookfacebook-graph-api

iOS import unresolved identifier


I'm really new to iOS and I just started to use the API-Graph of Facebook.

Now I want to do the "/me" query but I got this error:

Use of unresolved identifier 'GraphRequest'

This is my code:

import UIKit
import FBSDKCoreKit
import FBSDKLoginKit
....
func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error!) {

if ((error) != nil)
{
    // Process error
}
else if result.isCancelled {
    // Handle cancellations
}
else {  
    if result.grantedPermissions.contains("public_profile") && result.grantedPermissions.contains("user_friends") && result.grantedPermissions.contains("user_posts") && result.grantedPermissions.contains("user_photos")
    {
        //HERE IS MY QUERY

        let params = ["fields" : "email, name"]
        let graphRequest = GraphRequest(graphPath: "me", parameters: params)  <-- HERE I GOT THE ERROR
        graphRequest.start {
            (urlResponse, requestResult) in

            switch requestResult {
            case .failed(let error):
                print("error in graph request:", error)
                break
            case .success(let graphResponse):
                if let responseDictionary = graphResponse.dictionaryValue {
                    print(responseDictionary)

                    print(responseDictionary["name"])
                    print(responseDictionary["email"])
                }
            }
        }


                guard let presentedController = self.storyboard?.instantiateViewController(withIdentifier: "01") else { return }
                presentedController.modalTransitionStyle = UIModalTransitionStyle.partialCurl
                self.present(presentedController, animated: true, completion: nil)
            }
        }


        guard let presentedController = self.storyboard?.instantiateViewController(withIdentifier: "01") else { return }
        presentedController.modalTransitionStyle = UIModalTransitionStyle.partialCurl
        self.present(presentedController, animated: true, completion: nil)*/
    }else{
        //cambia stato bottone
        let alert = UIAlertController(title: "Missing Permission", message: "We need all the permission for continue", preferredStyle: UIAlertControllerStyle.alert)
        alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: nil))
        self.present(alert, animated: true, completion: nil)
    }
}
}

I did the Import and the Facebook Login work good. What I did wrong? (I'm using Swift 3)

Update 2:

error


Solution

  • GraphRequest should instead be FBSDKGraphRequest.

    let graphRequest = FBSDKGraphRequest(graphPath: "me", parameters: params)
    

    Note: In addition to the iOS SDK, there is also a Swift specific SDK. https://developers.facebook.com/docs/swift

    EDIT:

    Based on your updated question, it appears that FBSDKGraphRequest can return an optional. An optional means that you will either get an instance of FBSDKGraphRequest or nil.

    You can handle an optional in a few different ways.

    Using guard:

    guard let graphRequest = FBSDKGraphRequest(graphPath: "me", parameters: params) else {
        // Handle the fact that graphRequest is nil
    }
    
    graphRequest.start { ... } // graphRequest is guaranteed to be not nil
    

    Using if let:

    if let graphRequest = FBSDKGraphRequest(graphPath: "me", parameters: params) {
        graphRequest.start { ... } // graphRequest is guaranteed to be not
    } else {
        // Handle the fact that graphRequest is nil
    }
    

    Using ?:

    let graphRequest = FBSDKGraphRequest(graphPath: "me", parameters: params)
    
    graphRequest?.start { ... }
    

    This would silently do nothing if graphRequest is nil.

    EDIT 2:

    You appear to be calling the start method using the Facebook Swift SDK way instead of their iOS SDK way.

    FBSDKGraphRequestHandler is a typedef that is defined with the following parameters:

    So when calling start, you need to account for these parameters in the closure.

    graphRequest.start { connection, result, error in
        ...
    }
    

    or use _ for the parameters that you are not interested in.

    graphRequest.start { _, result, _ in
        ...
    }
    

    NOTE: Your switch statement will likely not work with the code above. You will need to make further changes to get it working with the parameters that are provided (connection, result and error). Again, it appears that you may be mixing Facebook's Swift SDK code and iOS SDK code.