LoginSignup
12
12

More than 5 years have passed since last update.

ただ「いいね!」を押してもらうだけのシンプルなサンプルアプリ"Go Like On"を書いた

Last updated at Posted at 2013-11-22

ずいぶん前に書いたサンプルアプリ。アプリ側からワンタップで「いいね!」までいけるかと聞かれて試しに実装してみた。
クライアント側の実装はすごくシンプルだけど、facebook developer側の設定がちょっとややこしかったのでメモとして残しとく。

GitHub : somtd / GoLikeOn

 2013-11-23 1.19.00.png

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を指定する。

setting_1

詳細設定

App TypeNative/Desktopに、
App Secret in ClientNo

setting_2

ここでちゃんとアクション型として"Like"を指定しておく。
setting_3

いいねする

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]);
                    }];
                }
            }];
        }];
    }
}

リポジトリはこちら(GitHub : somtd / GoLikeOn)から

12
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
12