12

I have tried to get the facebook friends list for an IOS app, but receiving empty response from facebook.

How to get face book friends list in swift?

Tamás Sengel
  • 55,884
  • 29
  • 169
  • 223
Uma Maheshwaran
  • 119
  • 1
  • 1
  • 8
  • 1
    Please show some related code. – phts Apr 03 '15 at 08:04
  • Here your app statue is in Live and submit your app for review with required permissions. See my answer https://stackoverflow.com/questions/23417356/facebook-graph-api-v2-0-me-friends-returns-empty-or-only-friends-who-also-u/54782732#54782732 – Naresh Feb 20 '19 at 09:28

7 Answers7

14

In Graph API v2.0 or above, calling /me/friends returns the person's friends who installed the same apps. Additionally, you must request the user_friends permission from each user. user_friends is no longer included by default in every login. Each user must grant the user_friends permission in order to appear in the response to /me/friends.

To get the list of friends who are using your app use following code:

    let params = ["fields": "id, first_name, last_name, middle_name, name, email, picture"]
    let request = FBSDKGraphRequest(graphPath: "me/friends", parameters: params)
    request.startWithCompletionHandler { (connection : FBSDKGraphRequestConnection!, result : AnyObject!, error : NSError!) -> Void in

        if error != nil {
            let errorMessage = error.localizedDescription 
            /* Handle error */
        }
        else if result.isKindOfClass(NSDictionary){
            /*  handle response */
        }
    }

If you want to access a list of non-app-using friends, then use following code:

    let params = ["fields": "id, first_name, last_name, middle_name, name, email, picture"]
    let request = FBSDKGraphRequest(graphPath: "me/taggable_friends", parameters: params)
    request.startWithCompletionHandler { (connection : FBSDKGraphRequestConnection!, result : AnyObject!, error : NSError!) -> Void in

        if error != nil {
            let errorMessage = error.localizedDescription 
        }
        else if result.isKindOfClass(NSDictionary){    
            /* Handle response */
        }
    }
Himanshu Mahajan
  • 4,779
  • 2
  • 36
  • 29
10

For fetching the friend list must allow user permission user_friends at the time of login. For swift 3.0 add below code for fetching friend list:

let params = ["fields": "id, first_name, last_name, middle_name, name, email, picture"]
    FBSDKGraphRequest(graphPath: "me/taggable_friends", parameters: params).start { (connection, result , error) -> Void in

        if error != nil {
            print(error!)
        }
        else {
            print(result!)
            //Do further work with response
        }
    }

I hope it works!

Maishi Wadhwani
  • 1,104
  • 15
  • 24
  • This edge was deprecated on April 4th, 2018 and now returns an empty data set. – Jared_M Sep 14 '18 at 13:26
  • @ Alex Shvets, Here your app statue is in Live and submit your app for review with required permissions. See my answer https://stackoverflow.com/questions/23417356/facebook-graph-api-v2-0-me-friends-returns-empty-or-only-friends-who-also-u/54782732#54782732 – Naresh Feb 20 '19 at 09:30
8

Remember only those friends are fetched who are using your app and u should have taken users read permission for user_friends while logging.

var fbRequest = FBSDKGraphRequest(graphPath:"/me/friends", parameters: nil);
            fbRequest.startWithCompletionHandler { (connection : FBSDKGraphRequestConnection!, result : AnyObject!, error : NSError!) -> Void in

                if error == nil {

                    println("Friends are : \(result)")

                } else {

                    println("Error Getting Friends \(error)");

                }
            }

Refer to : https://developers.facebook.com/docs/graph-api/reference/v2.3/user/friends#read

Dheeraj Singh
  • 5,143
  • 25
  • 33
  • Is there a way to see "all friends" including those who have not installed the same app? – Van Du Tran Jul 10 '15 at 14:39
  • @VanDuTran i don't think so that u would be able to get all the friends. From new SDK u are able to get only those friends who have installed the app. – Dheeraj Singh Jul 16 '15 at 13:02
7

Just making an union here that solved my problems:

From Dheeraj Singh, to get friends using your app:

var request = FBSDKGraphRequest(graphPath:"/me/friends", parameters: nil);

request.startWithCompletionHandler { (connection : FBSDKGraphRequestConnection!, result : AnyObject!, error : NSError!) -> Void in
    if error == nil {
        println("Friends are : \(result)")
    } else {
        println("Error Getting Friends \(error)");
    }
}

And to get all facebook friends, from Meghs Dhameliya, ported to swift:

var request = FBSDKGraphRequest(graphPath:"/me/taggable_friends", parameters: nil);

request.startWithCompletionHandler { (connection : FBSDKGraphRequestConnection!, result : AnyObject!, error : NSError!) -> Void in
    if error == nil {
        println("Friends are : \(result)")
    } else {
        println("Error Getting Friends \(error)");
    }
}
Renan Kosicki
  • 2,890
  • 2
  • 29
  • 32
3

you can get friend list using taggable_friends Graph API Reference for User Taggable Friend

  /* make the API call */
    [FBRequestConnection startWithGraphPath:@"/me/taggable_friends"
                          completionHandler:^(
                              FBRequestConnection *connection,
                              id result,
                              NSError *error
                          ) {
                              /* handle the result */
                          }];
Meghs Dhameliya
  • 2,406
  • 24
  • 29
2

Swift - 4
This will only return your facebook friends using the same app.

// First you have to check that `user_friends` has associated with your `Access Token`, If you are getting false, please allow allow this field while you login through facebook.
if FBSDKAccessToken.current().hasGranted("user_friends") {

        // Prepare your request
        let request = FBSDKGraphRequest.init(graphPath: "me/friends", parameters: params, httpMethod: "GET")

        // Make a call using request
        let _ = request?.start(completionHandler: { (connection, result, error) in
              print("Friends Result: \(String(describing: result))")
        })
}
Kiran Jasvanee
  • 6,362
  • 1
  • 36
  • 52
  • I'm getting error : Use of unresolved identifier 'FBSDKAccessToken', Use of unresolved identifier 'FBSDKGraphRequest' . I imported import FacebookLogin import FacebookCore – Naresh Feb 19 '19 at 05:27
1

With user_friends permission you can have access to the list of your friends that also use your app, so if your app is not used by any of your friend, the list will be empty. See user_friends permission reference

Andr3a88
  • 679
  • 5
  • 13