iOS6 の Social.framework で Facebook いいねする
まずは Facebook DEVELOPERS よりアプリを作成する
https://developers.facebook.com/
作成後、設定 -> 詳細設定に移り、「App Type」を「Native/Desktop」に、「App Secret in Client」を「いいえ」にする。
オープングラフの編集より、「Like」を選択
作成すると、「Get Code」よりアクセストークンや送信する各値について書かれているダイアログが出るのでメモっておく。
Link Binary with Librariesにて「Social.framework」「Accounts.framework」を追加 。 ヘッダにSocial.hをインポート。
#import <Social/Social.h> #import <Accounts/Accounts.h>
後は iOS に設定している Facebook アカウントを取得し、SLRequest を使って Graph API を叩けば「いいね」出来る。
ACAccountStore *accountStore = [[ACAccountStore alloc] init]; ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook]; NSDictionary *options = @{ ACFacebookAppIdKey : @"アプリ ID", ACFacebookPermissionsKey : @[@"email"] }; [accountStore requestAccessToAccountsWithType:accountType options:options completion:^(BOOL granted, NSError *error) { if (granted) { NSArray *accounts = [accountStore accountsWithAccountType:accountType]; if ([accounts count] > 0) { ACAccount *account = [accounts objectAtIndex:0]; NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys: @"Get Code で取得した Access Token", @"access_token", @"いいねする URL", @"object", nil]; SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeFacebook requestMethod:SLRequestMethodPOST URL:[NSURL URLWithString:@"https://graph.facebook.com/me/og.likes"] parameters:params]; [request setAccount:account]; [request performRequestWithHandler:^(NSData* responseData, NSHTTPURLResponse* urlResponse, NSError* error) { NSLog(@"%@", [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]); }]; } } }];