我几天来一直在搜索如何根据他/她的Twitter帐户获取用户详细信息,我在我的应用程序中使用twitter登录 &安培;我在Swift中没有找到任何关于此的内容,所以我在问! 我怎样才能获得用户名和&电子邮件& uprofile使用Twitter的Parse登录用户的图像,以便将它们保存在解析云上?
答案 0 :(得分:8)
您可以非常轻松地访问登录用户的用户名和用户ID。在大多数Twitter登录方法中,您会看到类似这样的内容:
@IBAction func loginTwitter(sender: UIBarButtonItem) {
Twitter.sharedInstance().logInWithCompletion {
(session, error) -> Void in
if (session != nil) {
print(session?.userName)
print(session?.userID)
} else {
print("error")
}
}
}
据我所知,Twitter不会公开用户的电子邮件地址。
对于个人资料图片,您需要发送GET请求。以下是一些可能与最新的TwitterKit版本不同的代码,但至少应该让您了解应该如何格式化请求。
func getUserInfo(screenName : String){
if let userID = Twitter.sharedInstance().sessionStore.session()!.userID {
let client = TWTRAPIClient(userID: userID)
let url = "https://api.twitter.com/1.1/users/show.json"
let params = ["screen_name": screenName]
var clientError : NSError?
let request = Twitter.sharedInstance().APIClient.URLRequestWithMethod("GET", URL: url, parameters: params, error: &clientError)
client.sendTwitterRequest(request) { (response, data, connectionError) -> Void in
if let someData = data {
do {
let results = try NSJSONSerialization.JSONObjectWithData(someData, options: .AllowFragments) as! NSDictionary
print(results)
} catch {
}
}
}
}
}
您需要查看返回的JSON并找到" profile_image_url_https"一对夫妇降级。
祝你好运!
答案 1 :(得分:0)
在Swift 4.2和Xcode 10.1中
它也在收到电子邮件。
import TwitterKit
@IBAction func onClickTwitterSignin(_ sender: UIButton) {
TWTRTwitter.sharedInstance().logIn { (session, error) in
if (session != nil) {
let name = session?.userName ?? ""
print(name)
print(session?.userID ?? "")
print(session?.authToken ?? "")
print(session?.authTokenSecret ?? "")
let client = TWTRAPIClient.withCurrentUser()
client.requestEmail { email, error in
if (email != nil) {
let recivedEmailID = email ?? ""
print(recivedEmailID)
}else {
print("error--: \(String(describing: error?.localizedDescription))");
}
}
//To get profile image url and screen name
let twitterClient = TWTRAPIClient(userID: session?.userID)
twitterClient.loadUser(withID: session?.userID ?? "") {(user, error) in
print(user?.profileImageURL ?? "")
print(user?.profileImageLargeURL ?? "")
print(user?.screenName ?? "")
}
let storyboard = self.storyboard?.instantiateViewController(withIdentifier: "SVC") as! SecondViewController
self.navigationController?.pushViewController(storyboard, animated: true)
}else {
print("error: \(String(describing: error?.localizedDescription))");
}
}
}
注销
@IBAction func onClickTwitterSignout(_ sender: UIButton) {
let store = TWTRTwitter.sharedInstance().sessionStore
if let userID = store.session()?.userID {
print(store.session()?.userID ?? "")
store.logOutUserID(userID)
print(store.session()?.userID ?? "")
self.navigationController?.popToRootViewController(animated: true)
}
}