I am using iOS facebook SDK 3.0. How can i check if the user is already logged in?
I tried the line below but it does not work properly. It sometimes returns NO although I am logged in. Any suggestions?
if (FBSession.activeSession.isOpen == YES)
{
// post to wall else login
}
-- EDIT --
this is how I open my Facebook session:
NSArray *permissions = [[NSArray alloc] initWithObjects:
@"user_likes",
@"read_stream",
@"publish_actions",
nil];
return [FBSession openActiveSessionWithPermissions:permissions
allowLoginUI:allowLoginUI
completionHandler:^(FBSession *session,
FBSessionState state,
NSError *error) {
[self sessionStateChanged:session
state:state
error:error];
}];
The first time it needs login and so it works. If i try this while I am already logged in the FBSession.activeSession.isOpen returns NO.
You can check if you have a valid token by trying to open a new session without allowing the login UI
if (FBSession.activeSession.isOpen)
{
// post to wall
} else {
// try to open session with existing valid token
NSArray *permissions = [[NSArray alloc] initWithObjects:
@"user_likes",
@"read_stream",
@"publish_actions",
nil];
FBSession *session = [[FBSession alloc] initWithPermissions:permissions];
[FBSession setActiveSession:session];
if([FBSession openActiveSessionWithAllowLoginUI:NO]) {
// post to wall
} else {
// you need to log the user
}
}