Neil Billingham Neil Billingham - 3 months ago 32
Swift Question

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?

Answer

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