8

I have made a basic app using Twitter's fabric that allows user to tweet within my app and Provide Login With Twitter.Every things works a I Wanted.

How my app Works

If the user doesn't logged in to twitter my app allows him to login and if he is login then directly allows him to tweet.

Now the Big Part Comes

As I saw in many Apps I Can Remove my signed account from the app.And I am not able to get any method that help me to achieve this. I want to allow user to logout from twitter within my app whenever he/She wants.

I googled but i doesn't find anything

Here Is my Code:

- (IBAction)LogOut:(id)sender
{
    [[Twitter sharedInstance]logOut];
}

- (IBAction)LogMeIn:(id)sender
{
[[Twitter sharedInstance] logInWithCompletion:^
 (TWTRSession *session, NSError *error) {
     if (session) {
         NSLog(@"signed in as %@", [session userName]);
         [LoginButton setTitle:@"LogOut" forState:normal];

     } else {
         NSLog(@"error: %@", [error localizedDescription]);
     }
 }];
}
Dalvik
  • 217
  • 1
  • 2
  • 12
  • 1
    In Swift 4.2 from dory Daniel answer, let store = Twitter.sharedInstance().sessionStore if let userID = store.session()?.userID { store.logOutUserID(userID) } – Naresh Feb 14 '19 at 09:40

9 Answers9

14

this was a problem with NSCookie from Foundation framework And i slove this issues with help of below code

NSURL *url = [NSURL URLWithString:@"https://api.twitter.com"];
NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:url];
for (NSHTTPCookie *cookie in cookies)
{
    [[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
}
Hiren kanetiya
  • 251
  • 2
  • 16
  • ...not sure why this answer only has 2 upvotes. this is the only solution that i have found to work in order to successfully log out of twitter using fabric. thank you so much this is great – Will Von Ullrich Aug 10 '15 at 17:12
  • 1
    I used this code but i still can't switch users. I placed this just after: [[Twitter sharedInstance] logOut]; Any ideas? – Héctor Sep 08 '15 at 16:55
  • please chack system setting twitter account Loged Out or Not? if there is no loged in any user then it should be work properly. – Hiren kanetiya Sep 09 '15 at 09:51
  • This was working for me, but now it is appears it is no longer working on iOS 8.1. I'm not sure if it never worked on iOS 8.1, or, since the app is now being built with the iOS 10 SDK and newer Twitter SDK, that something else has changed.... – xaphod Dec 13 '16 at 21:32
  • where to use this code? after [[Twitter sharedInstance] logOut]? – Harminder Singh Dec 29 '17 at 06:37
13

You need to use below code

[[[Twitter sharedInstance] sessionStore] logOutUserID:USERID];

Provide user id of the user you want to logout.

UditS
  • 1,936
  • 17
  • 37
Purushottam Sain
  • 306
  • 2
  • 10
  • Since `[[Twitter sharedInstance] logOut];` is going to be deprecated - I think this should be the accepted answer. – orenk86 Apr 03 '16 at 09:29
9

UPDATE: May 2016 - Framework has changed so this answer is no longer relevant.

See this answer: https://stackoverflow.com/a/35765833/940709 and this answer: https://stackoverflow.com/a/30916904/940709 instead.

[[[Twitter sharedInstance] sessionStore] logOutUserID:USERID];

Community
  • 1
  • 1
SeanCAtkinson
  • 753
  • 1
  • 4
  • 15
  • how can i call it .. the doc only gives me **-(void)logout** but doesn't show how to use – Dalvik Mar 10 '15 at 12:03
  • The twitter object is a singleton. Call [[Twitter sharedInstance] logOut]; in your code. – SeanCAtkinson Mar 10 '15 at 13:02
  • When i run this code nothing happens...After Logout when i press login it shows that i am already signed in – Dalvik Mar 10 '15 at 13:14
  • It looks like you change the log in button to a log out button after a successful login. How are you linking your IBActions to the button? You may also need to remove the logMeIn target from the button and add the logOut selector as a target on the button. Probably easier is to have a single IBAction for your button and then check the logged in state when the button is pressed. If they are logged out, run the login code, if they are logged in, run the log out code. – SeanCAtkinson Mar 10 '15 at 13:32
  • FYI you can check if the user is logged in by calling the session parameter of the twitter object so: [[Twitter sharedInstance] session]; If session is nil then they are not logged in. – SeanCAtkinson Mar 10 '15 at 13:35
  • The doc clearly says that if the authentication fails the result would be not nil. So How can Check Whether the user is logged in or not . http://screencast.com/t/dvG8LLdc – Dalvik Mar 11 '15 at 11:11
  • No the doc clearly says the error will be non-nil not the session object. Here is what is says on the session object: http://i.imgur.com/aPXVWoO.png – SeanCAtkinson Mar 12 '15 at 09:44
  • Unfortunately the log out method doesn't seem to actually log the user out. – Daniel Roberts May 26 '16 at 14:33
  • Looks like the framework has changed since this answer was accepted. I'll update it later tonight with links the answers below. – SeanCAtkinson May 26 '16 at 14:38
  • i am not able to logout user in ios10..I tried all solutions but nothing works... – jayant rawat Apr 06 '17 at 12:36
8

This is the best simple answer for Swift 3:

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

or you can use Naeem's answer but add () after store.session

J. Chomel
  • 8,193
  • 15
  • 41
  • 69
Dory Daniel
  • 798
  • 14
  • 21
3

Logout Code From Twitter Docs:

Objective-C

TWTRSessionStore *store = [[Twitter sharedInstance] sessionStore];
NSString *userID = store.session.userID;

[store logOutUserID:userID];

Swift

let store = Twitter.sharedInstance().sessionStore

if let userID = store.session()?.userID {
  store.logOutUserID(userID)
}
Naresh
  • 16,698
  • 6
  • 112
  • 113
Naeem
  • 789
  • 1
  • 10
  • 23
2

For Swift try this,

/**
 *  Deletes the local Twitter user session from this app. This will not remove the system Twitter account nor make a network request to invalidate the session.
 *
 *  @param userID ID of the user to log out
 */
Twitter.sharedInstance().sessionStore.logOutUserID(userId)
Mohammad Zaid Pathan
  • 16,304
  • 7
  • 99
  • 130
0

First make sure some user is signed in, then perform logout.

NSString *signedInUserID = [TWTRAPIClient clientWithCurrentUser].userID;
if (signedInUserID) {
   [[Twitter sharedInstance].sessionStore logoutUserID:signedInUserID];
}
PanxShaz
  • 760
  • 9
  • 12
0

While login mention method as webBasedForceLogin, so that it will not create entry into Safari Cache.

private func twitterLogin() {
    Twitter.sharedInstance().logIn(withMethods: .webBasedForceLogin, completion: { (session, error) in
        if let error = error {
            print(error.localizedDescription)
        }

        guard let session = session else {
            return
        }

        print("success! Welcome \(session.userName).")
        self.twitterButton.setTitle("TWITTER LOGOUT", for: .normal)
    })
}

private func twitterLogout() {
    let sessionStore = Twitter.sharedInstance().sessionStore
    if let userID = sessionStore.session()?.userID {
        sessionStore.logOutUserID(userID)
    }

    twitterButton.setTitle("TWITTER LOGIN", for: .normal)
}
appleBoy21
  • 632
  • 8
  • 23
-2

Use below:

[TWTRSessionStore logout]

Deprecated:

[Twitter logOut] 

is deprecated.

Users are encouraged to call - [TWTRSessionStore logout] instead of calling this method on the Twitter instance directly.

Jigar Tarsariya
  • 3,189
  • 3
  • 14
  • 38