1

I am working on iOS app and I need to get the users friend list from facebook and also the profile picture. However I can Sign in without a problem but still need this two pieces of information.. This is my code:

@IBAction func loginFBAction(_ sender: UIButton) {
    let fbLoginManager:FBSDKLoginManager = FBSDKLoginManager()
    fbLoginManager.logIn(withReadPermissions: ["email"], from: self) { (result, error) in
        if error != nil {
            let fbLoginresult:FBSDKLoginManagerLoginResult = result!
            if(fbLoginresult.grantedPermissions.contains("email"))
            {
                print(fbLoginresult)
                self.getFBUserData()
                fbLoginManager.logOut()
            }
            print("Process error")
        }
        else if (result?.isCancelled)! {
            print("Cancelled")
        }
        else{
            print("Logged in")

 func getFBUserData() {
    if((FBSDKAccessToken.current()) != nil){
        FBSDKGraphRequest(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
                print(result as Any)
            }})
    }
}
  • you do not authorize with the correct permissions(s) for that, and you don´t ask for the user friends. btw, you do know that you can only get friends who authorized your app too, right? – luschn Nov 28 '18 at 08:42

1 Answers1

0

Your code doesn't work because you call getFBUserData at wrong place.

In fbLoginManager.logIn you check if error is not nil - it's mean if Facebook SDK return error during login. And in your code you want to fetch user data if you have an error. So because you do not have an error and user is successfully log in this code do not execute.

Place this call after print("Logged in") and it should work.

  • Thanks!! I forgot this when i paste it!! I have this exact call u mention. The problem is that, besides the email, I need to get the friends list from the user! So far i tried to read the Graph API for iOS but i could not find something that helps me in this case... – Gustavo Gobetti Bertocco Nov 28 '18 at 19:22
  • You should add **"user_friends"** to the array when you calling `fbLoginManager.logIn` method after email because now you are asking your user only about email. If you want to use this permission Facebook need to review your app and approve it. (https://developers.facebook.com/docs/apps/review) – Janek Kruczkowski Nov 28 '18 at 19:56
  • And remember that you can only get user friends that also use your app and login via Facebook and grant you permission. – Janek Kruczkowski Nov 28 '18 at 20:04
  • Thanks!!! Now i have a json with the number of friends!! I will need to make a button to refresh the friends list and resquest more because FB displays 25 friends per request! Also working to get the profile picture to work! Thanks a lot Janek!! – Gustavo Gobetti Bertocco Nov 28 '18 at 21:15