2012-04-13 6 views
3

私は次のコードでTwitterへ投稿することができます:iOS 5のTwitterフレームワークでユーザーのTwitterプロファイル情報を取得するにはどうすればよいですか?

TWTweetComposeViewController *tweeter = [[TWTweetComposeViewController alloc] init]; 
     [tweeter setInitialText:@"message"]; 
     [tweeter addImage:image]; 
     [self presentModalViewController:tweeter animated:YES]; 

にはどうすれは、iOS 5のTwitterのフレームワークとユーザーのTwitterプロフィール情報を得ることができますか?

答えて

3

端末に複数のアカウントが設定されている可能性があります。

// Is Twitter is accessible is there at least one account 
    // setup on the device 
    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]; 
     } 
    }]; 
    } 

http://iosdevelopertips.com/core-services/ios-5-twitter-framework-%E2%80%93-part-3.html

+0

おかげで...しかし、私は、ユーザーのプロファイル情報のJSONとしてレスポンスを必要とする... –

+1

@RahulNair申し訳ありませんが、私の水晶玉は、ウル返事のためにその – AnthonyBlake

6

まあ、することができますが、あなたは、Twitterユーザーが表に自分のデバイス上で持っているアカウントを表示したいと言います。テーブルセルにアバターを表示したい場合は、TwitterのAPIを照会する必要があります。

NSArrayACAccountのオブジェクトであると仮定して、各アカウントの追加のプロファイル情報を格納する辞書を作成できます。

// Assuming that you've dequeued/created a UITableViewCell... 

    // Check to see if we have the profile image of this account 
    UIImage *profileImage = nil; 
    NSDictionary *info = [self.twitterProfileInfos objectForKey:account.identifier]; 
    if (info) profileImage = [info objectForKey:kTwitterProfileImageKey]; 

    if (profileImage) { 
     // You'll probably want some neat code to round the corners of the UIImageView 
     // for the top/bottom cells of a grouped style `UITableView`. 
     cell.imageView.image = profileImage; 

    } else { 
     [self getTwitterProfileImageForAccount:account completion:^ { 
      // Reload this row 
      [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
     }];    
    } 

すべてこれはアカウント識別子をキーと辞書の辞書からUIImageオブジェクトにアクセスして、静的NSStringキーですやっている:あなたのテーブルビューコントローラのtableView:cellForRowAtIndexPath:は、このようないくつかのコードが必要になります。イメージオブジェクトを取得しない場合は、インスタンスメソッドを呼び出し、テーブルローをリロードする完了ハンドラブロックを渡します。インスタンスメソッドビット次のようになります。

#pragma mark - Twitter 

- (void)getTwitterProfileImageForAccount:(ACAccount *)account completion:(void(^)(void))completion { 

    // Create the URL 
    NSURL *url = [NSURL URLWithString:@"users/profile_image" relativeToURL:kTwitterApiRootURL]; 

    // Create the parameters 
    NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys: 
          account.username, @"screen_name", 
          @"bigger", @"size", 
          nil]; 

    // Create a TWRequest to get the the user's profile image 
    TWRequest *request = [[TWRequest alloc] initWithURL:url parameters:params requestMethod:TWRequestMethodGET]; 

    // Execute the request 
    [request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) { 

     // Handle any errors properly, not like this!   
     if (!responseData && error) { 
      abort(); 
     } 

     // We should now have some image data 
     UIImage *profileImg = [UIImage imageWithData:responseData]; 

     // Get or create an info dictionary for this account if one doesn't already exist 
     NSMutableDictionary *info = [self.twitterProfileInfos objectForKey:account.identifier]; 
     if (!info) { 
      info = [NSMutableDictionary dictionary];    
      [self.twitterProfileInfos setObject:info forKey:account.identifier]; 
     } 

     // Set the image in the profile 
     [info setObject:profileImg forKey:kTwitterProfileImageKey]; 

     // Execute our own completion handler 
     if (completion) dispatch_async(dispatch_get_main_queue(), completion); 
    }]; 
} 

だから、あなたが正常に失敗します、しかし、それはプロフィール画像をダウンロードして、テーブルを更新すること。補完ハンドラでは、これらを画像キャッシュに入れたり、クラスの存続期間を超えて永続化したりすることができます。

他のTwitterユーザー情報にアクセスする場合は、同じ手順を使用できます。see their docs

2

上記の方法は事態が複雑になっています。単に使用:ウル返信用

ACAccount *twitterAccount = [accountsArray objectAtIndex:0]; 
NSLog(twitterAccount.accountDescription); 
+0

感謝を教えていませんでした、私はURコードをチェックします....:D –

+0

'accountDescription'はiOS8のtwitterのユーザー名のみを表示します –

関連する問題