Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get FBSDKLoginManagerLoginResult's email and name

I am using Facebook custom login to get user's email and public profile but I only get this. Is there any else code that I have missed out? Most of the online tutorials are in Obj-C or outdated already. I am using Swift for this project.

RESULT: '<FBSDKLoginManagerLoginResult: 0x7fe6f8c1d510>' 

Here are my code for the custom button

let login = FBSDKLoginManager()
    login.logInWithReadPermissions(["email", "public_profile"]){ result, error in
        println("RESULT: '\(result)' ")

        if error != nil {
            println("error")
        }else if(result.isCancelled){
            println("result cancelled")
        }else{
            println("success")

        }
    }
like image 448
shoujo_sm Avatar asked May 05 '15 09:05

shoujo_sm


3 Answers

Use FBSDKGraphRequest to get user info.

let login = FBSDKLoginManager()
    login.logInWithReadPermissions(["email", "public_profile"]){ result, error in
        println("RESULT: '\(result)' ")

        if error != nil {
            println("error")
        }else if(result.isCancelled){
            println("result cancelled")
        }else{
            println("success Get user information.")

            var fbRequest = FBSDKGraphRequest(graphPath:"me", parameters: nil);
        fbRequest.startWithCompletionHandler { (connection : FBSDKGraphRequestConnection!, result : AnyObject!, error : NSError!) -> Void in

            if error == nil {

                println("User Info : \(result)")
            } else {

                println("Error Getting Info \(error)");

            }
        }
        }
    }
like image 144
Dheeraj Singh Avatar answered Nov 11 '22 12:11

Dheeraj Singh


Swift 3+

FBSDKGraphRequest(graphPath:"me", parameters: ["fields":"email"]).start(completionHandler: { (connection, result, error) in
    if error == nil {
      print("User Info : \(result)")
    } else {
      print("Error Getting Info \(error)");                        
    }
})
like image 3
Dasoga Avatar answered Nov 11 '22 11:11

Dasoga


Swift 4 code in Xcode 10:

func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error!) {
        if error != nil{
            //Failed Login

        } else if result.isCancelled{
            //User cancelled

        } else{
            //Successful login
            FBSDKGraphRequest(graphPath:"me", parameters: ["fields" : "email,name,picture"]).start(completionHandler: { (connection, result, error) in
                if error == nil {
                    print("User Info : \(result)")
                } else {
                    print("Error Getting Info \(error)");
                }
            })
        }
    }
like image 3
Vamshi Krishna Avatar answered Nov 11 '22 10:11

Vamshi Krishna