简体   繁体   中英

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.

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
    }
}

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.

i did it as in the Facebook example

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

}

The session is active if the state is either in FBSessionStateOpen or in FBSessionStateOpenTokenExtended . You can use the function below to check if the user is logged in:

- (BOOL)isSessionOpen
{
    return FBSession.activeSession.state == FBSessionStateOpen || FBSession.activeSession.state == FBSessionStateOpenTokenExtended;
}

How are you opening your FBSession?

If you're creating an instance, be sure to set FBSession.activeSession. That was my issue for a while.

    if ([FBSDKAccessToken currentAccessToken]) 
{
   NSLog(@"Already login");
    //[FBSession openActiveSessionWithAllowLoginUI: YES];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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