Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative for Twitter.sharedInstance().session()?.userName after deprecation on iOS Swift

I've been using Twitter.sharedInstance().session()?.userName to display a user's twitter address in a Label in iOS (in Swift). Now I notice we get the following deprecation warning:

-[Twitter session] will soon be deprecated. It is recommended that users use -[TWTRSessionStore session] or -[TWTRSessionStore sessionForUserID:] if they are managing multiple users

I switched to the recommend way, and expected to find a .userName property around, but could not find one.

How are people going about swapping out Twitter.sharedInstance().session()?.userName going forwards?

like image 395
Neil Billingham Avatar asked Sep 19 '15 12:09

Neil Billingham


3 Answers

Ok so I was having a problem with this as well, and it seems like all of the newer changes (which are admittedly very poorly documented) are designed to allow for better control over multiple user sessions existing at the same time. I did some digging into the TWTRSessionStore class that they mention in the warning, and what I found is that although the concrete implementation of TWTRSessionStore doesn't have a -session property, it adheres to the the following protocols <TWTRUserSessionStore, TWTRGuestSessionStore, TWTRSessionRefreshingStore> and the TWTRUserSessionStore does have a -session property.

So, to sum up, this is how I log out:

NSString *userID = [[[[Twitter sharedInstance] sessionStore] session] userID];

if(userID){
    [[[Twitter sharedInstance] sessionStore] logOutUserID:userID];
}

I'm doing it in Objective-C, clearly, but the Swift version is very similar.

like image 184
Matthew Davis Avatar answered Nov 10 '22 17:11

Matthew Davis


If you're still looking for the solution, here's one:

if let session = Twitter.sharedInstance().sessionStore.session() {
    let client = TWTRAPIClient()
    client.loadUserWithID(session.userID) { (user, error) -> Void in
        if let user = user {
            print("@\(user.screenName)")
        }
    }
}
like image 42
tadija Avatar answered Nov 10 '22 15:11

tadija


For Swift,

    if let userID = Twitter.sharedInstance().sessionStore.session()?.userID {
        Twitter.sharedInstance().sessionStore.logOutUserID(userID)
    }
like image 1
4 revs, 3 users 77% Avatar answered Nov 10 '22 15:11

4 revs, 3 users 77%