AFNetworking + Pause/ Resume downloading big files
11,075
You can use AFDownloadRequestOperation to do this.
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];
After you restart your app, and generate the request having a same url, it will resume downloading. "shouldResume:YES" works
![L0rdShrek](https://i.stack.imgur.com/N96nz.jpg?s=256&g=1)
Author by
L0rdShrek
Updated on June 15, 2022Comments
-
L0rdShrek about 2 years
I need to download large .zip files (up to 800 MB) with my iPad app. I want to resume the download again if it is canceled or if the app is in the background.
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? }
Thanks for your help.
-
Tudor almost 12 yearsIs there a non ARC alternative?
-
Praveen Sharma over 11 yearsDo you know if this works with URL redirection? I have presigned urls which then redirect to the actual file to download. The downloads don't seem to be resuming using these URLs.
-
mrgrieves over 11 yearsJust make sure that you specify the same targetPath every time. Also it can't be directory; if you specify a directory, it'll use the URL to name the file.
-
Adam Lockhart almost 11 years@Tudorizer Just set a compiler flag for the file under build phases, -fobjc-arc. See the AFNetworking on github for details.
-
mkc842 about 10 yearsThanks.. could you elaborate on [operations addObject:operation] ?
-
lenhhoxung about 10 yearsAFDownloadRequestOperation not found in the latest AFNetworking git hub
-
Siriss over 9 yearswhat is 'operations'?
-
Hamid Zandi almost 8 yearshow is in AFN 3.0 ? answer is in AFN 2.0 :(