简体   繁体   中英

Graph request returns nil after login

I am using facebook ios sdk 4.1

And i am using this code to get user's info after user click button and login

func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!) {


        let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: nil)
        graphRequest.startWithCompletionHandler({ (connection, result, error) -> Void in

            if ((error) != nil)
            {
                println("Error: \(error)")
            }
            else
            {
                var fbuseremail = result.valueForKey("email") as! NSString
                var fbfirstname = result.valueForKey("first_name") as! NSString
                var fblastname = result.valueForKey("last_name") as! NSString
                var fbusername = result.valueForKey("name") as! NSString

             //....more code

But result.valueForKey("email") and others returns nil

Any idea how to fix this ?

Get user info in facebook sdk 4.x swift

@IBAction func btnFBLoginPressed(sender: AnyObject) {
    var fbLoginManager : FBSDKLoginManager = FBSDKLoginManager()
    fbLoginManager .logInWithReadPermissions(["email"], handler: { (result, error) -> Void in
        if (error == nil){
            var fbloginresult : FBSDKLoginManagerLoginResult = result
            if(fbloginresult.grantedPermissions.containsObject("email"))
            {
                self.getFBUserData()
                fbLoginManager.logOut()
            }
        }
    })
}

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

Output :

{
    email = "ashishkakkad8@gmail.com";
    "first_name" = Ashish;
    id = 910855688971343;
    "last_name" = Kakkad;
    name = "Ashish Kakkad";
    picture =     {
        data =         {
            "is_silhouette" = 0;
            url = "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-xpf1/v/t1.0-1/p200x200/10394859_900936369963275_5557870055628103117_n.jpg?oh=fefbfca1272966fc78286c36741f9ac6&oe=55C89225&__gda__=1438608579_9133f15e55b594f6ac2306d61fa6b6b3";
        };
    };
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM