iPhone: Facebook logout feature does not working

2019-02-28 10:00发布

I am implementing Facebook integration using FBConnct and it works fine but when I want to log out from the Facebook it doesn't work.

My code is as follows:

- (IBAction)logOutbuttonPressed:(id)sender { 
        [Facebook logout:self];
        }
        - (void)fbDidLogout {


            NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
            [defaults removeObjectForKey:@"FBAccessTokenKey"];
            [defaults removeObjectForKey:@"FBExpirationDateKey"];
            NSLog(@" after %@",facebook.accessToken);
            NSLog(@" date%@",facebook.expirationDate);
            [defaults synchronize];
          }


        - (void)logout:(id<FBSessionDelegate>)delegate {
          [self logout];

          if (delegate != self.sessionDelegate &&
            [delegate respondsToSelector:@selector(fbDidLogout)]) {
            [delegate fbDidLogout];
          }

3条回答
一夜七次
2楼-- · 2019-02-28 10:54
+(void)fbDidLogout
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    if ([defaults objectForKey:@"FBAccessTokenKey"]) 
    {
        [defaults removeObjectForKey:@"FBAccessTokenKey"];
        [defaults removeObjectForKey:@"FBExpirationDateKey"];
        [defaults synchronize];
    }

    // Hide the publish button.

    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];
        }
    }
}
查看更多
再贱就再见
3楼-- · 2019-02-28 11:02

Call this method to logout from facebook.

    - (void)logOutFB {

    fbGraph.accessToken = nil;
    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];
        }
    }
    }
查看更多
Root(大扎)
4楼-- · 2019-02-28 11:05

You need to implement this code for facebook logout as well

   - (void)fbDidLogout {


        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        [defaults removeObjectForKey:@"FBAccessTokenKey"];
        [defaults removeObjectForKey:@"FBExpirationDateKey"];
        NSLog(@" after %@",facebook.accessToken);
        NSLog(@" date%@",facebook.expirationDate);
        [defaults synchronize];

    //        Finding the Facebook Cookies and deleting them
    NSHTTPCookieStorage* cookies = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    NSArray* facebookCookies = [cookies cookiesForURL:
                                [NSURL URLWithString:@"http://login.facebook.com"]];
    for (NSHTTPCookie* cookie in facebookCookies) {
        [cookies deleteCookie:cookie];
    }
    fbGraph = nil;
      }

Try this import 'FBConnect.h' in ur second view controller

then .......

FBSession *session = [FBSession session]; [session logout];
查看更多
登录 后发表回答