Leaving an active Twitter session using cloth

I have read forums and suggestions on how to log out of Twitter in Xcode for IOS using Fabric, but I cannot get the logOut method to call and log out from the current session. Here is my current code for the login 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 imported and have the login functionality working well from the Fabric tutorial.

I just can't get the button I created that uses the LOGOUT action to log out of the current Twitter session. I even tried clearing the cookies to make sure it could erase the Twitter session from memory and reset, but nothing. If anyone could help me I would really appreciate it! Thank!

FYI: PLEASE don't just suggest [[Twitter sharedInstance] logOut] ;, This method doesn't do what I'm asking on its own. If someone can tell me how to successfully log out using this method as well as the rest of the procedure that will be fine.

+3


source to share


2 answers


After a long extensive series of methods, clearing cookies, data, pretty much anything else you could think of, I found it to be actually quite simple.

The easiest way to dump and clear the previous user session:



  • Go to settings
  • Go to your Twitter and deny Twitter access to your app (it should appear here)
  • Go back to your application 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 are quite a few in this method, and to be honest, some are probably outsiders and not even needed, but anyone who needs it can tinker with what should and should not remain. Anyway, hopefully it helps people - it certainly helped me!

+1


source


You can use this simple code for Swift 3:



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

      

+1


source







All Articles