Created
September 6, 2021 14:06
-
-
Save arvkmr/d3e8a0798eee476a0d1ecb8ddfc78d06 to your computer and use it in GitHub Desktop.
objective-c URLSession download class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// FileDownloader.m | |
// | |
#import <Foundation/Foundation.h> | |
#import "FileDownloader.h" | |
#import "NSMutableArray+QueueAdditions.h" | |
@interface FileDownloader() | |
@property NSInteger downloadSize; | |
@property NSInteger pendingDownloadSize; | |
@property long bytesReceived; | |
@property NSString* outPutPath; | |
@property NSMutableArray *queue; | |
@property NSString *path; | |
@property NSURLSessionConfiguration *defaultConfigObject; | |
@property NSURLSession *defaultSession; | |
@property NSURLSessionDataTask *dataTask; | |
@property NSFileHandle *currentHandle; | |
@end | |
@implementation FileDownloader : NSObject | |
-(instancetype) initWithDir: (NSString*) path { | |
self = [super init]; | |
_path = path; | |
_queue = [[NSMutableArray alloc] init]; | |
_defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration]; | |
_defaultSession = [NSURLSession sessionWithConfiguration: _defaultConfigObject delegate: self delegateQueue: [NSOperationQueue mainQueue]]; | |
return self; | |
} | |
-(void) downloadFile: (NSURL*) url { | |
[_queue enqueue:url]; | |
if(_dataTask == nil){ | |
id ur = [_queue dequeue]; | |
if(ur != nil){ | |
_dataTask = [_defaultSession dataTaskWithURL: url]; | |
[_dataTask resume]; | |
} | |
} | |
} | |
-(void) next { | |
id url = [_queue dequeue]; | |
if(url != nil){ | |
_dataTask = [_defaultSession dataTaskWithURL: url]; | |
[_dataTask resume]; | |
} else { | |
_dataTask = nil; | |
} | |
} | |
-(void) cancel { | |
[_queue removeAllObjects]; | |
[_dataTask cancel]; | |
} | |
- (void)URLSession:(NSURLSession *)session | |
task:(NSURLSessionTask *)task | |
willPerformHTTPRedirection:(NSHTTPURLResponse *)response | |
newRequest:(NSURLRequest *)request | |
completionHandler:(void (^)(NSURLRequest *))completionHandler { | |
NSLog(@"willPerformRedirection"); | |
completionHandler(request); | |
} | |
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition disposition))completionHandler { | |
completionHandler(NSURLSessionResponseAllow); | |
_downloadSize = [response expectedContentLength]; | |
NSLog(@"didReceiveResponse %ld",_downloadSize); | |
NSString *file = [_path stringByAppendingPathComponent:[response suggestedFilename]]; | |
[[NSFileManager defaultManager] createFileAtPath:file contents:[[NSData alloc] init] attributes:nil]; | |
_currentHandle = [NSFileHandle fileHandleForWritingAtPath: file]; | |
} | |
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data { | |
_bytesReceived += [data length]; | |
[self->_currentHandle writeData:data]; | |
} | |
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error { | |
if(error != nil) { | |
NSLog(@"%@",error.localizedDescription); | |
} | |
[self next]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment