Skip to content

Instantly share code, notes, and snippets.

@saturngod
Last active December 11, 2015 05:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saturngod/4552749 to your computer and use it in GitHub Desktop.
Save saturngod/4552749 to your computer and use it in GitHub Desktop.
NSDictionary* headers = [(NSHTTPURLResponse *)response allHeaderFields];
if ([headers objectForKey:@"Accept-Ranges"]) {
_allowResume = YES;
}
else
{
_allowResume = NO;
}
-(void)resume
{
//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];
}
SGdownloader *downloader = [[SGdownloader alloc] initWithURL:[NSURL URLWithString:@"http://myfile.com/file.jpg"] 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
}];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment