Qiita Conference 2025

Qiita史上最多!豪華12名のゲストが登壇

特別講演ゲスト(敬称略)

ymrl、成瀬允宣、鹿野壮、伊藤淳一、uhyo、徳丸浩、ミノ駆動、みのるん、桜庭洋之、tenntenn、けんちょん、こにふぁー

19
19

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

複数アカウントに対応したカスタムツイートの投稿方法

Last updated at Posted at 2012-12-30

TWTweetComposeViewControllerを使わないやりかた。
基本的にはサンプルコード(Tweeting)に準じています。

ポイント

  • 複数アカウントに対応させるため、ActionSheetなどで場合分けする
  • delegateやBlocks使用時はアカウントを取得し直す

ちなみに、ACAccountStorerequestAccessToAccountsWithType:withCompletionHandler:メソッドはiOS6ではDeprecatedになっています。
iOS6以降のみ対応なら、requestAccessToAccountsWithType:options:completion:を使用するのが望ましいですね。

コード

※ ActionSheetのBlocks対応にはUIAlertView-Blocksを使っています。

viewController.m
- (void)viewDidLoad
{
    // 循環参照避け
    __weak typeof(self) id _self = self;
    
    ACAccountStore* store = [[ACAccountStore alloc] init];
    ACAccountType* type = [store accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
    
    // iOS6ではDeprecated
    [store requestAccessToAccountsWithType:type withCompletionHandler:^(BOOL granted, NSError *error) {
        if (granted) {
            NSArray* accounts = [store accountsWithAccountType:type];
            if ([accounts count] > 0) {
                if ([accounts count] == 1) {
                    [self tweet:0];
                    return;
                }
                // 複数アカウントの場合
                UIActionSheet* sheet = [[UIActionSheet alloc] initWithTitle:@"使用するアカウントを選択してください。" cancelButtonItem:nil destructiveButtonItem:nil otherButtonItems:nil];

                // アカウントの数ぶんボタンを増やす
                for (NSInteger i=0; i<[accounts count] i++) {
                    RIButtonItem* button = [RIButtonItem itemWithLabel:[NSString stringWithFormat:@"@%@", [[accounts objectAtIndex:i] accountDescription]]];
                    [button setAction:^(void){
                        [_self tweet:i];
                    }];
                    [sheet addButtonItem:button];
                }
                [sheet addButtonItem:[RIButtonItem itemWithLabel:@"キャンセル"]];
                [sheet setCancelButtonIndex:[accounts count]];
                [sheet showInView:self.view];
            }
        } else {
            // エラー通達など
        }
    }];
}

- (void)tweet:(NSInteger)index
{
    // アカウントを再取得
    ACAccountStore* store = [[ACAccountStore alloc] init];
    ACAccountType* type = [store accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
    NSArray* accounts = [store accountsWithAccountType:type];

    // リクエストを生成
    TWRequest* req = [[TWRequest alloc] initWithURL:[NSURL URLWithString:@"http://api.twitter.com/1/statuses/update.json"] parameters:@{@"status" : @"テストです。"} requestMethod:TWRequestMethodPOST];

    // アカウントをセット
    [req setAccount:[accounts objectAtIndex:index]];

    // 送信
    [req performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
        if (urlResponse.statusCode == 200) {
            NSLog(@"投稿しました");
        }
        else {
            NSLog(@"投稿できませんでした\nstatus code : %d", urlResponse.statusCode);
        }
    }];
}

19
19
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up

Qiita Conference 2025 will be held!: 4/23(wed) - 4/25(Fri)

Qiita Conference is the largest tech conference in Qiita!

Keynote Speaker

ymrl、Masanobu Naruse, Takeshi Kano, Junichi Ito, uhyo, Hiroshi Tokumaru, MinoDriven, Minorun, Hiroyuki Sakuraba, tenntenn, drken, konifar

View event details
19
19

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?