Last week , I updated the SGDownloader to support more feature.

Now, SGDownloader can do pause and resume the download file.

Example Usages

SGdownloader *downloader = [[SGdownloader alloc] initWithURL:[NSURL URLWithString:@""] timeout:60];

[downloader startWithDownloading:(float progress,NSInteger percentage) {

    //progress for progress bar
    //percentage for download percentage

} onFinished:(NSData* fileData,NSString* fileName){

    //use NSData to write a file or image

}onFail (NSError* error){

    //on fail


SGDownloader also support delegate method to use with tableview. You can check more example at github.

How did I do Pause And Resume

I am using Range at HTTP Header. Some of the servers didn’t support Range. So, I also checking Accept-Ranges at HTTP header like below.

NSDictionary* headers = [(NSHTTPURLResponse *)response allHeaderFields];
if ([headers objectForKey:@"Accept-Ranges"]) {
    _allowResume = YES;
    _allowResume = NO;

For resume , I am using Range header like below

    //add range for resume but some of the server didn't support resume
    NSString *range = @"bytes=";
    range = [range stringByAppendingString:[[NSNumber numberWithInt:self.receiveBytes] stringValue]];
    range = [range stringByAppendingString:@"-"];
    NSURL* fileURL = self.request.URL.absoluteURL;

    self.resumeProgress = YES;//add trigger for except exceptedBytes

    self.request = [[NSMutableURLRequest alloc] initWithURL:fileURL cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:self.tmpTimeout];

    [self.request setValue:range forHTTPHeaderField:@"Range"];

    self.connection = [[NSURLConnection alloc] initWithRequest:self.request delegate:self startImmediately:YES];

To Do

I am trying to support like multi connection like download manager to make download faster.

If you found a issues , please submit on github.

comments powered byDisqus