Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I store & access a Twitter Fabric login session (iOS/Swift)?

I'm able to log in to Twitter through my app using this Twitter Fabric code:

 let logInButton = TWTRLogInButton(logInCompletion: {
        (session: TWTRSession!, error: NSError!) in
             // play with Twitter session
             if (session != nil) {
                 println("signed in as \(session.userName)");
                 self.TWUsernameLabel.text = "Logged in as @" + session.userName
        } else {
            println("error: \(error.localizedDescription)");
        }
    })

When I click the login button, it prompts me to approve the login and then logs me in, or it knows I already approved the login and it logs me in. This works like a charm and took all of ten minutes to set up. Amazing.

I already have an email-based login to access the app. I'd like to store a user's logged in Twitter account in that same database, so when a user logs in with their email, I already know their Twitter (if they have logged in before) and they don't need to log in again. The reason I do the email login is because Twitter is an important feature in my app, but not a total requirement.

The issue is that I have no idea how to access session outside of when the button is clicked and logInCompletion fires, and I don't know what variables to store upon initial login/check upon using the app.

I've read through the Twitter Fabric documentation numerous times, but it isn't written in swift, so it's pretty confusing. Any ideas? Thanks

like image 513
Andrew Avatar asked Apr 23 '15 00:04

Andrew


1 Answers

If there is currently an active session you should be able to access it just like the docs say to

Twitter.sharedInstance().session() 

If the user isn't logged in that method will return nil. If you want to know if someone is already authenticated just check to see if that method returns a value or not.

like image 126
Dare Avatar answered Oct 11 '22 09:10

Dare