简体   繁体   English

在Facebook iOS SDK v4.0中可靠地检查应用启动时的活动会话

[英]Reliable check for active session on app startup in Facebook iOS SDK v4.0

I am creating an iOS App with Facebook iOS SDK 4.0, using a simple Facebook login dialog with the FBSDKLoginButton view class. 我正在使用Facebook iOS SDK 4.0创建一个iOS应用程序,使用带有FBSDKLoginButton视图类的简单Facebook登录对话框。

When starting the app after a previous successful login, the button correctly appears in "logged in" state, ie displaying the text "Log out". 在上次成功登录后启动应用程序时,该按钮正确显示为“已登录”状态,即显示文本“注销”。

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. 但是, [FBSDKAccessToken currentAccessToken]为nil,这就是为什么我的应用程序不知道活动会话,因此不会使用数据填充FB相关字段。

Since the FBSession class no longer exists with FB iOS SDK4.0, how can I correctly determine whether a cached session exists? 由于FB iOS SDK4.0不再存在FBSession类,因此如何正确确定缓存会话是否存在?

I assumed that [FBSDKAccessToken currentAccessToken] was the way to do it. 我假设[FBSDKAccessToken currentAccessToken]是这样做的。 But, as mentioned, the currentAccessToken is not set... Any ideas why it could be nil, or how to correctly detect an active/cached session? 但是,如上所述, currentAccessToken没有设置...任何想法为什么它可能是零,或如何正确检测活动/缓存会话?

EDIT: I have put the call to currentAccessToken in the viewDidLoad method of my main view controller, which contains the Facebook login dialog ad button. 编辑:我已经在我的主视图控制器的viewDidLoad方法中调用currentAccessToken ,其中包含Facebook登录对话框广告按钮。 Unfortunately, the currentAccessToken is nil, as described above. 不幸的是,如上所述,currentAccessToken为零。 Any suggestions why it is not available? 任何建议为什么它不可用?

EDIT2: Bigman solved the issue (see his comments in his answer below): Getting the currentAccessToken in viewDidAppear instead of viewDidLoad did the trick. EDIT2:BIGMAN解决了这个问题(见他在下面他的回答评论):获取currentAccessTokenviewDidAppear而不是viewDidLoad没有的伎俩。

So I just ran into this myself. 所以我自己就碰到了这个。

Goal: On app launch, if a token has already been acquired from a previous successful login attempt, do some alternative logic (in my case, launching directly to a different view controller). 目标:在应用启动时,如果已经从先前成功登录尝试获取了令牌,请执行一些替代逻辑(在我的情况下,直接启动到不同的视图控制器)。

The key here is what Dheeraj brought up originally. 这里的关键是Dheeraj最初提出的。

[[FBSDKApplicationDelegate sharedInstance] application:application didFinishLaunchingWithOptions:launchOptions];

The minute you call out to their shared delegate, that is when they wake up everything. 你呼叫他们的共享代表的那一刻,就是他们唤醒一切。 So code example. 所以代码示例。 This might be a really bad practice, I'm unsure, this is just how I got it to work. 这可能是一个非常糟糕的做法,我不确定,这就是我如何让它发挥作用。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  [FBSDKLoginButton class];
  [FBSDKProfile enableUpdatesOnAccessTokenChange:YES];
  NSLog(@"What the heck, no token? %@", [FBSDKAccessToken currentAccessToken]);
  //Output: What the heck, no token? (nil)
  BOOL fbDidFinish = [[FBSDKApplicationDelegate sharedInstance] application:application didFinishLaunchingWithOptions:launchOptions];
  NSLog(@"Hey! My Token! %@", [FBSDKAccessToken currentAccessToken]);
  //Output: Hey! My Token! <FBSDKAccessToken: 0x7fddf8d136a0>
  //Your address in memory will be different
  //At this point, you can do additional logic based on having the Token, like skipping ahead to a different ViewController
  return fbDidFinish;
}

Assuming you've already successfully logged in once before, you should see your access token on the second log output. 假设您之前已经成功登录过一次,您应该在第二个日志输出上看到您的访问令牌。 Hope this helps. 希望这可以帮助。

Add the following in your AppDelegate didFinishLaunchingWithOptions : 在AppDelegate didFinishLaunchingWithOptions中添加以下内容:

return [[FBSDKApplicationDelegate sharedInstance] application:application
                                    didFinishLaunchingWithOptions:launchOptions];

This would get you [FBSDKAccessToken currentAccessToken] when user is logged in. 这将在用户登录时获得[FBSDKAccessToken currentAccessToken]。

Refer to : https://developers.facebook.com/docs/ios/getting-started#startcoding 请参阅: https//developers.facebook.com/docs/ios/getting-started#startcoding

The way to get this token is you will want to provide a delegate this FBSDKLoginButton. 获取此令牌的方法是您将要为此FBSDKLoginButton提供委托。 I make one of my view controller conform to FBSDKLoginButtonDelegate and I implemented 我使我的一个视图控制器符合FBSDKLoginButtonDelegate并实现了

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

    let token = result.token

    println(token)
}

In the storyboard, I set this view controller to the FBSDKLoginButton. 在故事板中,我将此视图控制器设置为FBSDKLoginButton。 The token is not nil at runtime. 令牌在运行时不是nil。

[My second attempt] [我的第二次尝试]

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

    let token = result.token

    let accessToken = FBSDKAccessToken.currentAccessToken()

    println(accessToken)

} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM