wagppinto wagppinto - 3 months ago 32
Objective-C Question

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
.

Answer

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