Christien
Christien

Reputation: 1213

Close Facebook session in Facebook-Ios-Sdk

How do I close aCtive Session Of the Facebook-iOS-Sdk each time when i start my app either after killing app it gives me the previous login session details. When I click on the facebook Button .I have used everything [appDelegate.session closeAndClearTokenInformation]; [FBSession.activeSession close]; [FBSession.activeSession closeAndClearTokenInformation] ..but active session still not been close..how do i close the Previous session data on clicking.

I use the below code to clear but whenever I click on the facebook button to start new session..it gives me back the previous credentials.

- (IBAction)buttonClickHandler:(id)sender {

appDelegate = [[UIApplication sharedApplication]delegate];

// this button's job is to flip-flop the session from open to closed
if (appDelegate.session.isOpen) {

    [appDelegate.session closeAndClearTokenInformation];
    [FBSession.activeSession close];
    [FBSession.activeSession  closeAndClearTokenInformation];
    FBSession.activeSession=nil;

} else {
    if (appDelegate.session.state != FBSessionStateCreated) {
        // Create a new, logged out session.
        appDelegate.session = [[FBSession alloc] init];
    }

    // if the session isn't open, let's open it now and present the login UX to the user
    [FBSession.activeSession closeAndClearTokenInformation];
    FBSession.activeSession=nil;
    [FBSession.activeSession openWithBehavior:FBSessionLoginBehaviorForcingWebView

                            completionHandler:^(FBSession *session,

                                                FBSessionState state,

                                                NSError *error) {
                                // and here we make sure to update our UX according to the new session state

                                NSLog(@" state=%d",state);

                                [FBRequestConnection
                                 startForMeWithCompletionHandler:^(FBRequestConnection *connection,
                                                                   id<FBGraphUser> user,
                                                                   NSError *error) {

                                     userInfo = @"";

                                     // Example: typed access (name)
                                     // - no special permissions required
                                     userInfo = user.username;

                                     NSLog(@"string %@", userInfo); 
                                          [self checkfacebook];

                                 }];


                            }];    } 


 }

and in ViewDidLoad

- (void)viewDidLoad
{
[super viewDidLoad];





AppDelegate *appDelegate = [[UIApplication sharedApplication]delegate];
if (!appDelegate.session.isOpen) {
    // create a fresh session object
    appDelegate.session = [[FBSession alloc] init];


    if (appDelegate.session.state == FBSessionStateCreatedTokenLoaded) {
        // even though we had a cached token, we need to login to make the session usable
        [FBSession.activeSession close];
        FBSession.activeSession=nil;
   }
}



NSHTTPCookieStorage* cookies = [NSHTTPCookieStorage sharedHTTPCookieStorage];
NSArray* facebookCookies = [cookies cookiesForURL:[NSURL URLWithString:@"http://login.facebook.com"]];

for (NSHTTPCookie* cookie in facebookCookies) {
    [cookies deleteCookie:cookie];
}    


}

Upvotes: 11

Views: 18675

Answers (3)

Adriano Spadoni
Adriano Spadoni

Reputation: 4790

Nitin's Gohel answer, Swift version:

    FBSession.activeSession().closeAndClearTokenInformation()
    FBSession.activeSession().close()
    FBSession.setActiveSession(FBSession())

you can use

    FBSession.setActiveSession(nil)

But the function ask for an unwrapped object, so "nil" seems not a good parameter to be pass

Upvotes: 0

Farooq Nasim Ahmad
Farooq Nasim Ahmad

Reputation: 164

After calling

    [FBSession.activeSession closeAndClearTokenInformation];

use following to clear cookies. It worked for me!

     NSHTTPCookieStorage* cookies = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (NSHTTPCookie* cookie in
     [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]) {
    [cookies deleteCookie:cookie];
}

Upvotes: 4

Nitin Gohel
Nitin Gohel

Reputation: 49710

hi Christien may be bellow two link its help's you please refur this two link of developers.facebook

http://developers.facebook.com/docs/reference/ios/3.0/class/FBSession/

http://developers.facebook.com/docs/reference/ios/3.0/class/FBSession/#close

[FBSession.activeSession closeAndClearTokenInformation];
[FBSession.activeSession close];
[FBSession setActiveSession:nil];

you can also check this two question related your issue :-

Facebook graph api log out not working

Facebook iOS SDK 3.1.1 "closeAndClearTokenInformation" method no working

hope its helps you

Upvotes: 28

Related Questions