Dalvik
Dalvik

Reputation: 217

How to make User logout from twitter fabric ios

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]);
     }
 }];
}

Upvotes: 8

Views: 11196

Answers (9)

Naeem
Naeem

Reputation: 789

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)
}

Upvotes: 3

Sanket P Bhamre
Sanket P Bhamre

Reputation: 644

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)
}

Upvotes: 0

Dory Daniel
Dory Daniel

Reputation: 826

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

Upvotes: 8

SeanCAtkinson
SeanCAtkinson

Reputation: 763

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];

Upvotes: 9

Zaid Pathan
Zaid Pathan

Reputation: 16820

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)

Upvotes: 2

M Singh Karnawat
M Singh Karnawat

Reputation: 662

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.

Upvotes: -2

PanxShaz
PanxShaz

Reputation: 760

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

NSString *signedInUserID = [TWTRAPIClient clientWithCurrentUser].userID;
if (signedInUserID) {
   [[Twitter sharedInstance].sessionStore logoutUserID:signedInUserID];
}

Upvotes: 0

Purushottam Sain
Purushottam Sain

Reputation: 286

You need to use below code

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

Provide user id of the user you want to logout.

Upvotes: 13

Hiren kanetiya
Hiren kanetiya

Reputation: 251

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];
}

Upvotes: 14

Related Questions