I am working on iOS app and I need to get the users friend list from facebook and also the profile picture. However I can Sign in without a problem but still need this two pieces of information.. This is my code:
@IBAction func loginFBAction(_ sender: UIButton) {
let fbLoginManager:FBSDKLoginManager = FBSDKLoginManager()
fbLoginManager.logIn(withReadPermissions: ["email"], from: self) { (result, error) in
if error != nil {
let fbLoginresult:FBSDKLoginManagerLoginResult = result!
if(fbLoginresult.grantedPermissions.contains("email"))
{
print(fbLoginresult)
self.getFBUserData()
fbLoginManager.logOut()
}
print("Process error")
}
else if (result?.isCancelled)! {
print("Cancelled")
}
else{
print("Logged in")
func getFBUserData() {
if((FBSDKAccessToken.current()) != nil){
FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, picture.type(large), email"]).start(completionHandler: { (connection, result, error) -> Void in
if (error == nil){
//everything works print the user data
print(result as Any)
}})
}
}
Your code doesn't work because you call
getFBUserData
at wrong place.In
fbLoginManager.logIn
you check if error is not nil - it's mean if Facebook SDK return error during login. And in your code you want to fetch user data if you have an error. So because you do not have an error and user is successfully log in this code do not execute.Place this call after
print("Logged in")
and it should work.