First pass at native code to support background downloads

This commit is contained in:
Aaron Franks
2016-06-02 00:52:47 +01:00
committed by Chris Dell
parent 00ad61d3cf
commit 87149c2b57
2 changed files with 25 additions and 27 deletions
+1 -1
View File
@@ -16,7 +16,7 @@ typedef void (^ProgressCallback)(NSNumber*, NSNumber*);
@end
@interface Downloader : NSObject <NSURLConnectionDelegate>
@interface Downloader : NSObject <NSURLSessionDelegate, NSURLSessionDownloadDelegate>
- (void)downloadFile:(DownloadParams*)params;
- (void)stopDownload;
+24 -26
View File
@@ -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