Reputation: 33
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".
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.
Since the FBSession
class no longer exists with FB iOS SDK4.0, how can I correctly determine whether a cached session exists?
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?
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. Unfortunately, the currentAccessToken is nil, as described above. 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.
Upvotes: 3
Views: 3553
Reputation: 714
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.
[[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.
Upvotes: 9
Reputation: 1473
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)
}
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)
}
Upvotes: 0
Reputation: 5183
Add the following in your AppDelegate didFinishLaunchingWithOptions :
return [[FBSDKApplicationDelegate sharedInstance] application:application
didFinishLaunchingWithOptions:launchOptions];
This would get you [FBSDKAccessToken currentAccessToken] when user is logged in.
Refer to : https://developers.facebook.com/docs/ios/getting-started#startcoding
Upvotes: 3