これは標準パターン非同期ダウンローダです:
.hファイルで:
NSMutableData *responseData;
そして.Mファイル:
-(void) execute {
NSString *urlString = @"http://www.google.com";
responseData = [[NSMutableData data] retain];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
}
-(void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
[responseData setLength:0];
}
-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
[responseData appendData:data];
}
-(void) connectionDidFinishLoading:(NSURLConnection *)connection{
[connection release];
NSString *data = [[[NSString alloc] initWithData:responseData encoding:NSASCIIStringEncoding] autorelease];
NSLog(@"%@", data);
[responseData release];
}
これはGoogleにダウンロードしたコンテンツをoutprintます。
出典
2012-04-25 19:54:49
Nic
ありがとうございます! –