19
19

More than 5 years have passed since last update.

iOSでSocial.frameworkを使ってFacebookのイイね!をする

Last updated at Posted at 2014-05-06

最初にNative/DesktopアプリとしてFBアプリ登録をしておく。

ライブラリを読み込んで、


#import <Social/Social.h>
#import <Accounts/Accounts.h>

以下が実際に送信する箇所。

ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];

NSDictionary *options = [[NSDictionary alloc] initWithObjectsAndKeys:
                             @"APP_ID", ACFacebookAppIdKey,
                             [NSArray arrayWithObject:@"email"], ACFacebookPermissionsKey, nil];
[accountStore requestAccessToAccountsWithType:accountType options:options completion:^(BOOL granted, NSError *error) {
    if (granted) {

        NSDictionary *options = [[NSDictionary alloc] initWithObjectsAndKeys:
                                     @"APP_ID", ACFacebookAppIdKey,
                                     [NSArray arrayWithObject:@"publish_actions"], ACFacebookPermissionsKey,
                                     ACFacebookAudienceEveryone, ACFacebookAudienceKey,
                                     nil];

        [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];

                        ACAccountCredential *fbCredential = [account credential];
                        NSString *accessToken = [fbCredential oauthToken];

                        NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
                                                accessToken, @"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]);
                        }];
                    }
                }
            }];
        }
    }];

ポイントとしては、
いきなりpublish_streamとか投稿する系のpermissionをとろうとすると、Error Domain=com.apple.accounts..みたいなエラーがでる。
回避するために一旦ベーシックなpermission(emailとか)をとっている。

また、emailなどの場合は、ACFacebookAudienceKeyを指定しなくても取得できるが、投稿系のpermissionはACFacebookAudienceKeyを指定しないと、これまたエラーになるので注意。

19
19
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
19
19