Twitterトレンドをアクションシートに表示する
疲れた。2バイト言語にこれほど苦しめられるとは・・・
今回はTwitterのトレンドを表示してみました。
サンプルはこちら。
JSONを分析したらSearchAPIのときと構成が変わらなかったのでよゆーよゆーと思ってたら詰みましたorz
ワールドワイド(英語)のトレンドはすぐに取り出せたんですが、日本語はなぜかNSStringにしてもエスケープ文字("\U5c0f\U58f2\U308a")で出てくるんです。じゃあそれをエンコードすればいいやと思ったら果てしなく大変でほんとに詰んだと思いました。
トレンドの中身を取り出す場合は
NSMutableArray *trendArray = [[json objectAtIndex:0] objectForKey:@"trends"]; NSString *trendname1 = [[trendArray objectAtIndex:0] objectForKey:@"name"];
になります。objectAtIndexがキモ。
SearchAPIの場合は
NSArray *tweets = [twdictionary objectForKey:@"results"]; //Tweetをひとつずつ取り出して表示する準備をする for (NSDictionary *tweet in tweets) { [tweetTextArray addObject:[tweet objectForKey:@"text"]]; }
でした。
取り出すところのコードが少し違うだけで後はほとんど同じです。
あ、日本語のトレンドを取り出す場合はJSONのアドレスを「https://api.twitter.com/1/trends/23424856.json」にしましょう。
23424856が日本です。1はワールドワイド。
・ViewController.h
@interface ViewController : UIViewController <UIActionSheetDelegate> { //以下追加 //トレンドのリスト NSMutableArray *trendArray; //トレンドのリストの0番目・1番目・2番目・3番目のデータの文字列 NSString *trendname1,*trendname2,*trendname3,*trendname4; } @property (weak, nonatomic) IBOutlet UILabel *trendLabel; //以下追加 @property (nonatomic,retain) NSMutableArray *trendArray; @property (nonatomic,retain) NSString *trendname1,*trendname2,*trendname3,*trendname4; //アクションシートを起動 - (IBAction)trendAction:(id)sender; @end
・ViewController.m
@synthesize trendLabel; //以下追加 @synthesize trendArray; @synthesize trendname1,trendname2,trendname3,trendname4; - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. //トレンドのURLを呼び出す NSURL *twTurl = [NSURL URLWithString:@"https://api.twitter.com/1/trends/23424856.json"]; //設定したアドレスにアクセスする設定をする NSURLRequest *twTrequest = [NSURLRequest requestWithURL:twTurl]; //NSURLConnectionで実際にアクセスする [NSURLConnection sendAsynchronousRequest:twTrequest queue:[NSOperationQueue mainQueue]completionHandler:^(NSURLResponse *twTresponse, NSData *twTdata, NSError *twTerror) { if (twTerror) { NSLog(@"error: %@", [twTerror localizedDescription]); return; } //jsonで解析する。NSJSONSerializationはiOS5から使える NSArray *json = [NSJSONSerialization JSONObjectWithData:twTdata options:kNilOptions error:&twTerror]; //ここSearchAPIと異なるので注意! trendArray = [[json objectAtIndex:0] objectForKey:@"trends"]; NSLog(@"%@",trendArray); //nameキーの上から4つを取り出す trendname1 = [[trendArray objectAtIndex:0] objectForKey:@"name"]; trendname2 = [[trendArray objectAtIndex:1] objectForKey:@"name"]; trendname3 = [[trendArray objectAtIndex:2] objectForKey:@"name"]; trendname4 = [[trendArray objectAtIndex:3] objectForKey:@"name"]; }]; } ... //アクションシートを起動する - (IBAction)trendAction:(id)sender { //タイトルやキャンセルボタンの設定、destructiveButtonTitleはシートのいちばん上のボタンに表示する文字列でotherButtonTitlesは2番目以降 UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"トレンドリスト" delegate:self cancelButtonTitle:@"キャンセル" destructiveButtonTitle:trendname1 otherButtonTitles:trendname2,trendname3,trendname4, nil]; //アクションシートを表示する [actionSheet showInView:self.view]; } //アクションシートのボタンをタップしたときにする処理 -(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { //タップしたボタンに書いてある文字列を表示 trendLabel.text = [actionSheet buttonTitleAtIndex:buttonIndex]; } @end