user2727725
user2727725

Reputation: 13

Logout Functionality using Facebook SDK in ios

I have Loggedout the facebook using Facebook SDK.But,Again go to same account it displays the values Two times in table view(For Ex:Prem Kumar.this name displays two times in the Cell).

//For Login

- (void)loginViewShowingLoggedInUser:(FBLoginView *)loginView

{
    // first get the buttons set for login mode


       NSLog(@"success");


    [HUD showWhileExecuting:@selector(LoadingProcess) onTarget:self withObject:nil animated:YES];

    if (FBSession.activeSession.isOpen)
    {

       NSLog(@"TOKEN : %@",[[FBSession activeSession]accessTokenData]);

        FBRequest *friendRequest = [FBRequest requestForGraphPath:@"me/friends?fields=name,picture,birthday,location"];

        [friendRequest startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error)
        {
            NSArray *data = [result objectForKey:@"data"];
            for (FBGraphObject<FBGraphUser> *friend in data)
            {
                [delegate.friendsListArray addObject:friend];
                NSLog(@"%@:%@:%@", [friend name],[friend birthday],[friend id]);
            }

             if ([delegate.friendsListArray count]!=0)

             {


            NSUserDefaults * standardDefaults=[NSUserDefaults standardUserDefaults];
            [[NSUserDefaults standardUserDefaults] synchronize];

            [standardDefaults setObject:delegate.friendsListArray forKey:@"FriendsListArray"];
            [standardDefaults setBool:YES forKey:@"logged_in"];
            [standardDefaults synchronize];


            NSLog(@"%@",[standardDefaults objectForKey:@"FriendsListArray"]);

            BirthdayList * birthdaylist=[[BirthdayList alloc]init];
            [self.navigationController pushViewController:birthdaylist animated:NO];
            [birthdaylist release];
              }
            else
            {
                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Friends not found" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
                [alertView show];
                [alertView release];
            }
        }];

    }

}

In Settings.m

-(void)LogoutTapped
{


    NSLog(@"Logged out of facebook");

    NSUserDefaults * standardDefaults=[NSUserDefaults standardUserDefaults];
    [[NSUserDefaults standardUserDefaults] synchronize];

    NSArray *keys = [[[[NSUserDefaults standardUserDefaults] dictionaryRepresentation]allKeys]copy];

       for(NSString *key in keys)
    {
        [standardDefaults removeObjectForKey:key];
        [standardDefaults removeObjectForKey:@"FriendsListArray"];
        [standardDefaults removeObjectForKey:@"logged_in"];
        [standardDefaults synchronize];

        NSLog(@"Key Name: %@", key);

    }

    [delegate.friendsListArray removeAllObjects];
    [keys release];

    NSLog(@"%@",[standardDefaults objectForKey:@"FriendsListArray"]);

    NSLog(@"%@",[standardDefaults objectForKey:@"logged_in"]);


    NSUserDefaults * userinfodefaults=[NSUserDefaults standardUserDefaults];
    NSArray *userkeys = [[[[NSUserDefaults standardUserDefaults] dictionaryRepresentation]allKeys]copy];

    for(NSString *userkey in userkeys)
    {
        [userinfodefaults removeObjectForKey:userkey];
        [userinfodefaults removeObjectForKey:@"userinfo"];
        [userinfodefaults removeObjectForKey:@"userbool"];

        NSLog(@"key1 Name: %@",userkey);
    }

    [delegate.UserListArray removeAllObjects];
    [delegate.Setmonthdataarray removeAllObjects];
    [delegate.SetMonthlistarray removeAllObjects];
    [userinfodefaults synchronize];
    [userkeys release];


    FBSession *session=[FBSession activeSession];
    [session closeAndClearTokenInformation];
    [session close];
    [[FBSession activeSession] closeAndClearTokenInformation];
    [[FBSession activeSession] close];
    [FBSession setActiveSession:nil];

    [delegate facebook].accessToken=nil;
    [delegate facebook].expirationDate=nil;

    NSHTTPCookieStorage* cookies = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    NSArray* facebookCookies = [cookies cookiesForURL:
                                [NSURL URLWithString:@"http://login.facebook.com"]];
    for (NSHTTPCookie* cookie in facebookCookies)
    {
        [cookies deleteCookie:cookie];
    }

    for (NSHTTPCookie *_cookie in cookies.cookies)
    {
        NSRange domainRange = [[_cookie domain] rangeOfString:@"facebook"];
        if(domainRange.length > 0){
            [cookies deleteCookie:_cookie];
        }
    }


    UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Logout" message:@"Logout Successfully" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
    [alert release];

    MainList * mainlist=[[MainList alloc]initWithNibName:@"MainList" bundle:nil];

    [self.navigationController pushViewController:mainlist animated:NO];

    [mainlist release];

}

and one more doubt.

To Login the facebook,I used Facebook SDK.

To Logout the facebook,I used Facebook Library.(Facebook.m,Facebook.h)

Have Any Problem For this?

Any Ideas Please help me.

Upvotes: 1

Views: 5763

Answers (1)

Divya Bhaloidiya
Divya Bhaloidiya

Reputation: 5064

Following code may be help you :

-(void) fbDidLogout
    {
        NSLog(@"Logged out of facebook");
        NSHTTPCookie *cookie;
        NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
        for (cookie in [storage cookies])
        {
            NSString* domainName = [cookie domain];
            NSRange domainRange = [domainName rangeOfString:@"facebook"];
            if(domainRange.length > 0)
            {
                [storage deleteCookie:cookie];
            }
        }
    }//

From your context, I'm assuming your device(s) does not have the Facebook app installed nor do you expect to use iOS 6 system authentication which would leave the default login behavior to use Safari. If you were to clear the Safari cookies, that should work but for a smoother experience in your scenario you should use the FBSession openWithBehavior:completionHandler: method and specify a behavior of FBSessionLoginBehaviorForcingWebview so that it uses the inline webview dialog for authentication.

See the SwitchUserSample in the Facebook iOS SDK for an example since that sample demonstrates an app that can toggle between multiple accounts.

https://developers.facebook.com/docs/ios/login/#logout

https://developers.facebook.com/docs/ios/ios-sdk-tutorial/

Upvotes: 5

Related Questions