[英]How can i get logged user details (image url ,username , email , etc.) with Twitter Kit in iOS
[英]Get user profile details (especially email address) from Twitter in iOS
我的目標是根據他/她的Twitter帳戶獲取用戶的詳細信息。 首先,讓我解釋一下我想做什么。
在我的情況下,將向用戶顯示使用Twitter帳戶注冊的選項。 因此,基於用戶的Twitter帳戶,我希望能夠獲取用戶詳細信息(例如電子郵件ID,姓名,個人資料圖片,出生日期,性別等)並將這些詳細信息保存在數據庫中。 現在,很多人可能會建議我使用ACAccount
和ACAccountStore
,這是一個提供訪問,操作和存儲帳戶的界面的類。 但在我的情況下,即使用戶尚未在iOS設置應用中為Twitter配置帳戶,我也想要注冊用戶。 我希望用戶導航到Twitter的登錄屏幕(在Safari或App本身,或使用任何其他替代方案)。
我也提到Twitter的API有列表的文檔在這里 。 但我很困惑應該如何向用戶提供登錄屏幕以登錄Twitter帳戶以及如何獲取個人資料信息。 我應該使用UIWebView
,還是將用戶重定向到Safari或采用其他方式?
最后,在與sdk-feedback@twitter.com
進行了長時間的對話sdk-feedback@twitter.com
,我將我的應用程序列入白名單。 這是故事:
發送郵件至sdk-feedback@twitter.com
,其中包含有關您的應用程序的一些詳細信息,如消費者密鑰,應用程序的App Store鏈接,隱私政策鏈接,元數據,如何登錄我們的應用程序的說明。 在郵件中提到您要訪問應用程序內的用戶電子郵件地址。
他們將審核您的應用程序並在2-3個工作日內回復您。
一旦他們說您的應用程序被列入白名單,請在Twitter開發人員門戶中更新您的應用程序設置。 登錄apps.twitter.com並:
同意Vizllx的聲明: “Twitter 為此 提供了一個漂亮的框架,你只需要將它集成到你的應用程序中。”
獲取用戶郵箱地址
-(void)requestUserEmail
{
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)
}
}
獲取用戶資料
-(void)usersShow:(NSString *)userID
{
NSString *statusesShowEndpoint = @"https://api.twitter.com/1.1/users/show.json";
NSDictionary *params = @{@"user_id": userID};
NSError *clientError;
NSURLRequest *request = [[[Twitter sharedInstance] APIClient]
URLRequestWithMethod:@"GET"
URL:statusesShowEndpoint
parameters:params
error:&clientError];
if (request) {
[[[Twitter sharedInstance] APIClient]
sendTwitterRequest:request
completion:^(NSURLResponse *response,
NSData *data,
NSError *connectionError) {
if (data) {
// handle the response data e.g.
NSError *jsonError;
NSDictionary *json = [NSJSONSerialization
JSONObjectWithData:data
options:0
error:&jsonError];
NSLog(@"%@",[json description]);
}
else {
NSLog(@"Error code: %ld | Error description: %@", (long)[connectionError code], [connectionError localizedDescription]);
}
}];
}
else {
NSLog(@"Error: %@", clientError);
}
}
希望能幫助到你 !!!
在Twitter中,您只能獲得user_name
和user_id
。 您無法獲取email id
, birth date
, gender
等,這是非常安全的。與Facebook相比,Twitter對提供數據非常保密。
需要參考: link1 。
Twitter為此提供了一個漂亮的框架,您只需將其集成到您的應用程序中即可。
https://dev.twitter.com/twitter-kit/ios
它有一個簡單的登錄方法: -
// Objective-C
TWTRLogInButton* logInButton = [TWTRLogInButton
buttonWithLogInCompletion:
^(TWTRSession* session, NSError* error) {
if (session) {
NSLog(@"signed in as %@", [session userName]);
} else {
NSLog(@"error: %@", [error localizedDescription]);
}
}];
logInButton.center = self.view.center;
[self.view addSubview:logInButton];
這是獲取用戶個人資料信息的過程: -
/* Get user info */
[[[Twitter sharedInstance] APIClient] loadUserWithID:[session userID]
completion:^(TWTRUser *user,
NSError *error)
{
// handle the response or error
if (![error isEqual:nil]) {
NSLog(@"Twitter info -> user = %@ ",user);
NSString *urlString = [[NSString alloc]initWithString:user.profileImageLargeURL];
NSURL *url = [[NSURL alloc]initWithString:urlString];
NSData *pullTwitterPP = [[NSData alloc]initWithContentsOfURL:url];
UIImage *profImage = [UIImage imageWithData:pullTwitterPP];
} else {
NSLog(@"Twitter error getting profile : %@", [error localizedDescription]);
}
}];
我認為您可以從Twitter Kit Tutorial中找到休息,它還允許通過調用TwitterAuthClient#requestEmail方法請求用戶的電子郵件,傳入有效的TwitterSession和Callback。
在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))");
}
}
}
按照Harshil Kotecha的回答。
第1步:到https://apps.twitter.com/app/
第2步:點擊你的應用>點擊權限標簽。
第3步:在這里查看郵箱
如果要退出
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)
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.