Moaz Khan
Moaz Khan

Reputation: 1352

Swift 4: Unable to get User Profile image from Facebook

I am using firebase Facebook auth. User is able to successfully login I am able to get user profile image from firebase after Facebook login but I need customized image for a better quality. I am unable to get user profile image using this code which uses UserProfile class. Although similar code for android works fine.

My Custom Facebook Button Code. Written in class SignUpFirst.

  @IBAction func facebookLogin(_ sender: AnyObject) {
    let loginManager = LoginManager()
    loginManager.logIn([ .publicProfile,.email], viewController: self) { loginResult in
        switch loginResult {
        case .failed(let error):
            print(error)
        case .cancelled:
            print("User cancelled login.")
        case .success(let grantedPermissions, let declinedPermissions, let accessToken):
            //print("Logged in! \(accessToken)")
            let credential = FacebookAuthProvider.credential(withAccessToken: accessToken.authenticationToken)

            Auth.auth().signIn(with: credential) { (user, error) in
                if let error = error {
                    //show error if failed
                    print(error.localizedDescription)
                    return
                }

                checkUser(firebaseUser: user!)
            }
        }
    }
}

Trying to get user profile image in class SignUpSecond.

UserProfile.current?.imageURLWith(UserProfile.PictureAspectRatio.normal, size: CGSize(width: 10.0, height: 10.0))

But this code is returning nil here. I have also tried adding this code in success of Facebooklogin button it was still returning nil. What is the solution.

Upvotes: 0

Views: 901

Answers (1)

Codus
Codus

Reputation: 1473

After successful login, you would get token but not profile. It means UserProfile.current still nil.

  1. you must load profile. You can reference similair question
  2. use https://graph.facebook.com/{use-id}/picture?type=square&width=10&height=10 to get avatar url directly. example: https://graph.facebook.com/1410901795642099/picture?type=square&width=10&height=10

Upvotes: 1

Related Questions