使用 iOS 社交/帐户框架检索 Twitter 用户数据 [英] Retrieving twitter user data with iOS Social / Accounts frameworks
本文介绍了使用 iOS 社交/帐户框架检索 Twitter 用户数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法使用 iOS 社交/帐户框架从 Twitter 获取用户数据(名字、姓氏和电子邮件)?我可以在 Facebook 上做到这一点,但我向 Twitter 发出的每个 SLRequest 都会返回一个空数组.
Is there a way to get user data (first name, last name, and email) from Twitter using the iOS social/accounts frameworks? I'm able to do it with Facebook, but every SLRequest I make to Twitter returns an empty array.
这是我现在得到的代码.我已经尝试了几个具有不同参数的 URL,但我没有任何运气.
Here's the code I've got right now. I've tried several URLS with varying parameters, but I haven't had any luck.
- (void)populateTwitterAccount {
NSURL *twitterURL = [NSURL URLWithString:@"https://api.twitbridge.com/1.1/users/show.json"];
SLRequest *twitterRequest = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodGET URL:twitterURL parameters:nil];
[twitterRequest setAccount:self.twitterAccount];
[twitterRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
NSString *accountDataString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(@"%@", accountDataString);
}];
}
推荐答案
是的,可以使用ACAccountStore
获取用户信息,您必须保留 ACAccountStore:.h
Yes, you can get user information using ACAccountStore
,
You have to retain ACAccountStore:
.h
@property (nonatomic, strong) ACAccountStore *account;
.m
NSUrl *url = [NSURL URLWithString:@"https://api.twitter.com/1.1/users/show.json"];
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:twittername,@"screen_name",nil];
account = [[ACAccountStore alloc] init];
ACAccountType *twitterAccountType = [account accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
NSArray *twitterAccounts = [account accountsWithAccountType:twitterAccountType];
// Runing on iOS 6
if (NSClassFromString(@"SLComposeViewController") && [SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
{
[account requestAccessToAccountsWithType:twitterAccountType options:NULL completion:^(BOOL granted, NSError *error)
{
if (granted)
{
SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodGET URL:url parameters:params];
[request setAccount:[twitterAccounts lastObject]];
dispatch_async(dispatch_get_main_queue(), ^
{
[NSURLConnection sendAsynchronousRequest:request.preparedURLRequest queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response1, NSData *data, NSError *error)
{
dispatch_async(dispatch_get_main_queue(), ^
{
if (data)
{
// [self loadData:data];
NSString* newStr = [[NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding];
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] ;
NSLog(@"data:%@",newStr);
}
});
}];
});
}
}];
}
else if (NSClassFromString(@"TWTweetComposeViewController") && [TWTweetComposeViewController canSendTweet]) // Runing on iOS 5
{
[account requestAccessToAccountsWithType:twitterAccountType withCompletionHandler:^(BOOL granted, NSError *error)
{
if (granted)
{
TWRequest *request = [[TWRequest alloc] initWithURL:url parameters:params requestMethod:TWRequestMethodGET];
[request setAccount:[twitterAccounts lastObject]];
dispatch_async(dispatch_get_main_queue(), ^
{
[NSURLConnection sendAsynchronousRequest:request.signedURLRequest queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response1, NSData *data, NSError *error)
{
dispatch_async(dispatch_get_main_queue(), ^
{
if (data)
{
NSString* newStr = [[NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding];
NSLog(@"data:%@",newStr); }
});
}];
});
}
}];
}
}
这篇关于使用 iOS 社交/帐户框架检索 Twitter 用户数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文