facebook friend list

770 views Asked by At

How can i get the list of my friends friends list using new facebook api. i got the friend list using the below code,

$friends = $facebook->api('/uid/friends');

but i am not able to get friend list if i pass their userid in the same function..

2

There are 2 answers

0
leebriggs On BEST ANSWER

You can't, that's their private data so it's under their control. If they then want the application to have access to it, they would grant permission and it can do so on their behalf.

0
ronak patel On

Facebook friend list only fetches a taggable friend list.

Facebook does not access full profiles friend list, it shows only total friend list count in your profile.

pod and plist setting all other should you apply Facebook Developer steps.

So is here my code in Swift 3.0 taggable friend list.

appdelegate.swift

import CoreData
import FBSDKCoreKit
import FBSDKLoginKit
  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
         return FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
        // Override point for customization after application launch.
        return true
    }
    
    func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
        return FBSDKApplicationDelegate.sharedInstance().application(app, open: url, options: options)
    }

viewcontroller.swift

import UIKit
import FBSDKLoginKit
import FacebookCore
import FacebookLogin
import SDWebImage


class ViewController: UIViewController, FBSDKLoginButtonDelegate,UITableViewDelegate,UITableViewDataSource

{
    @IBOutlet var TblView: UITableView!
    @IBOutlet weak var btnFacebook: FBSDKLoginButton!

    @IBOutlet weak var ivUserProfileImage: UIImageView!

    @IBOutlet weak var lblName: UILabel!
    
     let reusableIdentifire = "cell"
    @IBOutlet var FriendListBtn: UIButton!
    
    var dict  = [AnyObject]()
    var FBNameArray = [String]()
    var FBImageArray = [String]()
    var Array = [String:AnyObject]()
    
    override func viewDidLoad()
    {
        super.viewDidLoad()
        self.TblView.delegate = self
        self.TblView.dataSource = self
        
      configureFacebook()
        
        //acess authorization 
        let fbLoginManager : FBSDKLoginManager = FBSDKLoginManager()
        fbLoginManager.logIn(withReadPermissions: ["email"], from: self) { (result, error) in
            if (error == nil){
                let fbloginresult : FBSDKLoginManagerLoginResult = result!
                if fbloginresult.grantedPermissions != nil {
                    if(fbloginresult.grantedPermissions.contains("email"))
                    {
                        self.getFBUserData()
                        fbLoginManager.logOut()
                    }
                }
            }
        }
            self.TblView.reloadData()
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        return FBNameArray.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        let cell: TableViewCell = self.TblView.dequeueReusableCell(withIdentifier: self.reusableIdentifire,for:indexPath) as! TableViewCell
        
        cell.FriendName.text = self.FBNameArray[indexPath.row]
        cell.FriendImg?.sd_setImage(with:URL(string: FBImageArray[indexPath.row] ), placeholderImage: UIImage(named: ""))

        //cell.FriendImg.image = UIImage(named: self.FBImageArray[indexPath.row])
        
        
        return cell
        
    }

    
    func configureFacebook()
    {
        btnFacebook.readPermissions = ["public_profile", "email", "user_friends"];
        btnFacebook.delegate = self
    }
    
    
    
    func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!)
    {}
    
    func loginButtonDidLogOut(_ loginButton: FBSDKLoginButton!)
    {}
    
    func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error!)
    {
        FBSDKGraphRequest.init(graphPath: "me", parameters: ["fields":"first_name, last_name, picture.type(large)"]).start { (connection, result, error) -> Void in
                       
            let strFirstName: String = ((result as AnyObject)["first_name"]) as! String
            let strLastName: String = ((result as AnyObject)["last_name"]) as! String
            let faceBookID:String = (result as AnyObject).value(forKey: "id") as! String
            let strPictureURL = String(format:"https://graph.facebook.com/%@/picture?type=large", faceBookID)
            
            self.lblName.text = "Welcome, \(strFirstName) \(strLastName)"
            self.ivUserProfileImage.image = UIImage(data: try! Data(contentsOf: URL(string: strPictureURL)!))
        }
    }
    private func loginButtonDidLogOut(loginButton: FBSDKLoginButton!)
    {
        let loginManager: FBSDKLoginManager = FBSDKLoginManager()
        ivUserProfileImage.image = nil
        lblName.text = "SignOut"

        loginManager.logOut()
        
           }
    
    func getFBUserData(){
        
        if((FBSDKAccessToken.current()) != nil){

            FBSDKGraphRequest(graphPath:"/me/taggable_friends", parameters:["fields":"user_id,first_name,last_name,name,picture.type(large)"],httpMethod: "GET")
                //["fields": "id, name, first_name, last_name, picture.type(large), email"])
                .start(completionHandler: { (connection,result, error) -> Void in
                if (error == nil)
                {
                    print("Friends are: \(result)")
                    let jsonResult = result as! Dictionary<String,AnyObject>
                    self.dict = jsonResult["data"] as! [AnyObject]
                    
                    for item in self.dict
                    {
                       // let id = item["id"] as! String
                        let name = item["name"] as! String
                        self.FBNameArray.append(name)
                        print("FBname: \(self.FBNameArray)")
                        let picture = item["picture"] as! NSDictionary
                        let parsePic = picture["data"] as! NSDictionary
                        let url = parsePic["url"] as! String
                        self.FBImageArray.append(url)
                        print("FBimage: \(self.FBImageArray)")
                        self.TblView.reloadData()
                    }
                    
                }
                    
            })
        }
    }

}

tableview.swift

import UIKit
import FBSDKLoginKit
import FacebookCore
import FacebookLogin

class TableViewController: UIViewController,UITableViewDelegate,UITableViewDataSource
{

    @IBOutlet var TblView: UITableView!
    
    let reusableIdentifire = "cell"
    
    var ImageArray = [String]()
    var NameArray = [String]()

    override func viewDidLoad()
    {
        super.viewDidLoad()
        
        self.TblView.delegate = self
        self.TblView.dataSource = self
        
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        return 1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        let cell: TableViewCell = self.TblView.dequeueReusableCell(withIdentifier: self.reusableIdentifire,for:indexPath) as! TableViewCell
        
            cell.FriendName.text = self.NameArray[indexPath.row]
            cell.FriendImg.image = UIImage(named: self.ImageArray[indexPath.row])
        return cell
    }
    
    @IBAction func BackBtn(_ sender: AnyObject)
    {
        self.navigationController?.popViewController(animated:true)
    }

}

tableviewcell.swift

import UIKit

class TableViewCell: UITableViewCell
{

    @IBOutlet var FriendImg: UIImageView!
    @IBOutlet var FriendName: UILabel!
}