LoginSignup
19
19

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
19
19