Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a response from NSURLSessionDownloadTask downloadTaskWithRequest

Some background first: Application is supposed to grab files from AWS S3 server. In order to do that, first step of that process is to go to local server and get the name of the file and some other information from it. After that step we have a complete URLMutableRequest.

NOTE: I am setting up the NSURLSession as a background session:

- (NSURLSession *)backgroundSession
{
    static NSURLSession *session = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration backgroundSessionConfiguration:@"identifier"];
        session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
    });
    return session;
}

This is the task to download the files from AWS S3 server:

for this task I want to use the delegates to run in background mode.

@property (nonatomic, strong) NSURLSessionDownloadTask *downloadTask;
@property (nonatomic, strong) NSURLSession *defaultSession; 

self.defaultSession = [self backgroundSession];
self.downloadTask = [self.defaultSession downloadTaskWithRequest:request];
[self.downloadTask resume];

How to I get a RESPONSE form this REQUEST?

Apple documentation says you can't have a block as completionHandler when using a backgroundSessionConfiguration.

like image 977
wagppinto Avatar asked Sep 11 '15 18:09

wagppinto


2 Answers

In case anyone wondering how to get download response before download is complete, try this: fire off dataTask instead, get the response, then convert dataTask to download if required.

NSURLSessionTask *task = [session dataTaskWithRequest:request];
[task resume];

NSURLSessionDataDelegate

- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition disposition))completionHandler {
    // use response, convert data task to download task
    completionHandler(NSURLSessionResponseBecomeDownload);
}

- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didBecomeDownloadTask:(NSURLSessionDownloadTask *)downloadTask {
    // downloadTask converted from dataTask
}

NSURLSessionDownloadDelegate

- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite {
    // update progress
}

- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location {
    // copy downloaded file from location
}
like image 103
Roman B. Avatar answered Nov 18 '22 01:11

Roman B.


NSURLSessionDownloadTask has a response property (part of its base class, NSURLSessionTask) that should be set to the response. See here.

like image 42
Mean Dinosaur Avatar answered Nov 18 '22 01:11

Mean Dinosaur