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

Share:
11,075
L0rdShrek
Author by

L0rdShrek

Updated on June 15, 2022

Comments

  • L0rdShrek
    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
    Tudor almost 12 years
    Is there a non ARC alternative?
  • Praveen Sharma
    Praveen Sharma over 11 years
    Do 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
    mrgrieves over 11 years
    Just 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
    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
    mkc842 about 10 years
    Thanks.. could you elaborate on [operations addObject:operation] ?
  • lenhhoxung
    lenhhoxung about 10 years
    AFDownloadRequestOperation not found in the latest AFNetworking git hub
  • Siriss
    Siriss over 9 years
    what is 'operations'?
  • Hamid Zandi
    Hamid Zandi almost 8 years
    how is in AFN 3.0 ? answer is in AFN 2.0 :(