如何从Facebook获取用户电子邮件ID

时间:2018-08-17 10:00:21

标签: swift facebook-ios-sdk

为什么我无法获得Facebook的用户邮件ID?我在下面附加了我的代码。我对使用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 个答案:

答案 0 :(得分:0)

  

facebook按钮单击

 @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)
            }

        }
    }
  

获取数据

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)
        }
    }
}

答案 1 :(得分:0)

好像email的值是nil。在分配值之前,先进行nil检查。

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

希望这会有所帮助。