Aaradhya
Aaradhya

Reputation: 559

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.

How can I logout of Facebook?

Upvotes: 45

Views: 36416

Answers (5)

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

Upvotes: 4

iHulk
iHulk

Reputation: 4909

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

Upvotes: 85

Aqib Mumtaz
Aqib Mumtaz

Reputation: 5074

FBSDK is doing logout like this:

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

Upvotes: 9

swiftBoy
swiftBoy

Reputation: 35783

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

Upvotes: 20

Rushi
Rushi

Reputation: 4500

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

Upvotes: 0

Related Questions