Reputation: 16051
I wanted to find the username of the app user's twitter handle, so used this code:
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
[accountStore requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error) {
if (granted) {
NSArray *accounts = [accountStore accountsWithAccountType:accountType];
And then got the username property from each of the ACAccounts returned. I discovered that if you change your twitter username though, iOS still works with your new username, but keeps your old username in it's records, so returning the username property actually returns your old username, which you can't then display or use to match against anything.
You can get the user ID, by doing this:
NSDictionary *tempDict = [[NSMutableDictionary alloc] initWithDictionary:
[twitterAccount dictionaryWithValuesForKeys:[NSArray arrayWithObject:@"properties"]]];
NSString *tempUserID = [[tempDict objectForKey:@"properties"] objectForKey:@"user_id"];
I was wondering how I could use that User ID to find out their actual username?
Upvotes: 1
Views: 2835
Reputation: 8516
Get Twitter account information(Username, Full name etc) using Accounts framework
- (void)getTwitterAccountInformation
{
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
[accountStore requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error) {
if(granted) {
NSArray *accountsArray = [accountStore accountsWithAccountType:accountType];
if ([accountsArray count] > 0) {
ACAccount *twitterAccount = [accountsArray objectAtIndex:0];
NSLog(@"%@",twitterAccount.username);
NSDictionary *tempDict = [[NSMutableDictionary alloc] initWithDictionary:
[twitterAccount dictionaryWithValuesForKeys:[NSArray arrayWithObject:@"properties"]]];
NSString *name = [[tempDict objectForKey:@"properties"] objectForKey:@"fullName"];
}
}
}];
}
Upvotes: 0
Reputation: 181
Thomas Verbeek's answer is right but deprecated for ios 7.
Instead of:
TWRequest *request = [[TWRequest alloc] initWithURL:url parameters:params requestMethod:TWRequestMethodGET];
Use:
SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodGET URL:url parameters:params];
Also make sure you imported the Social.framework to be able to use it.
Upvotes: 1
Reputation: 2431
Getting a Twitter username for a user id is actually a lot more laborious than it ought to be. The Facebook SDK makes a similar process a lot easier (never thought I'd ever say that...).
Regardless, to make Twitter information requests, you need to attach your chosen account from the Account store to a TWRequest:
NSURL *url = [NSURL URLWithString:@"https://api.twitter.com/1.1/users/show.json"];
NSMutableDictionary *params = [NSMutableDictionary new];
[params setObject:tempUserID forKey:@"user_id"];
[params setObject:@"0" forKey:@"include_rts"]; // don't include retweets
[params setObject:@"1" forKey:@"trim_user"]; // trim the user information
[params setObject:@"1" forKey:@"count"]; // i don't even know what this does but it does something useful
TWRequest *request = [[TWRequest alloc] initWithURL:url parameters:params requestMethod:TWRequestMethodGET];
// Attach an account to the request
[request setAccount:twitterAccount]; // this can be any Twitter account obtained from the Account store
[request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
if (responseData) {
NSDictionary *twitterData = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingAllowFragments error:NULL];
NSLog(@"received Twitter data: %@", twitterData);
// to do something useful with this data:
NSString *screen_name = [twitterData objectForKey:@"screen_name"]; // the screen name you were after
dispatch_async(dispatch_get_main_queue(), ^{
// update your UI in here
twitterScreenNameLabel.text = screen_name;
});
// A handy bonus tip: twitter display picture
NSString *profileImageUrl = [twitterData objectForKey:@"profile_image_url"];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:profileImageUrl]];
UIImage *image = [UIImage imageWithData:imageData]; // the matching profile image
dispatch_async(dispatch_get_main_queue(), ^{
// assign it to an imageview in your UI here
twitterProfileImageView.image = image;
});
});
}else{
NSLog(@"Error while downloading Twitter user data: %@", error);
}
}];
Notice how I wrap interface updating things in asynchronous blocks. This is to ensure the interface doesn't freeze up. I also threw in a bonus tip for retrieving the user's profile image, were you so inclined.
Upvotes: 3
Reputation: 442
Here you go: twitterAccout.username should return the actual username... Not tested but I am sure almost sure it will work !
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
[accountStore requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error) {
if(granted) {
NSArray *accountsArray = [accountStore accountsWithAccountType:accountType];
if ([accountsArray count] > 0) {
ACAccount *twitterAccount = [accountsArray objectAtIndex:0];
NSLog(@"%@",twitterAccount.username);
NSLog(@"%@",twitterAccount.accountType);
}
}
}];
Upvotes: 4