Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pause/Resume downloads in Objective-C

Alright. Hopefully this will be my last post about the download manager I am writing in Objective-C. Everything seems to work well except the pause/resume functionality. My issue is that when a download tries to continue from where it left off, it appends the data it receives to the file, but it still seems that it's trying to download the entire file. This results in a file that is larger than the original file is supposed to be. Here is the code I am using for downloading files. Am I doing something wrong?

-(void)start:(unsigned int)fromByte {
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:self.url] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:DEFAULT_TIMEOUT];

    // Define the bytes we wish to download.
    NSString *range = [NSString stringWithFormat:@"bytes=%i-", fromByte];
    [request setValue:range forHTTPHeaderField:@"Range"];

    // Data should immediately start downloading after the connection is created.
    self.urlConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:TRUE];

    if (!self.urlConnection) {
        #warning Handle error.
    }
}
like image 341
FreeAsInBeer Avatar asked Mar 02 '11 16:03

FreeAsInBeer


1 Answers

I see you are specifying the Range header in the request. First thing to check is whether the server is actually honoring the Range request, by checking the headers in the response object (which should be an NSHTTPURLResponse) in connection:didReceiveResponse: for a proper Content-Range.

like image 170
Anomie Avatar answered Sep 30 '22 13:09

Anomie