iOS 5中的Twitter用户ID

Muh*_*.HS 17 iphone twitter objective-c ios

我正在使用以下代码从iOS 5中的Twitter获取用户详细信息.

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

}
Run Code Online (Sandbox Code Playgroud)

在我的NSLog控制台中,我得到如下输出:

Twiter details-- type:com.apple.twitter  
identifier: E8591841-2AE0-4FC3-8ED8-F286BE7A36B0
accountDescription: @Sadoo55
username: sadukoyghdkwed@gmail.com
objectID: x-coredata://F8059811-CFB2-4E20-BD88-F4D06A43EF11/Account/p8
enabledDataclasses: {(
)}
properties: {
  "user_id" = 308905856;
}
parentAccount: (null)
owningBundleID:com.apple.Preferences
Run Code Online (Sandbox Code Playgroud)

我想从中得到"user_id".我怎样才能获取"user_id"(即308905856)?

Ngo*_*yen 20

以下是我在iOS5和iOS6中获取user_id所做的工作.

    NSDictionary *tempDict = [[NSMutableDictionary alloc] initWithDictionary: 
                                     [twitterAccount dictionaryWithValuesForKeys:[NSArray arrayWithObject:@"properties"]]];
    NSString *tempUserID = [[tempDict objectForKey:@"properties"] objectForKey:@"user_id"];
Run Code Online (Sandbox Code Playgroud)

  • 然后不会`[twitterAccount valueForKeyPath:@"properties.user_id"]`在没有中间字典的情况下做同样的事情? (8认同)

jam*_*ack 16

获取用户ID和/或用户名是多少比这更容易.

iOS 7版本

ACAccount *account = accountsArray[0];
NSString *userID = ((NSDictionary*)[account valueForKey:@"properties"])[@"user_id"];
Run Code Online (Sandbox Code Playgroud)

要么

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);
}
Run Code Online (Sandbox Code Playgroud)

iOS 5版本

有一个未记录的ACAccount方法,名为accountProperties,类型为NSDictionary,带有user_id键.

ACAccount属性

ACAccount *twitterAccount = [accountsArray objectAtIndex:0];
NSString *userID = [[twitterAccount accountProperties] objectForKey:@"user_id"];
NSString *username = twitterAccount.username;
Run Code Online (Sandbox Code Playgroud)

**在iOS6下不起作用,因为不再定义该方法.

  • 这个答案是不正确的.由于在问题的调试日志输出中非常明显,标识符与user_id不同. (2认同)

ber*_*tin 14

您可以将帐户对象视为字典并执行以下操作:

ACAccount *account = [twitterAccounts objectAtIndex:0];
NSString *userID = [account valueForKeyPath:@"properties.user_id"];
Run Code Online (Sandbox Code Playgroud)

这将返回user_id