強火で進め

このブログではプログラム関連の記事を中心に書いてます。

Apple公式のサンプル「Tweeting」の不具合の修正方法

AppleTwitter を使うサンプルが Twitter API の仕様変更で動かなくっていました。

具体的は「Get Public Timeline」のボタンを押した時に正しく動作しません。

修正方法

TweetingViewController.m の以下の部分が問題となる部分です。

- (IBAction)getPublicTimeline:(id)sender {
	// Create a request, which in this example, grabs the public timeline.
	// This example uses version 1 of the Twitter API.
	// This may need to be changed to whichever version is currently appropriate.
	TWRequest *postRequest = [[TWRequest alloc] initWithURL:[NSURL URLWithString:@"http://api.twitter.com/1/statuses/public_timeline.json"] parameters:nil requestMethod:TWRequestMethodGET];

ここで使用している http://api.twitter.com/1/statuses/public_timeline.json は現在は使えなくなっているので発生したエラーです。

このサンプルではパブリックタイムラインを取得していますがパブリックタイムラインはStreaming APIなので代わりにフォローしてる友人のIDリストを取得する様にこの様に変更しました。

	// Create a request, which in this example, grabs the public timeline.
	// This example uses version 1 of the Twitter API.
	// This may need to be changed to whichever version is currently appropriate.
    NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
                            @"nakamura001", @"screen_name",
                            @"-1", @"cursor",
                            nil];
    TWRequest *postRequest = [[TWRequest alloc] initWithURL:[NSURL URLWithString:@"http://api.twitter.com/1/friends/ids.json"] parameters:params requestMethod:TWRequestMethodGET];

これで「Get Public Timeline」をタップした時にデータを取得できました。まぁ、取得出来るデータは Public Timeline じゃないですがw

関連情報

API requests with TWRequest | Twitter Developers
https://dev.twitter.com/docs/ios/making-api-requests-twrequest