I am implementing Twitter login in my app. I am able to login using Fabric and everything seems fine. Now i want the user to login only once and next time he/she opens the app a message should be displayed that "you are already signed in" or something like this and login button should not be there on the screen. I should also be able to get user's info (Twitter user name) when user launch the app second time, how can i achieve that ?
here is my code so far
import UIKit
import TwitterKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func viewDidAppear(animated: Bool) {
var logInButton = TWTRLogInButton { (session, error) in
if let unwrappedSession = session {
print(session?.userName)
print(session?.userID)
//requesting Email address
let client = TWTRAPIClient.clientWithCurrentUser()
let request = client.URLRequestWithMethod("GET",
URL: "https://api.twitter.com/1.1/account/verify_credentials.json",
parameters: ["include_email": "true", "skip_status": "true"],
error: nil)
client.sendTwitterRequest(request) { response, data, connectionError in
print(data)
print(response)
}
let alert = UIAlertController(title: "Logged In",
message: "User \(unwrappedSession.userName) has logged in",
preferredStyle: UIAlertControllerStyle.Alert
)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
} else {
print("Login error: \(error!.localizedDescription)");
}
}
logInButton.loginMethods = [.SystemAccounts]
// TODO: Change where the log in button is positioned in your view
logInButton.center = self.view.center
self.view.addSubview(logInButton)
let store = Twitter.sharedInstance().sessionStore
if let userID = store.session()?.userID {
//store.logOutUserID(userID)
}
}
}