2012-08-29 25 views
6

Necesito descargar archivos .zip grandes (hasta 800 MB) con la aplicación de mi iPad. Quiero reanudar la descarga nuevamente si se cancela o si la aplicación está en segundo plano.AFNetworking + Pausa/Reanudar la descarga de archivos grandes

operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; 
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:filePath append:YES]; 

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

// unzip the .zip 


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

//Handle the error 

}]; 


[operation setDownloadProgressBlock:^(NSInteger bytesWritten, long long totalBytesWritten,long long totalBytesExpectedToWrite) { 

//update the progressView 

}]; 

[operation setShouldExecuteAsBackgroundTaskWithExpirationHandler:^{ 

// What I have to do here? 

}]; 

[operation start]; 
[operation waitUntilFinished]; 


-(void)applicationWillResignActive:(UIApplication *)application{ 

// What I have to do here? 

} 

Gracias por su ayuda.

Respuesta

23

Puede usar AFDownloadRequestOperation para hacer esto.

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"....zip"]]; 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"....zip"]; 
    AFDownloadRequestOperation *operation = [[AFDownloadRequestOperation alloc] initWithRequest:request targetPath:path shouldResume:YES]; 
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { 
     NSLog(@"Successfully downloaded file to %@", path); 
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
     NSLog(@"Error: %@", error); 
    }]; 
    [operation setProgressiveDownloadProgressBlock:^(NSInteger bytesRead, long long totalBytesRead, long long totalBytesExpected, long long totalBytesReadForFile, long long totalBytesExpectedToReadForFile) { 
     NSLog(@"Operation%i: bytesRead: %d", 1, bytesRead); 
     NSLog(@"Operation%i: totalBytesRead: %lld", 1, totalBytesRead); 
     NSLog(@"Operation%i: totalBytesExpected: %lld", 1, totalBytesExpected); 
     NSLog(@"Operation%i: totalBytesReadForFile: %lld", 1, totalBytesReadForFile); 
     NSLog(@"Operation%i: totalBytesExpectedToReadForFile: %lld", 1, totalBytesExpectedToReadForFile); 
    }]; 
    [operations addObject:operation]; 

Después de reiniciar la aplicación y generar la solicitud con la misma URL, se reanudará la descarga. "shouldResume: YES" funciona

+1

¿Existe alguna alternativa que no sea ARC? – Tudorizer

+0

¿Sabe si esto funciona con la redirección de URL? He prescrito URL que luego redireccionan al archivo real para descargar. Parece que las descargas no se reanudan con estas URL. –

+0

Solo asegúrese de especificar el mismo targetPath cada vez. Tampoco puede ser un directorio; si especifica un directorio, usará la URL para nombrar el archivo. – mrgrieves