Logout of Facebook in app using Facebook ios SDK

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.

  • NSTimer inside custom tableViewCell
  • RealmSwift initializer: self used before super.init call
  • How to use fmdb for a login page?
  • NSURLSession/NSURLConnection HTTP load failed on iOS 9
  • Subclassing UIAlertController and encountering runtime errors
  • UISearchController won't dismiss searchbar and overlap for iOS 8 Swift
  • How can I logout of Facebook?

    4 Solutions Collect From Internet About “Logout of Facebook in app using Facebook ios SDK”

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

    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

    FBSDK is doing logout like this:

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

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