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?
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.
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)")
}
}
}
For Swift,
if let userID = Twitter.sharedInstance().sessionStore.session()?.userID {
Twitter.sharedInstance().sessionStore.logOutUserID(userID)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With