title: Amazon S3 iOS SDK background upload with progress block
body: <p>I am working on an app that depends on AWS for file uploads and downloads. In the case of uploads, when I upload video files using AWSS3TransferManagerUploadRequest, I can show progress bar using progress block, but when I press home button and app enters background, the upload pauses (probably because it uses NSURLConnection). Only if the app is running in foreground will upload take place. </p>
  
  <p>This is the code for uploading files using AWSS3TransferManagerUploadRequest.</p>
  
  <pre><code>AWSS3TransferManager *transferManager = [AWSS3TransferManager defaultS3TransferManager];
  
  AWSS3TransferManagerUploadRequest *uploadRequest = [AWSS3TransferManagerUploadRequest new];
  
  uploadRequest.bucket = bucketName;
  uploadRequest.key = appropriateKey;
  
  uploadRequest.body = [NSURL fileURLWithPath:filePath];
  
  uploadRequest.contentType = contentType;
  
  uploadRequest.uploadProgress = progressBlock;
  
  
  [[transferManager upload:uploadRequest] continueWithExecutor:[AWSExecutor mainThreadExecutor] withBlock:^id(AWSTask *task) {
      if (task.error)
      {
          if ([task.error.domain isEqualToString:AWSS3TransferManagerErrorDomain])
          {
              switch (task.error.code)
              {
                  case AWSS3TransferManagerErrorCancelled:
                  case AWSS3TransferManagerErrorPaused:
                      break;
  
                  default:
                      failBlock(task.error.code, task.error.localizedDescription);
                      break;
              }
          }
          else
          {
              failBlock(task.error.code, task.error.localizedDescription);
          }
      }
      if (task.result)
      {
          AWSS3TransferManagerUploadOutput *uploadOutput = task.result;
          successBlock(uploadOutput);
      }
      return nil;
  }];
  </code></pre>
  
  <p>This works, except for transfer in background. </p>
  
  <p>So, I went ahead and used the AWS's pre-signed URLs. The following is the code for the same.</p>
  
  <pre><code>AWSS3GetPreSignedURLRequest *getPreSignedURLRequest = [AWSS3GetPreSignedURLRequest new];
  getPreSignedURLRequest.bucket = bucketName;
  
  getPreSignedURLRequest.key = appropriateKey;
  getPreSignedURLRequest.HTTPMethod = AWSHTTPMethodPUT;
  getPreSignedURLRequest.expires = [NSDate dateWithTimeIntervalSinceNow:3600];
  
  
  //Important: set contentType for a PUT request.
  NSString *fileContentTypeStr = contentType;
  getPreSignedURLRequest.contentType = fileContentTypeStr;
  [[[AWSS3PreSignedURLBuilder defaultS3PreSignedURLBuilder] getPreSignedURL:getPreSignedURLRequest]
   continueWithBlock:^id(AWSTask *task) {
  
       if (task.error) {
           NSLog(@"Error: %@",task.error);
       } else {
  
           NSURL *presignedURL = task.result;
           NSLog(@"upload presignedURL is: \n%@", presignedURL);
  
           NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:presignedURL];
           request.cachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
           [request setHTTPMethod:@"PUT"];
           [request setValue:fileContentTypeStr forHTTPHeaderField:@"Content-Type"];
  
  
           NSURLSession *session;
           NSURL *uploadFileUrl = [NSURL fileURLWithPath:filePath];
           NSURLSessionUploadTask *uploadTask = [session uploadTaskWithRequest:request fromFile:uploadFileUrl];
           //uploadTask is an instance of NSURLSessionDownloadTask.
           //session is an instance of NSURLSession.
           [uploadTask resume];
  
       }
  
       return nil;
   }];
  </code></pre>
  
  <p>This solves the problem of uploading in the background. But it does not have a progress block and because of that I cannot track progress. </p>
  
  <p>I have to be able to track progress and upload from background. Is this possible? Or is there any workaround? </p>