From 87149c2b57be0f78f045c85e62a42c7eb27ec0cc Mon Sep 17 00:00:00 2001 From: Aaron Franks Date: Fri, 20 May 2016 16:07:05 -0700 Subject: [PATCH] First pass at native code to support background downloads --- Downloader.h | 2 +- Downloader.m | 50 ++++++++++++++++++++++++-------------------------- 2 files changed, 25 insertions(+), 27 deletions(-) diff --git a/Downloader.h b/Downloader.h index 1faaf34..a9af75b 100644 --- a/Downloader.h +++ b/Downloader.h @@ -16,7 +16,7 @@ typedef void (^ProgressCallback)(NSNumber*, NSNumber*); @end -@interface Downloader : NSObject +@interface Downloader : NSObject - (void)downloadFile:(DownloadParams*)params; - (void)stopDownload; diff --git a/Downloader.m b/Downloader.m index 888361a..96bbb0a 100644 --- a/Downloader.m +++ b/Downloader.m @@ -8,7 +8,8 @@ @property (copy) DownloadParams* params; -@property (retain) NSURLConnection* connection; +@property (retain) NSURLSession* session; +@property (retain) NSURLSessionTask* task; @property (retain) NSNumber* statusCode; @property (retain) NSNumber* contentLength; @property (retain) NSNumber* bytesWritten; @@ -27,12 +28,7 @@ NSURL* url = [NSURL URLWithString:_params.fromUrl]; - NSMutableURLRequest* downloadRequest = [NSMutableURLRequest requestWithURL:url - cachePolicy:NSURLRequestUseProtocolCachePolicy - timeoutInterval:30]; - [[NSFileManager defaultManager] createFileAtPath:_params.toFile contents:nil attributes:nil]; - _fileHandle = [NSFileHandle fileHandleForWritingAtPath:_params.toFile]; if (!_fileHandle) { @@ -41,31 +37,26 @@ return _params.errorCallback(error); } - _connection = [[NSURLConnection alloc] initWithRequest:downloadRequest delegate:self startImmediately:NO]; + NSURLSessionConfiguration *config; + config = [NSURLSessionConfiguration defaultSessionConfiguration]; + // TODO: use the following config for session objects: + //config = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:fromUrl]; - [_connection scheduleInRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode]; - - [_connection start]; + _session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:nil]; + _task = [_session downloadTaskWithURL:url]; + [_task resume]; } -- (void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error +- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didReceiveResponse:(NSURLResponse *)response { - [_fileHandle closeFile]; + NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)downloadTask.response; + _statusCode = [NSNumber numberWithLong:httpResponse.statusCode]; + _contentLength = [NSNumber numberWithLong:httpResponse.expectedContentLength]; - return _params.errorCallback(error); + return _params.beginCallback(_statusCode, _contentLength, httpResponse.allHeaderFields); } -- (void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse*)response -{ - NSHTTPURLResponse* httpUrlResponse = (NSHTTPURLResponse*)response; - - _statusCode = [NSNumber numberWithLong:httpUrlResponse.statusCode]; - _contentLength = [NSNumber numberWithLongLong: httpUrlResponse.expectedContentLength]; - - return _params.beginCallback(_statusCode, _contentLength, httpUrlResponse.allHeaderFields); -} - -- (void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data +- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(NSData *)data { if ([_statusCode isEqualToNumber:[NSNumber numberWithInt:200]]) { [_fileHandle writeData:data]; @@ -76,16 +67,23 @@ } } -- (void)connectionDidFinishLoading:(NSURLConnection*)connection +- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location { [_fileHandle closeFile]; return _params.completeCallback(_statusCode, _bytesWritten); } +- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionTask *)downloadTask didCompleteWithError:(NSError *)error +{ + [_fileHandle closeFile]; + return _params.errorCallback(error); +} + + - (void)stopDownload { - [_connection cancel]; + [_task cancel]; } @end