Facebook api swift current user is nil

主宰稳场 提交于 2020-01-24 00:43:31

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!