sendCustomTweet してみる

アカウントを取得したら後はツイートするだけ。
ざっくりこんな感じで。

- (IBAction)tweet:(id)sender {
	ACAccountStore *accountStore = [[ACAccountStore alloc] init];
	ACAccount *twitterAccount = [accountStore accountWithIdentifier:self.userID];

	// Prepare tweet message.
	NSDate *now = [NSDate date];
	NSString *tweetMessage = [NSString stringWithFormat:@"Hello!\n%@", [now description]];

	// Create a request, which in this example, posts a tweet to the user's timeline.
	// This example uses version 1 of the Twitter API.
	// This may need to be changed to whichever version is currently appropriate.
	TWRequest *postRequest = [[TWRequest alloc] initWithURL:[NSURL URLWithString:@"http://api.twitter.com/1/statuses/update.json"]
						parameters:[NSDictionary dictionaryWithObject:tweetMessage forKey:@"status"]
						requestMethod:TWRequestMethodPOST];
	
	// Set the account used to post the tweet.
	[postRequest setAccount:twitterAccount];
	
	// Start activity indicator.
	[activityIndicator startAnimating];
	
	// Perform the request created above and create a handler block to handle the response.
	[postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
		self.responseLabel.text = [NSString stringWithFormat:@"HTTP response status: %i", [urlResponse statusCode]];
		[self.activityIndicator stopAnimating];
	}];

}

最初の 2行でアカウントストアから「accountWithIdentifier」を使って Tweet するアカウントを取得してる。
「accountWithIdentifier」で使っている self.userID は、self.accountTableView でアカウントを選択した時にアカウントの identifier を入れておいたプロパティ。
アカウントはこうやって identifier プロパティを使って取得できるので、identifier を保存しておけば次回アプリ起動時にも前回選択したアカウントを取得できる。


アカウント取得以降は TWRequest を作って、ツイートするアカウントをセットしてツイートするだけ。
肝は TWRequest を作る部分だと思うけど、ここは iOS 5 関係なく Twitter API の使い方なのでググれば使い方がたくさん出てくると思う。
(でも parameters の NSDictionary 作るのが面倒なんだろうな…)


とりあえず、ツイートするだけならこれで OK!
iOS 5Twitter は本当に楽チンになった!