Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facebook friends list api for swift ios

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?

like image 493
Uma Maheshwaran Avatar asked Apr 03 '15 08:04

Uma Maheshwaran


2 Answers

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 */
        }
    }
like image 191
Himanshu Mahajan Avatar answered Nov 18 '22 06:11

Himanshu Mahajan


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!

like image 10
Maishi Wadhwani Avatar answered Nov 18 '22 05:11

Maishi Wadhwani