Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if logged in on ios facebook sdk 3.0

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.

like image 910
gsach Avatar asked Sep 13 '12 18:09

gsach


People also ask

What is Facebook SDK for iOS?

The Facebook SDK enables: Facebook Login - Authenticate people with their Facebook credentials. Share and Send dialogs - Enable sharing content from your app to Facebook. App Events - Log events in your application.

What is the use of Facebook SDK?

The Facebook SDK is a set of software components that developers can include in their mobile app to understand how people use the app, run optimized marketing campaigns and enable Facebook login and social sharing.


3 Answers

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
    }
}
like image 121
Moxy Avatar answered Oct 12 '22 22:10

Moxy


If you are using FBSDK greater then 4.x then there is no concept of FBSession. You have to find the active session only by using [FBSDKAccessToken currentAccessToken] simply check if it has nil value, no active session else it is.

Instead, you should check [FBSDKAccessToken currentAccessToken] at viewDidLoad or similar. If a current token is available, do the post-login work. You can also use currentAccessToken to retrieve cached tokens.

you can find more here https://developers.facebook.com/docs/ios/upgrading-4.x

FBSession.activeSession has been replaced with [FBSDKAccessToken currentAccessToken] and FBSDKLoginManager. There is no concept of session state. Instead, use the manager to login and this sets the currentAccessToken reference.

like image 34
iHulk Avatar answered Oct 12 '22 21:10

iHulk


i did it as in the Facebook example

if (FBSession.activeSession.state == FBSessionStateCreatedTokenLoaded)
{

}
like image 9
Kassem Avatar answered Oct 12 '22 21:10

Kassem