我正在我的应用程序中实现Twitter登录。我能够使用Fabric登录,一切似乎都很好。现在我希望用户只登录一次,下次他/她打开应用程序时,应该显示一条消息,指出"您已经登录"或类似的东西,并且登录按钮不应该出现在屏幕上。当用户第二次启动应用程序时,我还应该能够获取用户的信息(Twitter用户名),我该如何实现?
这是我到目前为止的代码
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)
}
}
}
您可以使用以下代码它对我有用
if((Twitter.sharedInstance().sessionStore.session()?.userID) == nil)
{
//show twitter login button
}
else
{
//user has already logged in take appropriate action
}
希望对:)有所帮助