facebook朋友列表

时间:2011-02-21 15:24:57

标签: facebook fbjs

如何使用新的facebook api获取我朋友朋友列表的列表。我使用以下代码获得了好友列表,

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

但是如果我在同一个函数中传递了他们的用户ID,我就无法得到朋友列表。

2 个答案:

答案 0 :(得分:2)

你不能,这是他们的私人数据,所以它在他们的控制之下。如果他们希望应用程序可以访问它,他们就会授予权限,并且可以代表他们这样做。

答案 1 :(得分:0)

facebook朋友列表只有可标记的朋友列表获取。

Facebook无法访问完整的个人资料朋友列表,只显示您的个人资料中的总朋友列表编号。

pod和plist设置所有其他应该是你的Facebook开发人员步骤适用

所以这里我的代码在swift 3.0可标记好友列表中:

//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!
}