在Facebook iOS SDK v4.0中可靠检查活动启动时的活动会话 [英] Reliable check for active session on app startup in Facebook iOS SDK v4.0

查看:54
本文介绍了在Facebook iOS SDK v4.0中可靠检查活动启动时的活动会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有FBSDKLoginButton视图类的简单Facebook登录对话框使用Facebook iOS SDK 4.0创建iOS应用.

I am creating an iOS App with Facebook iOS SDK 4.0, using a simple Facebook login dialog with the FBSDKLoginButton view class.

在先前成功登录后启动应用程序时,该按钮正确显示为已登录"状态,即显示文本注销".

When starting the app after a previous successful login, the button correctly appears in "logged in" state, i.e. displaying the text "Log out".

但是,[FBSDKAccessToken currentAccessToken]为nil,这就是为什么我的App意识不到活动会话,因此没有用数据填充FB相关字段的原因.

However, [FBSDKAccessToken currentAccessToken] is nil, which is why my App is unconscious of the active session and thus does not populate the FB-related fields with data.

由于FBSession类在FB iOS SDK4.0中不再存在,如何正确确定是否存在缓存的会话?

Since the FBSession class no longer exists with FB iOS SDK4.0, how can I correctly determine whether a cached session exists?

我认为[FBSDKAccessToken currentAccessToken]是做到这一点的方法.但是,如前所述,currentAccessToken尚未设置...为什么它可能为零,或者如何正确检测活动/缓存的会话?

I assumed that [FBSDKAccessToken currentAccessToken] was the way to do it. But, as mentioned, the currentAccessToken is not set... Any ideas why it could be nil, or how to correctly detect an active/cached session?

我已经在主视图控制器的viewDidLoad方法中对currentAccessToken进行了调用,该方法包含Facebook登录对话框广告按钮.不幸的是,如上所述,currentAccessToken为nil.有什么建议为什么不可用?

I have put the call to currentAccessToken in the viewDidLoad method of my main view controller, which contains the Facebook login dialog ad button. Unfortunately, the currentAccessToken is nil, as described above. Any suggestions why it is not available?

Bigman解决了该问题(请参见下面的回答,请参见他的评论):在viewDidAppear中使用currentAccessToken而不是viewDidLoad可以解决问题.

Bigman solved the issue (see his comments in his answer below): Getting the currentAccessToken in viewDidAppear instead of viewDidLoad did the trick.

推荐答案

获取此令牌的方法是,您将需要为此FBSDKLoginButton提供一个委托. 我使其中一个视图控制器符合FBSDKLoginButtonDelegate并实现了

The way to get this token is you will want to provide a delegate this FBSDKLoginButton. I make one of my view controller conform to FBSDKLoginButtonDelegate and I implemented

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

    let token = result.token

    println(token)
}

在情节提要中,我将此视图控制器设置为FBSDKLoginButton.该令牌在运行时并非为空.

In the storyboard, I set this view controller to the FBSDKLoginButton. The token is not nil at runtime.

[我的第二次尝试]

[My second attempt]

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

    let token = result.token

    let accessToken = FBSDKAccessToken.currentAccessToken()

    println(accessToken)

} 

这篇关于在Facebook iOS SDK v4.0中可靠检查活动启动时的活动会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆