Mac OSでも楽チン ツイート その2 Social.framework

アプリケーションからとにかく簡単にツイートしたい時は、前出のNSSharingServiceを使うのが最も簡単な方法の用だが、タイムラインを取得したり、よりカスタマイズをしたいという場合は、10.8より追加された、Social.frameworkのSLRequestを使用するのが良さそうだ。このクラスは使い勝手としては、iOS5から搭載されたTwitter.frameworkのTWRequestにそっくりで、こちらも非常に楽チンだ。

では、まずはFrameworksに”Social.framework”とAccounts.frameworkを追加する。次にヘッダをそれぞれインポート<Social/Social.h><Accounts/Accounts.h>。以上で準備完了。

つぎにツイートをするにはこんな感じ。

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification{
    ACAccountStore *accountStore = [[ACAccountStore alloc]init];
    ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
    [accountStore requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error) {
        if(granted){
            NSArray *accounts = [accountStore accountsWithAccountType:accountType];

            if (accounts != nil && [accounts count] != 0) {
                ACAccount *twAccount = [accounts objectAtIndex:0];
                NSURL *url = [NSURL URLWithString:@"https://api.twitter.com/1/statuses/update.json"];
                NSDictionary *params = [NSDictionary dictionaryWithObject:@"Hello" forKey:@"status"];
                SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodPOST URL:url parameters:params];
                request.account = twAccount;
                [request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
                    NSString *output = [NSString stringWithFormat:@"HTTP response status: %ld",[urlResponse statusCode]];
                    NSLog(@"%@", output);
                }];
            }
        }
    }];
}

このようにiOSのTWRequestとほぼ同じ。既にシステムに登録済のアカウントを取得して、それを使ってツイートする。このようにTwitter本家で認証した後にツイートする従来の手法に比べ認証を手前で用意する必要がない分、飛躍的に楽チンができる。

次にタイムラインを取得してみる。

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification{
    ACAccountStore *accountStore = [[ACAccountStore alloc]init];
    ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
    [accountStore requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error) {
        if(granted){
            NSArray *accounts = [self.accountStore accountsWithAccountType:accountType];

            if (accounts != nil && [accounts count] != 0) {
                ACAccount *twAccount = [accounts objectAtIndex:0];
                NSURL *url = [NSURL URLWithString:@"http://api.twitter.com/1/statuses/home_timeline.json"];
                NSDictionary *params = [NSDictionary dictionaryWithObject:@"1" forKey:@"include_entities"];
                SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodGET URL:url parameters:params];
                request.account = twAccount;
                [request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
                    if (urlResponse){
                        NSError *jsonError;
                        NSArray *timeline = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves error:&jsonError];
                        if(timeline){
                            NSString *output = [NSString stringWithFormat:@"HTTP response status: %ld",[urlResponse statusCode]];
                            NSLog(@"%@", output);
                            NSLog(@"%@",timeline);
                        }else{
                            NSLog(@"error: %@",jsonError);
                        }
                    }
                }];
            }
        }
    }];
}

これもほぼTWRequestと同じ。

実は、私の作成したMac用アプリ「MeNews」もツイッターのタイムラインの取得機能を持っている。当然これらの楽チン クラスがまだ用意されていない時に開発をしたものだ。手順としては、Twitterにアプリケーションの登録をして、OAuthConsummerを使用した。それに比べると本当に楽チンなのだ。ただ、無料でシステムのアップグレードができるiOSと違って、歴史も長く、ユーザの使用目的が多岐に渡るMac OSは、そう簡単に過去のOSを動作非対応にするわけにはいかないと思う。なのでこれらのクラスを使用するのはまだしばらく時間がかかりそうだ。

SafariからRSSリーダ機能を削除したAppleが、共用ボタンという形でTwitterとの対話性を高めたところに時代の流れを感じる。

Avatar photo

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です