Twitter user id in iOS 5 Twitter user id in iOS 5 ios ios

Twitter user id in iOS 5


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"];


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 properties

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.


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