如何获取我的Facebook朋友的个人资料图片,姓名和性别

时间:2018-11-16 17:38:20

标签: facebook facebook-graph-api

您好,我正在使用一个IOS应用程序,该应用程序登录Facebook并获取您所有Facebook朋友的个人资料图片,姓名和性别,这基本上像火种,但基于Facebook朋友 我的问题是如何让我的朋友的个人资料图片名称和性别

1 个答案:

答案 0 :(得分:-1)

您需要向FBSDKLogin Manager询问电子邮件和profile_picture的读取权限

FBSDKLoginManager()。logIn(withReadPermissions:[“ email”,“ public_profile”]

也在GraphRequest中询问您需要的字段

FBSDKGraphRequest(graphPath:“ / me”,参数:[“ fields”:“ id,名称,名字,姓氏,性别,电子邮件,生日,图片”])

请参阅以下代码以供参考

btn_Facebook.addTarget(self, action: #selector(handleCustomFBLogin), for: .touchUpInside)

///FACEBOOK LOGIN
func handleCustomFBLogin(sender:UIButton!){
    FBSDKLoginManager().logIn(withReadPermissions: ["email", "public_profile"], from: self) { (result, err) in
        if(err != nil){
            print("Custom FB Login Failed")
            return
        }
        //print(result?.token.tokenString)
        self.showEmailAddress()
    }
}

func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error!){
    if(error != nil){
        print(error)
        return
    }
    print("Successfully Logged in using facebook")
    showEmailAddress()
}

func showEmailAddress(){
    let accesstoken = FBSDKAccessToken.current();
    guard let accessTokenString = accesstoken?.tokenString else {return}

    FBSDKGraphRequest(graphPath: "/me", parameters: ["fields" : "id, name, first_name, last_name, gender, email, birthday, picture"]).start { (connection, result, err) in
        if(err != nil){
            print("Failed to start GraphRequest", err ?? "")
            return
        }
        print(result ?? "")
        if(result != nil){
            self.sendDetailsForFacebookLogin(result: result as! NSDictionary)
        }else{
            MyCustomAlert.sharedInstance.ShowAlert(vc: self, myTitle: StringClass.sharedInstance.lcStr_loginfailedcaps, myMessage: StringClass.sharedInstance.lcStr_plsTryAgain)
        }

    }

}

func loginButtonDidLogOut(_ loginButton: FBSDKLoginButton!){
    print("Logged out of Facebook")
}

func sendDetailsForFacebookLogin(result:NSDictionary){
    //print(result)

    var theemail:String = ""
    var thefname:String = ""
    var thelname:String = ""
    var thedob:String = ""
    var thecity:String = ""
    var thereunder:String = “”

    if result["email"] != nil {
        theemail = (result.value(forKey: "email") as? String)!
    }
    if result["first_name"] != nil {
        thefname = (result.value(forKey: "first_name") as? String)!
    }
    if result["last_name"] != nil {
        thelname = (result.value(forKey: "last_name") as? String)!
    }
    if result["birthday"] != nil {
        thedob = (result.value(forKey: "birthday") as? String)!
    }
    if result["city"] != nil {
        thecity = (result.value(forKey: "city") as? String)!
    }
    if result[“gender”] != nil {
        thegender = (result.value(forKey: “gender”) as? String)!
    }

    let picDict:NSDictionary = result.value(forKey: "picture") as! NSDictionary
    let dataDict:NSDictionary = picDict.value(forKey: "data") as! NSDictionary
    let theimg:String = dataDict.value(forKey: "url") as! String



}