2009-10-09
■[iPhone] ASIHTTPRequestを使って簡単非同期通信
CFNetwork APIのラッパーフレームワークであるASIHTTPRequestを使うと、iPhoneアプリの非同期通信が簡単に実現出来るので、その利用方法を以下にメモしておく。
[設定]
1. ASIHTTPRequestのホームページのリンク(Download the latest version)からASIHTTPRequestの最新ソースコードをダウンロードする
2. 必要なファイルをiPhoneアプリのプロジェクトにコピーする
最低限必要なファイルはClassesフォルダ配下の12ファイル(ASIInputStream.h、ASIInputStream.m、ASIHTTPRequest.h、ASIHTTPRequest.m、ASINSStringAdditions.h、ASINSStringAdditions.m、ASIFormDataRequest.h、ASIFormDataRequest.m、ASINetworkQueue.h、ASINetworkQueue.m、ASIAuthenticationDialog.h、ASIAuthenticationDialog.m)とExternalフォルダ配下の2ファイル(Reachability.h、Reachability.m)
3. iPhoneアプリのプロジェクトにフレームワークCFNetwork、SystemConfigurationを追加する
フレームワーク追加時には既追加済みフレームワーク(UIKit.framework等)と同じiPhone SDKバージョンの物を追加するように気をつける。
4. iPhoneアプリのプロジェクトに動的ライブラリlibz.1.2.3.dylibを追加する
注:記事を書いた当時と設定が若干変わっているようです。最新の設定方法は以下のサイトを参考にして下さい。
[使い方]
例えば特定のWebサイトの情報を取って来る場合は以下のようなコードになる。
これだけのコードで非同期にWebサイトの情報を取って来る事が出来る。
#import "ASIHTTPRequest.h" - (void)grabURLInBackground { NSURL *url = [NSURL URLWithString:@"http://d.hatena.ne.jp/tomute/"]; ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; [request setDelegate:self]; [request startAsynchronous]; } - (void)requestFinished:(ASIHTTPRequest *)request { NSString *responseString = [request responseString]; NSLog(@"%@", responseString); // バイナリデータを取得したい場合は以下 // NSData *responseData = [request responseData]; } - (void)requestFailed:(ASIHTTPRequest *)request { NSError *error = [request error]; NSLog(@"%@", [error localizedDescription]); }
上記のrequestはグローバルなNSOperationQueueに格納され、同時に実行されるrequestの数は最大4という制限がある。また複数のrequestの進捗具合などを知りたい場合等、より複雑な処理を行う場合には、自分自身でNSOperationQueueを作成する方が良い。その場合には以下のようなコードとなる。
#import "ASIHTTPRequest.h" - (void)grabURLInBackground { queue = [[NSOperationQueue alloc] init]; NSURL *url = [NSURL URLWithString:@"http://d.hatena.ne.jp/tomute/"]; ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; [request setDelegate:self]; [request setDidFinishSelector:@selector(requestDone:)]; [request setDidFailSelector:@selector(requestWentWrong:)]; [queue addOperation:request]; } - (void)requestDone:(ASIHTTPRequest *)request { NSString *response = [request responseString]; NSLog(@"%@", response); } - (void)requestWentWrong:(ASIHTTPRequest *)request { NSError *error = [request error]; NSLog(@"%@", [error localizedDescription]); }
更に詳しい使い方に関しては「ASIHTTPRequest example code - All-Seeing Interactive」に記載されている。
- X01HTの日記W-ZERO3とiPhoneも - iPhoneアプリの非同期通信が簡単に...
- Tomute’s Notes - 本ブログのiPhoneアプリ開発関連メモの纏め
- Tomute’s Notes - iPhoneアプリからTwitpicに画像をアップロード...
- Tomute’s Notes - iPhoneアプリからTwitpicに画像をアップロード...
- もとまか日記:開発向け(仮) - 画像取得で「Corrupt JPEG data: ...
- オレが iPhone 持ってドヤ顔するまで - うおおおおお
- Veritas Operation - XMLの非同期読み込みにトライ
- 1284 http://www.google.co.jp/url?sa=t&rct=j&q=asihttprequest&source=web&cd=4&ved=0CEQQFjAD&url=http://d.hatena.ne.jp/tomute/20091009/1255153469&ei=KDKETqzEGo3qmAWrv-Ek&usg=AFQjCNF0vT83HvCQlr1BRKmQvTPduqEhjQ
- 816 https://www.google.co.jp/
- 797 http://www.google.co.jp/url?sa=t&rct=j&q=&esrc=s&source=web&cd=2&cts=1331020638745&ved=0CDEQFjAB&url=http://d.hatena.ne.jp/tomute/20091009/1255142057&ei=TcNVT8OTMI_LmAXzh9T8CQ&usg=AFQjCNEtfTNi3U4NkRrFM8Pb9lXNuoQ4kA&sig2=GoNOmgk3vapJoI2TiEmA4A
- 449 http://www.google.co.jp/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&ved=0CDAQFjAA&url=http://d.hatena.ne.jp/tomute/20091009/1255153469&ctbs=lr:lang_1ja&ei=M1IfT9_yKu_GmQW19KWzDg&usg=AFQjCNF0vT83HvCQlr1BRKmQvTPduqEhjQ&sig2=RuPRDQFk5uxMHwgXOqUPxw
- 334 http://d.hatena.ne.jp/ch3cooh393/20101029/1288347365
- 308 http://iphone.longearth.net/2010/08/05/twitpicのoauthecho対応/
- 266 http://www.google.co.jp/url?sa=t&rct=j&q=&esrc=s&source=web&cd=3&cts=1331028160225&sqi=2&ved=0CEQQFjAC&url=http://d.hatena.ne.jp/tomute/20091009/1255153469&ei=u-BVT8zEO46OmQWvsqnYCQ&usg=AFQjCNF0vT83HvCQlr1BRKmQvTPduqEhjQ
- 256 http://www.google.co.jp/search?client=safari&rls=en&q=ASIHTTPRequest&ie=UTF-8&oe=UTF-8&redir_esc=&ei=vm-aS_G_DMqGkAXik9XYAQ
- 226 http://d.hatena.ne.jp/wiitomo/20091011/p8
- 219 http://blog.quq.jp/2010/06/objectivec_links/


