45
votes

I have integrated Facebook login in my app and therfore user can login with both my app account and also Facebook and do corresponding actions.For Facebook integration I have added Facebook SDK.Now when Logout button is clicked in my app it has to clear all the credentials of Facebook Account.I have gone for :

-(IBAction)btnlogOutClicked:(id)sender
{
  [appDelegate fbDidlogout]; 
}
-(void)fbDidlogout
{
    FBSession* session = [FBSession activeSession];
    [session closeAndClearTokenInformation];
    [session close];
    [FBSession setActiveSession:nil];


}

But when I again click on button I m redirected directly to my account without going to Facebook Login page.

How can I logout of Facebook?

5
Which SDK? There are many. (PHP, Js, C#)...Mihai Bujanca
Facebook ios SDK is the one I m using...Aaradhya
This code is working fine for me :)Soumya Ranjan

5 Answers

85
votes

Using new Facebook SDK login kit just write below line and thats it..

[[FBSDKLoginManager new] logOut];

If using swift, make sure to have the necessary imports

import FBSDKLoginKit

func logout() {
    FBSDKLoginManager().logOut()
}
20
votes

For logout you should try this

you can add the Logout button on Navigation Controller (Top Right Corner) in viewDidLoad method

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] 
                                    initWithTitle:@"Logout"
                                    style:UIBarButtonItemStyleBordered
                                    target:self
                                    action:@selector(logoutButtonWasPressed:)];

and the action method for above added button is

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

Hope this will help you!

Reference

Edit:

As you asked why its not asking for UserName and Password,So the reason is :

When we integrate Facebook SDK in our app and try to login, it automatically check two places (to make sure we have already logged-in Facebook or not)

  1. First of all It checks whether we have already logged-in into Facebook Native app which is installed on this device.

  2. then It checks whether we have saved our FaceBook UserName and Password in Device Settings.

If both places we haven't logged-in then It will ask UserName and Password in application

you can check Facebook account setup in Device Settings as shown in below screen shot,

Press Home Button --> Settings -->Facebook

enter image description here

9
votes

FBSDK is doing logout like this:

  [FBSession.activeSession closeAndClearTokenInformation];
  FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
  [login logOut];
4
votes

If using swift 3 or 4:

var loginManager = LoginManager()

Paste this code when some action is need to be done for logging out

loginManager.logOut()
0
votes

In your postButtonClicked write following if else :

-(void)postButtonClicked
{

    _session = [[FBSession sessionForApplication:kApiKey secret:kApiSecret delegate:self] retain];
    [_session resume];

    posting = YES;
    showSlideShow = 1;

    if (_facebookName != nil)
    {
        [self logoutButtonClicked];
    }
    if (![_session isConnected])
    {
        self.loginDialog = nil;
        _loginDialog = [[FBLoginDialog alloc] init];
        [_loginDialog show];
    }
    else {
        self.loginDialog = nil;
        _loginDialog = [[FBLoginDialog alloc] init];
        [_loginDialog show];
    }
}