Twitter user id in iOS 5
10,325
Solution 1
Getting the user ID &/or username is much easier than that.
iOS 7 version
ACAccount *account = accountsArray[0];
NSString *userID = ((NSDictionary*)[account valueForKey:@"properties"])[@"user_id"];
or
ACAccount *twitterAccount = accountsArray[0];
NSRange range = [account.description rangeOfString:@" [0-9]{7,8}"
options:NSRegularExpressionSearch];
if (range.location != NSNotFound) {
NSString *userID = [[account.description substringWithRange:range] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSLog(@"User ID: %@", userID);
}
iOS 5 version
There was an undocumented method of ACAccount called accountProperties of type NSDictionary with a user_id key.
ACAccount *twitterAccount = [accountsArray objectAtIndex:0];
NSString *userID = [[twitterAccount accountProperties] objectForKey:@"user_id"];
NSString *username = twitterAccount.username;
**Does not work under iOS6 because the method is no longer defined.
Solution 2
Here's what I did to get the user_id in iOS5 and iOS6.
NSDictionary *tempDict = [[NSMutableDictionary alloc] initWithDictionary:
[twitterAccount dictionaryWithValuesForKeys:[NSArray arrayWithObject:@"properties"]]];
NSString *tempUserID = [[tempDict objectForKey:@"properties"] objectForKey:@"user_id"];
Solution 3
You can think account object like a dictionary and perform this:
ACAccount *account = [twitterAccounts objectAtIndex:0];
NSString *userID = [account valueForKeyPath:@"properties.user_id"];
This will return user_id
Related videos on Youtube
Comments
-
Muhammed Sadiq.HS almost 2 years
I'm using the following code to get the user details from Twitter in iOS 5.
if ([TWTweetComposeViewController canSendTweet]) { // Create account store, followed by a Twitter account identifer account = [[ACAccountStore alloc] init]; ACAccountType *accountType = [account accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter]; // Request access from the user to use their Twitter accounts. [account requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error) { // Did user allow us access? if (granted == YES) { // Populate array with all available Twitter accounts arrayOfAccounts = [account accountsWithAccountType:accountType]; [arrayOfAccounts retain]; // Populate the tableview if ([arrayOfAccounts count] > 0) [self performSelectorOnMainThread:@selector(updateTableview) withObject:NULL waitUntilDone:NO]; } }]; } // -(void)updateTableview { numberOfTwitterAccounts = [arrayOfAccounts count]; NSLog(@"Twiter details-- %@",[arrayOfAccounts objectAtIndex:0]); }
In my
NSLog
console, I am getting the output as follows:Twiter details-- type:com.apple.twitter identifier: E8591841-2AE0-4FC3-8ED8-F286BE7A36B0 accountDescription: @Sadoo55 username: [email protected] objectID: x-coredata://F8059811-CFB2-4E20-BD88-F4D06A43EF11/Account/p8 enabledDataclasses: {( )} properties: { "user_id" = 308905856; } parentAccount: (null) owningBundleID:com.apple.Preferences
I want to get the "user_id" from this. How can I fetch "user_id" (ie. 308905856)?
-
DarkDust about 12 yearsOh come on, this is your 19th question, you should know how to format your question by now.
-
-
aksani56 about 12 yearsI dont think twitter gives you the flexibility for getting user-id for twitter. Check this: dev.twitter.com/docs/ios/using-reverse-auth . See this also in the same context with regard to your question : stackoverflow.com/questions/8543015/…. As far as i came across the posts online i would say one word we can't access user-id, twitter doesn't give flexibility for its inside API. Think you can't get that :(
-
aksani56 about 12 yearsIts giving a JSON response. Extract it in some NSDictionary and get the[dictionary valueForKey:@"properties"]valueForKey:@"user_id"];
-
aksani56 about 12 years
-
UIAdam about 12 yearsSince she is using iOS 5, there is built-in JSON support in the NSJSONSerialization class.
-
Gurpartap Singh about 12 yearsThis answer is incorrect. As it is very apparent in the debug log output in the question, identifier is not the same as user_id.
-
Muhammed Sadiq.HS about 12 years@cirrosstrauts The Updated answer is correct. But am getting a warning that " instance method '-accountProperties' not found (return type defaults to 'id') " Any way thanks to ur answer..
-
runmad almost 12 yearsThere is no such property "accountProperty"
-
Tommy over 11 yearsThen wouldn't
[twitterAccount valueForKeyPath:@"properties.user_id"]
do the same thing without an intermediate dictionary?