Will Ullrich
Will Ullrich

Reputation: 2228

Logout of Active Twitter Session Using Fabric

I have read through the forums and suggestions about how to logout of Twitter in Xcode for IOS using Fabric, but I can't get the logOut method to call and logout the user from the current session. Here is my current code for the login view controller:

- (IBAction)TESTT:(id)sender {
[[Twitter sharedInstance] logInWithCompletion:^
 (TWTRSession *session, NSError *error) {
     if (session != nil) {
         NSLog(@"signed in as %@", [session userName]);
     } else {
         NSLog(@"error: %@", [error localizedDescription]);
     }
 }];
}

- (IBAction)LOGOUT:(id)sender {
[self logOut];
}

- (void)logOut{
[[Twitter sharedInstance] logOut];
}

I have imported and have the login functionality working well from the Fabric tutorial.

I just can't get the button that I made which is using the LOGOUT action to logout the user from the current Twitter session. I have even tried to clear the cookies to see if that could wipe the Twitter session from the memory and reset it - but nothing. If anyone could help me out I would really appreciate it - thanks!

FYI: PLEASE do not suggest only [[Twitter sharedInstance] logOut]; . This method does not do what I am asking by itself. If someone can tell me how to successfully logout using this method along with the rest of the procedure that would be fine.

Upvotes: 3

Views: 1143

Answers (2)

Dory Daniel
Dory Daniel

Reputation: 826

You can use this simple code for Swift 3:

let store = Twitter.sharedInstance().sessionStore
        if let userID = store.session()?.userID {
            store.logOutUserID(userID)
        }

Upvotes: 1

Will Ullrich
Will Ullrich

Reputation: 2228

After a long extensive series of methods, clearing of cookies, data, almost everything you could think of, I discovered it is actually quite simple.

The easiest way to sign out and clear the previous user session is as follows:

  1. Go to settings
  2. Go to your Twitter and Disallow Twitter access to your app (it should appear here)
  3. Go back to the app and call the following method:

    - (void)twitterLogout:(id)sender {
        NSUserDefaults *twitterSession = [NSUserDefaults standardUserDefaults];
        [twitterSession setObject:0 forKey:@"TwitterSession"];
        [twitterSession synchronize];
    
        NSLog(@"Twitter session = %@", twitterSession);
    
        [[Twitter sharedInstance] logOut];
        [self.view insertSubview:_logoutTwitter atIndex:16];
    
    
        NSHTTPCookie *cookie;
         NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
        for (cookie in [storage cookies])
        {
            NSString* domainName = [cookie domain];
            NSRange domainRange = [domainName rangeOfString:@"Twitter"];
            if(domainRange.length > 0)
            {
                [storage deleteCookie:cookie];
            }
        }
    
        NSURL *url = [NSURL URLWithString:@"https://api.twitter.com"];
        NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:url];
        for (NSHTTPCookie *cookie in cookies)
        {
            [[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
        }  
    }
    

There is quite a lot in this method, and to be honest some of it is probably extraneous and not even needed, but anyone who needs this can mess around with what should and shouldn't stay. Either way hopefully this helps people - it certainly helped me!

Upvotes: 1

Related Questions