2013/07/17

[Twitter][iOS][Objective-C]user_idとusernameを取得する

前回、認証の方法をさらっと書きましたが、今回は、usernameとidの取得方法について。

#import <Accounts/Accounts.h>
ACAccountStore *aCAccountStore = [[ACAccountStore alloc] init];
ACAccountType *aCAccountType = [aCAccountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
[aCAccountStore requestAccessToAccountsWithType:aCAccountType withCompletionHandler:^(BOOL granted, NSError *error) {
    if (granted) {
        NSArray *account = [aCAccountStore accountsWithAccountType:aCAccountType];
        NSUInteger count = [account count];
        for (NSUInteger i=0; i<count; i++) {
            ACAccount *acAccount = [account objectAtIndex:i];
            NSLog(@"twitter id is %@",[acAccount valueForKeyPath:@"properties.user_id"]);
            NSLog(@"username is %@",acAccount.username);             
        }
        
    }else{
        NSLog(@"not grandted");
    }

}];
usernameはACAccountに記述されていたから問題はなかったのですが、user_idは迷いました。

いくつか検索してみて、ACAccount Twitterのuser_idを取得する

NSString *idStr = [self valueForKeyPath:@"properties.user_id"];
という記述が書いてあったので、NSObjectクラスに標準装備されているvalueForKeyPathメソッドを使って取得することができましたー。

一度、こうしてまとめておけば次回以降は困ることがないので、いいですね。

0 コメント:

コメントを投稿