ただ「いいね!」を押してもらうだけのシンプルなサンプルアプリ"Go Like On"を書いた
ずいぶん前に書いたサンプルアプリ。アプリ側からワンタップで「いいね!」までいけるかと聞かれて試しに実装してみた。 クライアント側の実装はすごくシンプルだけど、facebook developer側の設定がちょっとややこしかったのでメモとして残しとく。
Check Account
まず、端末側でfacebookにサインアップしているかをチェックする。このときアプリのアクセス許可を求めるダイアログが表示される。
- (void)checkAccounts { __block __weak ViewController *weakSelf = self; ACAccountType *accountType; ACAccountStore *accountStore = [[ACAccountStore alloc] init]; if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) { accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook]; NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys: @"YOUR_FACABOOK_APP_ID", ACFacebookAppIdKey, [NSArray arrayWithObjects:@"public_actions", @"publish_stream", nil], ACFacebookPermissionsKey, ACFacebookAudienceFriends, ACFacebookAudienceKey, nil]; [accountStore requestAccessToAccountsWithType:accountType options:options completion:^(BOOL granted, NSError *error) { NSLog(@"error:%@",[error description]); NSArray *accountArray = [accountStore accountsWithAccountType:accountType]; NSLog(@"accounts:%@",accountArray); for (ACAccount *account in accountArray) { NSString *text = [NSString stringWithFormat:@"%@",[account username]]; [weakSelf performSelectorOnMainThread:@selector(showFacebookAccount:) withObject:text waitUntilDone:YES]; } }]; } }
アカウントの取得に成功するとアカウント(メールアドレス)が表示され、デカイいいねボタンがアクティブになる。
Facebook developer設定
基本設定
基本設定の中でbundle IDを指定する。
詳細設定
App Type
をNative/Desktop
に、
App Secret in Client
はNo
。
型
ここでちゃんとアクション型として"Like"を指定しておく。
いいねする
Facebookアプリ側の設定が正しく行われていれば、下記のメソッドでちゃんと「いいね」できるようになっているはず!
- (IBAction)onLikeButton:(id)sender { NSDictionary *information = @{@"url":@"http://somtd.hatenablog.com"}; [self postStreakWithInfomation:information]; } - (void)postStreakWithInfomation:(NSDictionary *)information { if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) { ACAccountStore *accountStore = [[ACAccountStore alloc] init]; ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook]; NSDictionary *optionsToRead = @{ ACFacebookAppIdKey: @"YOUR_FACABOOK_APP_ID", ACFacebookPermissionsKey: @[@"basic_info"], }; NSDictionary *optionsToWrite = @{ ACFacebookAppIdKey: @"YOUR_FACABOOK_APP_ID", ACFacebookPermissionsKey: @[@"publish_stream"], ACFacebookAudienceKey : ACFacebookAudienceFriends, }; [accountStore requestAccessToAccountsWithType:accountType options:optionsToRead completion:^(BOOL granted, NSError *error) { [accountStore requestAccessToAccountsWithType:accountType options:optionsToWrite completion:^(BOOL granted, NSError *error) { NSArray *accountArray = [accountStore accountsWithAccountType:accountType]; for (ACAccount *account in accountArray) { NSString *urlString = [NSString stringWithFormat:@"https://graph.facebook.com/%@/og.likes", [[account valueForKey:@"properties"] valueForKey:@"uid"]] ; NSURL *url = [NSURL URLWithString:urlString]; NSDictionary *params = [NSDictionary dictionaryWithObject:information[@"url"] forKey:@"object"]; SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeFacebook requestMethod:SLRequestMethodPOST URL:url parameters:params]; [request setAccount:account]; [request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) { NSLog(@"responseData=%@", [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]); }]; } }]; }]; } }