コードを ASIHTTPRequest から AFNetworking に変更しようとしています。現在、10 ~ 15 の異なる HTTP URL (ファイル) を選択し、それらをドキュメント フォルダーにダウンロードしたいと考えています。
非常に簡単な ASIHTTPRequest を使用すると、
[myQueue setDownloadProgressDelegate:myUIProgressView];
AFNetworking では、その方法がわかりません。ファイルをダウンロードして保存し、ファイルが正常にダウンロードされたときに通知する次のコードがありますが、このキューの進行状況バーを合計サイズで作成できません。
for (i=0; i<3; i++) {
NSString *urlpath = [NSString stringWithFormat:@"http://www.domain.com/file.zip"];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlpath]];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"testFile%i.zip",i]];
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:path append:NO];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Successfully downloaded file to %@", path);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
[operation setDownloadProgressBlock:^(NSInteger bytesWritten, NSInteger totalBytesWritten, NSInteger totalBytesExpectedToWrite) {
NSLog(@"Sent %d of %d bytes, %@", totalBytesWritten, totalBytesExpectedToWrite, path);
}];
[myQueue addOperation:operation];
}