2011-11-04 10 views
16

Hombre, perplejo en este caso. Tratando de descargar un archivo con AFNetworking y lo mejor que puedo hacer es capturar los bytes de progreso en downloadProgressBlock. He intentado muchas diferentes formas de AFNetworking y ahora vuelvo a la última versión. Parece que en un momento se modificó AFHTTPRequestOperation para que también incluyera NSURLResponse, pero eso desapareció en la última versión. Y, según el siguiente código, nunca se llama al bloque "success". Obtengo un registro de los bytes descargados y luego se llama^complete. el éxito y el error nunca se llaman.AFNetworking - ¿Cómo utilizar AFHTTPRequestOperation para descargar el archivo?

Cualquier orientación sobre esto sería increíble. No puedo averiguar dónde se devuelven los datos y cómo hacer que use NSFileManager en él. Necesito descargar archivos, no escribir secuencias para imágenes, etc.

Editar: También traté de capturar los datos anulando - (nulo) la conexión: didReceiveData como se sugiere en la documentación, pero no hay nada allí.

// url es 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]; 

Respuesta

64

simplemente Se puede usar esta característica que se incluye en AFNetworking e incluso guardar en disco - Aviso la 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 
}]; 

EDITAR: missed

[operation start]; 

EDITAR: ...or if you use an AFHTTPClient * apiClient :

// [apiClient enqueueHTTPRequestOperation:operation]; 
+3

Alguien dar a este hombre un upvote! ¡Y OP debería seleccionar la respuesta! – radj

Cuestiones relacionadas