skip to Main Content

Why can’t I get user mail id for Facebook? I have attached my code below. I have doubt in login and registration using API

func getFacebookUserInfo() {
    if(FBSDKAccessToken.current() != nil)
    {
        //print permissions, such as public_profile
        print(FBSDKAccessToken.current().permissions)
        let graphRequest = FBSDKGraphRequest(graphPath: "me", parameters: ["fields" : "id, first_name, last_name, email"])
        let connection = FBSDKGraphRequestConnection()

        connection.add(graphRequest, completionHandler: { (connection, result, error) -> Void in

            let data = result as! [String : AnyObject]

            self.firstnameLabel.text = data["first_name"] as? String
            self.lastnameLabel.text = data["last_name"] as? String
            guard let result = result as? NSDictionary else{ return }
            let email = result["email"] as? String
            self.googleemail = email!
            let FBid = data["id"] as? String
            let url = NSURL(string: "https://graph.facebook.com/(FBid)/picture?type=large&return_ssl_resources=1")
            self.imageView.image = UIImage(data: NSData(contentsOf: url! as URL)! as Data)
            self.validateUserData()
        })
        connection.start()
    }
}

enter image description here

2

Answers


  1. facebook button click

     @IBAction func btnFacebookClick(_ sender: Any)
        {
            //        GeneralClass.startProgress()
            loginManager.logIn(readPermissions: [ ReadPermission.publicProfile,ReadPermission.email], viewController: self) { (loginResult) in
                switch loginResult {
                case .failed(let error):
                    self.loginManager.logOut()
                    print(error)
                case .cancelled:
    
                    self.loginManager.logOut()
                    print("User cancelled login.")
    
                case .success(let grantedPermissions, let declinedPermissions, let accessToken):
    
                    print(grantedPermissions)
                    print(declinedPermissions)
                    print("Logged in!")
    
    
                    self.getFBUserData(accessToken: accessToken)
                }
    
            }
        }
    

    get data

    func getFBUserData(accessToken: AccessToken)
    {
    
        let request = GraphRequest(graphPath: "me",
                                   parameters: ["fields" : "first_name,middle_name,last_name,name,link,email,id,picture" as AnyObject],
                                   accessToken:accessToken,
                                   httpMethod: .GET)
        request.start { (httpResponse, result) in
    
            switch result {
            case .success(let response):
    
                let responseDictionary = response.dictionaryValue
                print("USER DATA:---(responseDictionary! as NSDictionary))")
                let dicData = responseDictionary! as NSDictionary
    
                if ((dicData["email"]) != nil) {
                    let email = dicData["email"] as! String
    
                    let user_firstName = dicData["first_name"] as! String
    
                    let imageUrl = (((dicData["picture"] as! NSDictionary)[KEY_DATA] as! NSDictionary)["url"] as! String)
    
    
    
    
                }
    
    
            case .failed(let error):
                self.loginManager.logOut()
                print(error)
            }
        }
    }
    
    Login or Signup to reply.
  2. Seems like the email value is nil. Set nil checking before assigning the value.

    if let email = result["email"] as? String {
        self.googleemail = email!
    }
    

    Hope this will help.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search