iOS6 の Social.framework で Twitter フォローする
Social.framework を使うことで、OAuth の面倒な手続きを全て iOS 側で行ってくれるので大変便利。
Link Binary with Librariesにて「Social.framework」「Accounts.framework」を追加 。 ヘッダにSocial.hをインポート。
#import <Social/Social.h> #import <Accounts/Accounts.h>
フォローさせる為には、SLRequest を使って API にリクエストを飛ばす必要がある。
参考:https://dev.twitter.com/docs/api/1.1/post/friendships/create
if (NSClassFromString(@"SLRequest") != nil) { ACAccountStore *accountStore = [[ACAccountStore alloc] init]; ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter]; [accountStore requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error) { if (granted) { NSArray *accountArray = [accountStore accountsWithAccountType:accountType]; if (accountArray.count > 0) { NSURL *url = [NSURL URLWithString:@"https://api.twitter.com/1.1/friendships/create.json"]; NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys: @"フォローしたい Twitter アカウント名", @"screen_name", @"true", @"follow", nil]; SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodPOST URL:url parameters:params]; [request setAccount:[accountArray objectAtIndex:0]]; [request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) { NSLog(@"responseData=%@", [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]); }]; } } }]; }
処理としては、登録している Twitter アカウントを配列で取得し、一番最初の物を使って対象ユーザーをフォロー。成功・失敗は responseData を取得して表示。