Twitter アカウントの取得

ざっくりこんな感じで。

- (IBAction)showAccount:(id)sender {
	// 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];
	
	// Start activity indicator while getting account list.
	[activityIndicator startAnimating];
	
	// 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];
			[self.accountTableView reloadData];
		}
		[self.activityIndicator stopAnimating];
	}];
}

これはほぼ前に書いたものと同じ。

アカウントストアを作ってアカウントタイプを ACAccountTypeIdentifierTwitter に指定して、Twitter アカウントのリストを取得している。
前と違うのはアカウント取得の前後でクルクルインジケーターを出したり消したりしてる部分。
それと、CompletionHandler でアカウント取得に成功した場合には、テーブルのデータ配列にアカウントのリストを設定してテーブルを再描画してる。


ここで、iOS 5 の設定次第では取得に失敗する場合がある。
まず、iOS 5 の「設定」->「Twitter」画面の一番下にある「許可:」の項目で、アプリケーションにアカウントの使用を許可してなくてはいけない。

もし、ここで許可が「オフ」の場合、アプリからアカウント取得を試みると許可するかどうかを確認するダイアログが表示される。

ここで許可をすれば問題ないが、許可しなかった場合には上記リストの「if (granted)」が NO になってアカウントの取得に失敗する。