gpt4 book ai didi

ios - 使用 Swift 在 IOS 上的 Twitter 单点登录

转载 作者:搜寻专家 更新时间:2023-11-01 07:23:35 24 4
gpt4 key购买 nike

我正在我的应用程序中实现 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
}

希望对您有所帮助:)

关于ios - 使用 Swift 在 IOS 上的 Twitter 单点登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37435459/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com