16

男、これに困惑した。AFNetworkingでファイルをダウンロードしようとしていますが、私ができる最善の方法は、downloadProgressBlock で進行状況のバイトをキャプチャすることです。私は AFNetworking のさまざまなフォークを試しましたが、今は最新のビルドに戻っています。NSURLResponse も含めるように AFHTTPRequestOperation が変更されたように見えますが、最新のリリースでは削除されています。そして、以下のコードによると、「成功」ブロックは呼び出されません。ダウンロードされたバイトのログを取得し、^complete が呼び出されます。success と error は呼び出されません。

これに関するガイダンスは素晴らしいでしょう。データが返される場所と、それを取得して NSFileManager を使用する方法がわかりませんか? 画像などのストリームを書き込むのではなく、ファイルをダウンロードする必要があります。

編集:ドキュメントで提案されているように - (void)connection:didReceiveData をオーバーライドしてデータをキャプチャしようとしましたが、何もしていません。

// URL はhttp://mydomain.com/somezip.zipです

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", url]]];

AFHTTPRequestOperation *operation = [AFHTTPRequestOperation HTTPRequestOperationWithRequest:request success:^(id object) {

    NSLog(@"hey, some success!");

} failure:^(NSHTTPURLResponse *response, NSError *error) {

    NSLog(@"%@", error);

}];


[operation setDownloadProgressBlock:^(NSInteger bytesRead, NSInteger totalBytesRead, NSInteger totalBytesExpectedToRead) {

    // callback is sent in and works great.  Just outputting to the console at the moment
    SEL callme = NSSelectorFromString(callback);
    [sender performSelectorOnMainThread:callme withObject:[NSNumber numberWithInt:bytesRead] waitUntilDone:NO];

}];

[operation setCompletionBlock:^{
    NSLog(@"operation complete");
}];

NSOperationQueue *queue = [[[NSOperationQueue alloc] init] autorelease];
[queue addOperation:operation];
4

1 に答える 1

64

AFNetworking に含まれているこの機能を使用するだけで、ディスクに保存することもできますoperation.outputStream

NSMutableURLRequest* rq = [api requestWithMethod:@"GET" path:@"YOUR/URL/TO/FILE" parameters:nil];
AFHTTPRequestOperation *operation = [[[AFHTTPRequestOperation alloc] initWithRequest:rq] autorelease];

NSString* path=[@"/PATH/TO/APP" stringByAppendingPathComponent: imageNameToDisk];
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:path append:NO];

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

    NSLog(@"SUCCCESSFULL IMG RETRIEVE to %@!",path)

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

    // Deal with failure
}];

編集missed

[operation start]; 

編集...or if you use an AFHTTPClient * apiClient :

// [apiClient enqueueHTTPRequestOperation:operation];
于 2012-06-26T12:05:49.277 に答える