I\'m trying to get information through the facebook sdk but so far I\'m getting only the id and the name of the user, although I\'m sure there is an email available, as it i
A function in swift like this:
func fbsignup() {
let lgManager = FBSDKLoginManager()
lgManager.logInWithReadPermissions(["public_profile","email"], fromViewController: self,handler: { (result:FBSDKLoginManagerLoginResult!, error:NSError!) -> Void in
if error != nil{
print("facebook error")
}else if result.isCancelled {
print("Canceled")
}else{
if result.grantedPermissions.contains("email") {
if (FBSDKAccessToken.currentAccessToken() != nil) {
FBSDKGraphRequest(graphPath: "me", parameters: ["fields":"email,name"]) .startWithCompletionHandler({ (connection:FBSDKGraphRequestConnection!, result:AnyObject!, error:NSError!) -> Void in
print(result)
})
}
}
}
})
}
I have never worked with IOS, but I have a lot of experience with Facebook. My first observation is that whenever you request a Facebook permission, you must make sure that: - you let your users know why you need that permission - you respect Facebook's terms - you will surely need that permission in the near future
This list
["public_profile", "email", "user_friends", "user_about_me", "user_birthday"]
seems to be vague. You must make sure that you request for the minimal set of privileges you will surely need in the near future.
As about the request itself, you must make sure that you use a valid Facebook session. I do it like this in PHP:
$request = new \Facebook\FacebookRequest( self::$session, 'GET', '/me' );
$response = $request->execute();
// get response
$graphObject = $response->getGraphObject()->asArray();
I am sure you must have something like getGraphObject in your SDK as well.
Use these functions this will work for sure
@IBAction func fbsignup(_ sender: Any) {
let fbloginManger: FBSDKLoginManager = FBSDKLoginManager()
fbloginManger.logIn(withReadPermissions: ["email"], from:self) { (result, error) -> Void in
if(error == nil){
let fbLoginResult: FBSDKLoginManagerLoginResult = result!
if( result?.isCancelled)!{
return }
if(fbLoginResult .grantedPermissions.contains("email")){
self.getFbId()
}
} }
}
func getFbId(){
if(FBSDKAccessToken.current() != nil){
FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id,name , first_name, last_name , email"]).start(completionHandler: { (connection, result, error) in
if(error == nil){
print("result")
}
})
}
}
and connect the fbsignup(_sender: Any) function with your button
Tested in Swift 2.2
func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!) {
print("User Logged In")
if ((error) != nil)
{
// Process error
}
else if result.isCancelled {
// Handle cancellations
}
else {
// If you ask for multiple permissions at once, you
// should check if specific permissions missing
if result.grantedPermissions.contains("email")
{
// Do work
getUserData()
}
}}
func getUserData()
{
if ((FBSDKAccessToken.currentAccessToken()) != nil)
{
let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "email, name"])
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 userEmail : NSString = result.valueForKey("email") as! NSString
print("User Email is: \(userEmail)")
}
})
}
}
In my case email was not verified by the user thats why not getting it even after granting the permissions ;)
I struggled with this problem for almost a day until finding the answer by @HeTzi, which worked perfectly. The swift version of such a request is:
func referenceFBData() {
var request = FBSDKGraphRequest(graphPath:"me", parameters: ["fields":"email,first_name,gender"]);
request.startWithCompletionHandler { (connection : FBSDKGraphRequestConnection!, result : AnyObject!, error : NSError!) -> Void in
if error == nil {
println("The information requested is : \(result)")
} else {
println("Error getting information \(error)");
}
}
}
Remember that you must first request the proper read permissions to obtain data using this request.