title: Can I use HTTP caching with an NSURLSessionDownloadTask on iOS?
body: <p>I'm trying to use <code>NSURLSessionDownloadTask</code>, and take advantage of Apple's in-built URL caching functionality. I have succeeded in getting the caching to work when using an <code>NSURLSessionDataTask</code> using the code below:</p>
  
  <pre><code>- (void)downloadUsingNSURLSessionDataTask:(NSURL *)url {
      NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration];
      NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfig delegate:self delegateQueue:nil];
      NSURLRequest *request = [NSURLRequest requestWithURL:url];
      NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request];
      [dataTask resume];
  }
  
  - (void)cachedDataTaskTest {
      // This call performs an HTTP request
      [self downloadUsingNSURLSessionDataTask:[NSURL URLWithString:@"http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"]];
      [NSThread sleepForTimeInterval:1];
  
      // This call returns the locally cached copy, and no HTTP request occurs
      [self downloadUsingNSURLSessionDataTask:[NSURL URLWithString:@"http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"]];
  }
  </code></pre>
  
  <p>However, I need to perform a background download for which I have to use an NSURLDownloadTask. When I switch to this the caching behaviour does not occur.</p>
  
  <pre><code>- (void)downloadUsingNSURLSessionDownloadTask:(NSURL *)url {
      NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration];
      NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfig delegate:self delegateQueue:nil];
      NSURLRequest *request = [NSURLRequest requestWithURL:url];
      NSURLSessionDownloadTask *downloadTask = [session downloadTaskWithRequest:request];
      [downloadTask resume];
  }
  
  - (void)cachedDownloadTaskTest {
      // This call performs an HTTP request
      [self downloadUsingNSURLSessionDownloadTask:[NSURL URLWithString:@"http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"]];
      [NSThread sleepForTimeInterval:1];
  
      // This call also performs an HTTP request
      [self downloadUsingNSURLSessionDownloadTask:[NSURL URLWithString:@"http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"]];
  }
  </code></pre>
  
  <p><a href="https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/URLLoadingSystem/NSURLSessionConcepts/NSURLSessionConcepts.html">This documentation</a> from Apple indicates that <code>NSURLDownloadTasks</code> don't call the <code>URLSession:dataTask:willCacheResponse:completionHandler:</code> delegate method, so it is not possible for your app to hook into the caching life cycle. My guess is that this implies that caching is simply not available for these tasks, but it is not explicit about this.</p>
  
  <blockquote>
    <ol start="11">
    <li><strong>For a data task</strong>, the <code>NSURLSession</code> object calls the delegate’s <code>URLSession:dataTask:willCacheResponse:completionHandler:</code> method. Your
    app should then decide whether to allow caching. If you do not
    implement this method, the default behavior is to use the caching
    policy specified in the session’s configuration object.</li>
    </ol>
  </blockquote>
  
  <p>Can anyone confirm this hunch that NSURLSessionDownloadTasks simply don't support caching? Is it possible to take advantage of Apple's HTTP caching behaviour in a background task?</p>