How do I store & access a Twitter Fabric login ses

2019-04-07 23:10发布

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

3条回答
【Aperson】
2楼-- · 2019-04-07 23:17

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.

查看更多
该账号已被封号
3楼-- · 2019-04-07 23:27

To access the authToken, userName or the userID for the logged in user session in Swift 4 is:

Twitter.sharedInstance().logIn(completion: { (session, error) in
     if (session != nil) {
         print(session!.authToken)
         print(session!.userName)
         print(session!.userID)
     } else {
         print(error?.localizedDescription)
     }
})
查看更多
该账号已被封号
4楼-- · 2019-04-07 23:29

Twitter kit automatically present a login screen if user has not logged in (no session exist..). But, before calling any future API, just check whether session is valid or not. In some APIs, you may required to pass this session too.

if ([[Twitter sharedInstance] session]) {

        TWTRShareEmailViewController *shareEmailViewController =
        [[TWTRShareEmailViewController alloc]
         initWithCompletion:^(NSString *email, NSError *error) {
             NSLog(@"Email %@ | Error: %@", email, error);
         }];

        [self presentViewController:shareEmailViewController
                           animated:YES
                         completion:nil];
    } else {
        // Handle user not signed in (e.g. attempt to log in or show an alert)
    }
}

You even don't need to worry about storing and managing access token and access token secret.

查看更多
登录 后发表回答