【问题标题】:Facebook api swift current user is nilFacebook api swift 当前用户为零
【发布时间】:2017-07-07 20:06:41
【问题描述】:

我对 Facebook api 有疑问。登录并成功解开用户令牌后,我在获取用户个人资料时遇到问题:它是 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()
    }
}

即使我尝试使用let userProfile = UserProfile(userId: (AccessToken.current?.userId)!) 获取配置文件手册,它的所有参数也都为零,但它已经初始化了用户。有趣的是,使用 UserProfile.current 可以拍摄用户照片。它可以是什么?如何通过其他方法获取完整的用户名?

【问题讨论】:

    标签: ios swift facebook facebook-graph-api


    【解决方案1】:

    这个问题也发生在android。 获得访问令牌后,您应该加载配置文件(在 android 中监听配置文件更改)。 我编写了一个示例,刚刚在 swift 3、Facebook iOS SDK 4.21.0 中进行了测试。 然后你就可以得到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")
                }
            })
        }
    }
    

    【讨论】:

    • 谢谢。我怎么理解,在获取 AccessToken UserProfile.current 后必须自动更新,但它没有发生?
    • 文件没有写清楚。只有登录后才能获得。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-23
    • 1970-01-01
    • 2016-12-05
    • 1970-01-01
    • 2015-09-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多