iOS 5への Twitterアカウントの登録とアプリからのアクセス

iOS 5になって TwitteriOSに統合された。


iPhoneアプリ「ねむスタ」でも iOS 5Twitter機能を使ってツイートできるようにしてみたので、アプリの使い方の一環として「iOS 5に Twitterアカウントを登録する方法」というページを作った。
まだ Twitterアカウントを持っていない場合でも、iOS の「設定」アプリから Twitterアカウントを取得できる辺が「統合」されている感があって良い感じ。「設定」アプリで Twitterアカウントを登録する際に 1度サインインすれば、その後はツイートの度にサインインしたりする必要がないので楽チン。


もちろん、実際のコードの中でも自分で Twitter と認証のプロセスを組む必要はなくて、Accounts.framework がよろしくやってくれる。これ、すっごく楽チン。

- (void)getTwitterAccounts {
	// Create an account store object.
	ACAccountStore *accountStore = [[ACAccountStore alloc] init];
	
	// Create an account type that ensures Twitter accounts are retrieved.
	ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
	
	// Request access from the user to use their Twitter accounts.
	[accountStore requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error) {
		if(granted) {
			// Get the list of Twitter accounts.
			self.accountsArray = [accountStore accountsWithAccountType:accountType];
		}
	}];
}

その代わり、実際にアプリがツイートするには「設定」アプリの Twitterで「Twitter アカウントを使用することが許可されたアプリ」としてスイッチを「オン」にする必要がある。(アプリが Twitterアカウントにアクセスする際に出る、アクセスを許可するかどうか確認するダイアログで「OK」を選択しなくちゃダメ。)