Take the 2-minute tour ×

I am using the sample program to learn the facebook-ios-sdk, I found a problem: if I logged into Facebook on my device and run that sample app, everything is fine, however, when I delete my Facebook account from my device and run that sample app again, I can still pass the login process and SCViewController can still be seen (There is a fast-app-switch to Facebook app process, but only I need to do is to click the "OKey" button, I don't need to fill any email/password information to log into the Facebook).

I checked the code and found that after my account is removed from the device, the token in FBSession.activeSession.accessToken is still considered as valid. Is there any problem? and How can I clear the token and make the log in dialog pop up? I have already invoked [FBSession.activeSession closeAndClearTokenInformation] when log out, the token should be cleared, according to the Facebook sdk document, but it is not the case.

the environment I use: XCode 4.6.1, iPad 6.1 simulator and facebook-ios-sdk v3.2.1.

updated: paste some code here: in SCAppDelegate.m, I added 3 functions, which is not in the sample code but in the SDK online document:

- (void)showLoginView
    UIViewController* topViewController = [self.navigationController topViewController];
    UIViewController* modalViewController = [topViewController modalViewController];
    // If the login screen is not already displayed, display it. If the login screen is
    // displayed, then getting back here means the login in progress did not successfully
    // complete. In that case, notify the login view so it can update its UI appropriately.
    if (![modalViewController isKindOfClass:[SCLoginViewController class]]) {
        SCLoginViewController* loginViewController = [[SCLoginViewController alloc]
        [topViewController presentModalViewController:loginViewController animated:NO];
    } else {
        SCLoginViewController* loginViewController = (SCLoginViewController*)modalViewController;
        [loginViewController loginFailed];

- (void)sessionStateChanged:(FBSession*)session state:(FBSessionState)state error:(NSError*)error
    switch (state) {
        case FBSessionStateOpen: {
            UIViewController* topViewController = [self.navigationController topViewController];
            if ([[topViewController modalViewController]isKindOfClass:[SCLoginViewController class]]) {
                [topViewController dismissModalViewControllerAnimated:YES];
        case FBSessionStateClosed:
        case FBSessionStateClosedLoginFailed:
            [self.navigationController popToRootViewControllerAnimated:NO];
            [FBSession.activeSession closeAndClearTokenInformation];
            [self showLoginView];

    if (error) {
        UIAlertView* alertView = [[UIAlertView alloc]initWithTitle:@"Error" message:error.localizedDescription delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alertView show];

- (void)openSession
        [self sessionStateChanged:session state:status error:error];}];

Then in SCLoginViewController.m, I add a button, instead of using the existing FBLoginView object, to do the login job. The handler function for that button is as follows:

- (IBAction)performLogin:(id)sender {
    //[self.spinner startAnimating];
    SCAppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
    [appDelegate openSession];

Then in SCViewController.m, I add another button to do the log out job. The handler function for that button is as follows:

- (IBAction)logoutButtonWasPressed:(id)sender {
    [FBSession.activeSession closeAndClearTokenInformation];

other code is almost the same as that in the sample code.

share|improve this question
Did you tried reset the iPad simulator? I'm using it on my app and it works fine. Open the safari on the ipad and logout the facebook account too. –  Fabio Cardoso Apr 5 '13 at 0:44
Yes, I tried to remove the simulator files under ~/Liberary/Application Support/iPhone Simulator/6.1/Applications, clean the project and rebuild it from scratch, still the same problem. –  cao lei Apr 5 '13 at 12:59
Can you post some code to see if you have any error on it? –  Fabio Cardoso Apr 5 '13 at 13:06
sure. Please check my updated post. Thanks! –  cao lei Apr 5 '13 at 14:37
If you put a breakpoint on [FBSession.activeSession closeAndClearTokenInformation]; you can go there? –  Fabio Cardoso Apr 5 '13 at 14:46
show 1 more comment

1 Answer

Apart from the clearing the token, you also need to clear the cookie that is stored in Safari when you use it to log into Facebook.

The following works for me with Facebook SDK 3+ on iOS 5.1+:

[FBSession.activeSession closeAndClearTokenInformation];

NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for(NSHTTPCookie *cookie in [storage cookies])
    NSString *domainName = [cookie domain];
    NSRange domainRange = [domainName rangeOfString:@"facebook"];
    if(domainRange.length > 0)
        [storage deleteCookie:cookie];
share|improve this answer
add comment

Your Answer


