その後のその後

iOSエンジニア 堤 修一のブログ github.com/shu223

Social.framework を利用して Twitter の DM を送信する

Social.framework (と Accounts.framework)を利用して Twitter の DM を送信する手順です。

1. フレームワークをプロジェクトに追加

以下の2つを追加し、

  • Social.framework
  • Accounts.framework


DM送信を実装するクラスでヘッダをインポートしておきます。

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

2. Twitterアカウントのアクセス権を取得

Twitterアカウントのアクセス権を取得し、ACAccountオブジェクトを取得します。

ACAccountStore *store = [[ACAccountStore alloc] init];
ACAccountType *type = [store accountTypeWithAccountTypeIdentifier:
                              ACAccountTypeIdentifierTwitter];

[store requestAccessToAccountsWithType:type
                               options:nil
                            completion:^(BOOL granted, NSError *error) {
                                
                                if (!granted) {
                                    
                                    NSLog(@"not granted");
                                    
                                    return;
                                }
                                
                                NSArray *twitterAccounts = [store accountsWithAccountType:type];
                                
                                if (!(twitterAccounts > 0)) {
                                    
                                    NSLog(@"no twitter accounts");
                                    
                                    return;
                                }
                                
                                ACAccount *account = [twitterAccounts lastObject];
                            }];

3. DM 送信

SLRequestクラスを利用して、direct_messages/new APIをたたきます。

NSURL *url = [NSURL URLWithString:@"http://api.twitter.com/1.1/direct_messages/new.json"];
NSDictionary *parameters = @{@"text": @"テスト送信",
                             @"screen_name": @"shu223"};

SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeTwitter
                                        requestMethod:SLRequestMethodPOST
                                                  URL:url
                                           parameters:parameters];

request.account = account;
[request performRequestWithHandler:^(NSData *responseData,
                                     NSHTTPURLResponse *urlResponse,
                                     NSError *error)
 {
     NSError *jsonError = nil;
     id jsonData = [NSJSONSerialization JSONObjectWithData:responseData
                                               options:0
                                                 error:&jsonError];
     if (error){
         
         NSLog(@"error:%@", error);
     }
     
     if (jsonError) {
         
         NSLog(@"jsonerror:%@", jsonError);
     }
     
     dispatch_async(dispatch_get_main_queue(), ^{
         
         NSLog(@"jsondata:%@" , jsonData);
     });
 }];


上のコード内で使用しているパラメータについて補足すると、

  • text
    • 送信するテキスト
    • required
  • screen_name
    • 宛先ユーザーのscreen name(@のあとに続く名前)
    • option

となっています。

補足

iOS 6 より deprecated になりましたが、Twitter.framework の TWRequest を用いても同様のことができます。

TWRequest *request = [[TWRequest alloc] initWithURL:url
                                         parameters:parameters
                                      requestMethod:TWRequestMethodPOST];