问题
I have a problem with Facebook api. After login and success unwrapping user token I have a problem with taking user profile: it's nil.
func updateLoginStatus() {
if let _ = AccessToken.current {
let currentUser = UserProfile.current
userName.text = currentUser?.fullName
userPictureView = UserProfile.PictureView(frame: blankUserImage.frame, profile: currentUser)
userPictureView!.clipsToBounds = true
userPictureView!.layer.cornerRadius = userPictureView!.bounds.width/2
popUpView.addSubview(userPictureView!)
isLogged = true
updateLabels()
} else {
isLogged = false
userPictureView?.removeFromSuperview()
updateLabels()
}
}
Even if I try to get profile manual by using let userProfile = UserProfile(userId: (AccessToken.current?.userId)!)
it also take nil at all parameters, but it have initialized user. Funny part is that with that UserProfile.current it have take user picture. What it can be? How I can get full user name by other methods?
回答1:
This problem happend in android too. You should load profile (listen profile change in android) after you got access token. I write a sample and just tested in swift 3, Facebook iOS SDK 4.21.0. Then you can got updatedUser.
if let _ = FBSDKAccessToken.current() {
if let currentUser = FBSDKProfile.current() {
print("current user got: \(currentUser)")
} else {
print("current user not got")
FBSDKProfile.loadCurrentProfile(completion: {
profile, error in
if let updatedUser = profile {
print("finally got user: \(updatedUser)")
} else {
print("current user still not got")
}
})
}
}
来源:https://stackoverflow.com/questions/44978868/facebook-api-swift-current-user-is-nil