iOS的facebookSDK获取用户完整的细节

Iam使用最后的FBSDK(使用swift)

//MARK: sign in with facebook func signInWithFacebook() { if (FBSDKAccessToken.currentAccessToken() != nil) { // User is already logged in, do work such as go to next view controller. println("already logged in ") self.returnUserData() return } var faceBookLoginManger = FBSDKLoginManager() faceBookLoginManger.logInWithReadPermissions(["public_profile", "email", "user_friends"], handler: { (result, error)-> Void in //result is FBSDKLoginManagerLoginResult if (error != nil) { println("error is \(error)") } if (result.isCancelled) { //handle cancelations } if result.grantedPermissions.contains("email") { self.returnUserData() } }) } func returnUserData() { let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: nil) graphRequest.startWithCompletionHandler({ (connection, result, error) -> Void in if ((error) != nil) { // Process error println("Error: \(error)") } else { println("the access token is \(FBSDKAccessToken.currentAccessToken().tokenString)") var accessToken = FBSDKAccessToken.currentAccessToken().tokenString var userID = result.valueForKey("id") as! NSString var facebookProfileUrl = "http://graph.facebook.com/\(userID)/picture?type=large" println("fetched user: \(result)") } 

当我打印提取的用户时,我只能得到id和名字! ,但我要求电子邮件和朋友和configuration文件的许可,有什么问题?

顺便说一句:我把这个项目从我的MacBook移动到另一个MacBook(因为我格式化了我的)它工作得很好,当我在创build该项目的MacBook,但移动项目后(使用bitbucket克隆),我得到了这个结果。

根据新的Facebook SDK,您必须通过FBSDKGraphRequest传递参数

 if((FBSDKAccessToken.currentAccessToken()) != nil){ FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, email"]).startWithCompletionHandler({ (connection, result, error) -> Void in if (error == nil){ println(result) } }) } 

文档链接: https : //developers.facebook.com/docs/facebook-login/permissions/v2.4

用户对象参考: https : //developers.facebook.com/docs/graph-api/reference/user

与公众个人资料你可以得到性别:

 public_profile (Default) Provides access to a subset of items that are part of a person's public profile. A person's public profile refers to the following properties on the user object by default: id name first_name last_name age_range link gender locale timezone updated_time verified 

我想这个代码应该可以帮助你获得所需的细节

Swift 2.x

 let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: nil) graphRequest.startWithCompletionHandler({ (connection, result, error) -> Void in if ((error) != nil) { // Process error print("Error: \(error)") } else { print("fetched user: \(result)") let userName : NSString = result.valueForKey("name") as! NSString print("User Name is: \(userName)") let userID : NSString = result.valueForKey("id") as! NSString print("User Email is: \(userID)") } }) 

斯威夫特4

Swift 4中的一个例子也展示了如何从结果中正确parsing出单个字段:

 func fetchFacebookFields() { //do login with permissions for email and public profile FBSDKLoginManager().logIn(withReadPermissions: ["email","public_profile"], from: nil) { (result, error) -> Void in //if we have an error display it and abort if let error = error { log.error(error.localizedDescription) return } //make sure we have a result, otherwise abort guard let result = result else { return } //if cancelled nothing todo if result.isCancelled { return } else { //login successfull, now request the fields we like to have in this case first name and last name FBSDKGraphRequest(graphPath: "me", parameters: ["fields" : "first_name, last_name"]).start() { (connection, result, error) in //if we have an error display it and abort if let error = error { log.error(error.localizedDescription) return } //parse the fields out of the result if let fields = result as? [String:Any], let firstName = fields["first_name"] as? String, let lastName = fields["last_name"] as? String { log.debug("firsName -> \(firsName)") log.debug("lastName -> \(lastName)") } } } } }