Swift 4:无法从Facebook获取用户个人资料图片

时间:2017-07-19 08:43:36

标签: ios swift xcode facebook

我正在使用firebase Facebook auth。用户能够成功登录我能够在Facebook登录后从firebase获取用户个人资料图片,但我需要自定义图像以获得更好的质量。 我无法使用此代码使用UserProfile类获取用户个人资料图片。虽然类似的Android代码工作正常。

我的自定义Facebook按钮代码。写在类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!)
            }
        }
    }
}

尝试在SignUpSecond类中获取用户个人资料图片。

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

但是这段代码在这里返回零。我也尝试在Facebooklogin按钮成功添加此代码,它仍然返回零。解决方案是什么。

1 个答案:

答案 0 :(得分:1)

成功登录后,您将获得令牌而非配置文件。 这意味着UserProfile.current仍为零。

  1. 您必须加载个人资料。您可以参考similair question
  2. 使用https://graph.facebook.com/{use-id}/picture?type=square&width=10&height=10直接获取头像网址。例: https://graph.facebook.com/1410901795642099/picture?type=square&width=10&height=10