ios - 适用于 iOS 的 Facebook SDK v4.0 - FBSDKProfile currentProfile 未设置

标签 ios swift facebook-sdk-4.0

我刚刚为我的 iOS 应用程序将我的 Facebook SDK“升级”到 4.0。

我的登录工作正常,但是,根据文档,我现在应该使用 FBSDKProfile.currentProfile() 来访问配置文件信息。

但是,这个值似乎总是nil,即使正在显示我的个人资料图片(通过FBSDKProfilePictureView)和我的FBSDKAccessToken.currentAccessToken() 不是 nil

这是我的登录ViewController:

import UIKit

class LoginViewController: UIViewController, FBSDKLoginButtonDelegate {

    @IBOutlet weak var fbLoginButton: FBSDKLoginButton!
    @IBOutlet weak var fbProfilePicture: FBSDKProfilePictureView! //displays a picture

    override func viewDidLoad() {
        super.viewDidLoad()
        self.fbLoginButton.delegate = self
        FBSDKProfile.enableUpdatesOnAccessTokenChange(true)
        if FBSDKAccessToken.currentAccessToken() != nil {
            println("\(FBSDKAccessToken.currentAccessToken().userID)") //works
        }
        self.fbLoginButton.readPermissions = ["public_profile", "email", "user_friends"]
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!) {
        println(FBSDKProfile.currentProfile()) //is nil
    }

    func loginButtonDidLogOut(loginButton: FBSDKLoginButton!) {
    }
}

有什么想法吗?

最佳答案

不要忘记FBSDKProfile.enableUpdatesOnAccessTokenChange(true)!!我为此苦苦挣扎了一段时间,直到我在 docs 中发现它.然后订阅 FBSDKProfileDidChangeNotification 上的通知应该适用于 FBSDKLoginButton。

完整示例...我通过 Interface Builder 连接 loginButton

class LoginController: UIViewController, FBSDKLoginButtonDelegate {

@IBOutlet var loginButton: FBSDKLoginButton!

override func viewDidLoad() {
    super.viewDidLoad()
    loginButton.delegate = self;
    FBSDKProfile.enableUpdatesOnAccessTokenChange(true)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "onProfileUpdated:", name:FBSDKProfileDidChangeNotification, object: nil)

}

func onProfileUpdated(notification: NSNotification)
{
}

func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!){

}

func loginButtonDidLogOut(loginButton: FBSDKLoginButton!) {

}

关于ios - 适用于 iOS 的 Facebook SDK v4.0 - FBSDKProfile currentProfile 未设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29292371/

相关文章:

swift - 当调用此 Collection View 的 ReloadData 时,附加到 UICollectionView 中的单元格的 Popover 移动

ios - 从另一个 ViewController 中移除 subview

android - Facebook android 应用程序错误 : Invalid key hash

ios - FBSDKSharingDelegate 回调不起作用,即使发布工作正常

ios - UITableViewCell 在展开动画期间没有剪裁到边界

ios - 将字典保存到文件并读回

ios - 从 dateFromString 到 stringFromDate

Android facebook 4.0.0 分享对话框不分享内容

ios - Apple Watch 界面的一部分可以独立滚动吗?

android - 图像资源从 native 应用程序中消失