Facebook friends list api for swift ios
Asked Answered
T

7

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?

Tamtam answered 3/4, 2015 at 8:2 Comment(2)
Please show some related code.Poikilothermic
Here your app statue is in Live and submit your app for review with required permissions. See my answer #23417856Ain
K
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 */
        }
    }
Kanya answered 2/12, 2015 at 12:48 Comment(4)
graphPath: "me/friends fetching only count of friendsGleiwitz
@Himanshu Mahajan, I tried your two codes but I'm not getting friends list. This is my response is : success(response: FacebookCore.GraphResponse(rawResponse: Optional({ data = ( ); }))) . When i use "me/friends" I'm getting only count.Ain
@Himanshu Mahajan Ae you got the list?, if yes. Please help me... Thank you.Ain
Does this GraphAPI return the email id of friends?Viand
G
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!

Gleiwitz answered 23/1, 2017 at 7:11 Comment(2)
This edge was deprecated on April 4th, 2018 and now returns an empty data set.Hydromancy
@ Alex Shvets, Here your app statue is in Live and submit your app for review with required permissions. See my answer #23417856Ain
L
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

Langlauf answered 3/4, 2015 at 12:32 Comment(2)
Is there a way to see "all friends" including those who have not installed the same app?Armoury
@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.Langlauf
S
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)");
    }
}
Speculator answered 6/6, 2015 at 22:20 Comment(1)
I need all friends but taggable_friends return a extrange id different to facebookIdCierracig
H
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 */
                          }];
Hematology answered 3/4, 2015 at 9:20 Comment(0)
P
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))")
        })
}
Pendulum answered 29/12, 2017 at 11:44 Comment(1)
I'm getting error : Use of unresolved identifier 'FBSDKAccessToken', Use of unresolved identifier 'FBSDKGraphRequest' . I imported import FacebookLogin import FacebookCoreAin
H
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

Hysterical answered 3/4, 2015 at 8:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.