iOS - Twitter session expires in a day



Hello guys,

I use TwitterKit for my iOS application. I followed the instruction in the document and it seemingly worked well but the session expires in a day.

I think the session won’t be nil. Could you find something wrong in my code below?

final class wdTwitterService: NSObject {
    override static func initialize() {
        // API keys are written in info.plist.
    static func login(completion:(String) -> Void, failed: (NSError?) -> Void) {
        Twitter.sharedInstance().logInWithCompletion { session, error in
            if let session = session {
                // Save the logged username
                var namesDic = [String: String]()
                if let names = wdUserDefault.objectForKey(UD_TWITTER_USERNAMES) as? [String: String] {
                    namesDic = names
                namesDic[session.userID] = session.userName
                wdUserDefault.setObject(namesDic, key: UD_TWITTER_USERNAMES)
            } else {
    static func isLoggedIn() -> Bool {
        if let _ = Twitter.sharedInstance().sessionStore.session()  {
            return true
        return false
    static var session: TWTRAuthSession? {
        get {
            if let session = Twitter.sharedInstance().sessionStore.session() {
                return session
            return nil



Thanks for reaching out on this @1024207411! If you’re using recent versions of the SDK, sessions can expire, but a new token will be pulled in when this happens.


Yes, we are using the latest Twitter Kit. So what happens if the user who has nil for his session due to the expiration sends a request such as get/tweet? Can Twitter Kit always regenerate the session automatically when he sends a request even though his session is nil?
I mean do we need to monitor if his session has been expired whenever sending requests?


Ahh, sorry I misunderstood. Yes, a user needs to be logged in for the tweet and for that I’d recommend using TWTRAPIClient.clientWithCurrentUser() so that you don’t need to handle cached sessions at all. There’s some sample code around using that, but in this case for requesting an email address here, that you can modify to suit your specific needs :slight_smile:


@bonnell It perfectly works now! Thank you for your tips :slight_smile: