skip to Main Content

I’m using the graph api to return my name and email from a Facebook login. This is the code I use – name comes back fine, but email is nil:

func getUserInfo() {
    let params = ["fields" : "email, name"]
    let graphRequest = GraphRequest(graphPath: "me", parameters: params)
    graphRequest.start {
        (urlResponse, requestResult) in

        switch requestResult {
        case .failed(let error):
            print("error in graph request:", error)
            break
        case .success(let graphResponse):
            if let responseDictionary = graphResponse.dictionaryValue {
                print(responseDictionary)

                if let name = responseDictionary["name"] {
                    self.nameLabel.text = "Logged in as: n(name)"
                }

                if let email = responseDictionary["email"] {
                    self.emailLabel.text = "Your email is: n(email)"
                } else {
                    self.emailLabel.text = "Your email is: "
                }
                print(responseDictionary["name"])
                print(responseDictionary["email"])
            }
        }
    }
}

This is what prints to my console:

["id": 1653899307960705, "name": *my name removed for privacy*]
Optional(*my name removed for privacy*)
nil

I’m not sure if my code is wrong or it’s just a bug – I specify both email and name as my parameters, but as you can see only the name comes back. Anyone know why this is happening?

2

Answers


  1. Your code is absolutely correct, there few possibilities that facebook graph API not providing email:

    1. Registered user has submitted its email but not validated
    2. User has set email (visibility) as private/limited access to friends only (not public)

    Check account setting of an/your account (email is valid & validated as well as it is public)

    Login or Signup to reply.
  2. I thing you forgot to take email permission, so here I am sharing you code to take permission.

    func fbLoginInitiate()
        {
            let loginManager = FBSDKLoginManager()
            loginManager.logInWithReadPermissions(["public_profile", "email","user_friends"], handler: {(result:FBSDKLoginManagerLoginResult!, error:NSError!) -> Void in
                if (error != nil) {
                    // Process error
                    self.removeFbData()
                } else if result.isCancelled {
                    // User Cancellation
                    self.removeFbData()
                } else {
                    //Success                
                    if result.grantedPermissions.contains("email") && result.grantedPermissions.contains("public_profile") && result.grantedPermissions.contains("user_friends"){
                        //Here you need to call your function for graphRequest 
                        self.getUserInfo()
                    } else {
                        //Handle error
                    }
                }
            })
        }
    func removeFbData()
        {
            //Remove FB Data
            let fbManager = FBSDKLoginManager()
            fbManager.logOut()
            FBSDKAccessToken.setCurrentAccessToken(nil)
        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search