LoginSignup
9
9

More than 5 years have passed since last update.

非同期のURLリクエストをBlocksで書くならSocial.framework使っちゃえ

Last updated at Posted at 2013-06-25

URLRequestの非同期処理といえばNSURLConnectionを使ってdelegateで拾ってあげるのが一般的ですが、実はsocial.frameworkを使えばBlocksで簡単に書くことができます。

 NSString *url = @"http://google.com";
 SLRequest* req=[SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodGET URL:[NSURL URLWithString:url] parameters:nil];
 [req performRequestWithHandler:^(NSData* responseData,
                                          NSHTTPURLResponse* urlResponse,NSError* error) {
        NSString *html= [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
        NSLog(@"%@",html);
 }];

requestForServiceType:の部分はTwitterでもFacebookでもいいと思います。でも入れないと勿論落ちます。
SLRequestのaccountは不要です

Twitterのサービスを使ってないのにTwitterって書くのなんか気持ち悪いからもしかしてコレ用のメソッドちゃんとあったりするのかな…?

9
9
1

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