即使登录,Facebook iOS SDK FBSDKAccessToken.currentAccessToken()也为零?

时间:2015-05-20 05:56:10

标签: ios facebook swift

我在使用FBSDKAccessToken时遇到了一些奇怪的行为:

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    println(FBSDKAccessToken.currentAccessToken()) //prints nil

    if (FBSDKAccessToken.currentAccessToken() != nil)
    {
        // User is already logged in, do work such as go to next view controller.
        println("this never prints")
        self.generateAPILoginDetails()
    }
    else
    {
        let loginView : FBSDKLoginButton = FBSDKLoginButton()
        self.view.addSubview(loginView)
        loginView.center = self.view.center
        loginView.readPermissions = ["public_profile", "email", "user_friends"]
        loginView.delegate = self
        FBSDKProfile.enableUpdatesOnAccessTokenChange(true)
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "onProfileUpdated:", name:FBSDKProfileDidChangeNotification, object: nil)
    }


}

我正在尝试获取令牌,但它始终为nil,即使loginView按钮显示" Log Out"消息,表明用户确实登录了Facebook。

1 个答案:

答案 0 :(得分:8)

我明白了:

您需要添加:

 NSNotificationCenter.defaultCenter().addObserver(self, selector: "onTokenUpdated:", name:FBSDKAccessTokenDidChangeNotification, object: nil)

我的NSNotification名称错误(FBSDKProfileDidChangeNotification)。

然后你使用你的回调处理程序获取令牌并用它做任务!

有关详细信息,请参阅此处:http://www.andrewkouri.com/swift-1-2-and-facebooks-new-login-sdk/