Logout of Facebook in app using Facebook ios SDK
Asked Answered
G

5

45

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?

Goodyear answered 25/3, 2013 at 8:2 Comment(3)
Which SDK? There are many. (PHP, Js, C#)...Serrulation
Facebook ios SDK is the one I m using...Goodyear
This code is working fine for me :)Cotemporary
Y
85

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()
}
Yen answered 19/5, 2015 at 13:57 Comment(3)
Is this working? FBSDKLoginManager need to be same as used in login? or may be new object.?Antibiotic
Working so smooth! ThxBonnell
on later revisions, the class has ben renamed to LoginManager() onlyGoodin
L
20

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

Leeuwarden answered 25/3, 2013 at 10:54 Comment(4)
I tried with ur code but it get login with my credentials if I click on Login buton without asking for login id and psswrd...What else should I need to do ?Goodyear
see in iOS 6 we have native framework for Facebook, so when you login from app it checks whether you have already saved login info in Device settings or not, so make sure as like Twitter you have entered Facebook account info in settings, else it will prompt you for UN and Pass whenever you try to login from your app.Leeuwarden
Sorry I didn't get u...Now I can Logout of facebook when I open facebook url in browser and then logout there.,Then its prompting for Facebook credentials (Id and psswrd) when I click on Login btn in my appGoodyear
@Goodyear Plz check added screen shot for Facebook setup, let me know if you have still trouble with it, drop you small sample code to achieve this.Leeuwarden
N
9

FBSDK is doing logout like this:

  [FBSession.activeSession closeAndClearTokenInformation];
  FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
  [login logOut];
Norvol answered 29/4, 2015 at 9:43 Comment(0)
B
4

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()
Belanger answered 19/2, 2018 at 15:38 Comment(1)
@SaschaM78 that for help mateBelanger
P
0

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];
    }
}
Puccoon answered 25/3, 2013 at 8:23 Comment(1)
Im using Facebook ios SDK.I have not used any FBLoginDialog.Can u please tell me how can I logout using Facebook ios SDK?Goodyear

© 2022 - 2024 — McMap. All rights reserved.