title: AWSS3 Mobile SDK cancel file download in swift
body: <p>I have a file download code in my application using AWS Mobile SDK. 
  That shows an alert with download progress and a cancel button. It is working but I need to cancel download when user hit on alert cancel button. I have tried to cancel the task using the object of AWSS3TransferUtilityDownloadTask and AWSS3TransferUtilityTask.But it seems not working it gives me progress and download completion.</p>
  
  <p>here Is my working code of file download. </p>
  
  <p>Please suggest me how can I Cancel my Download request.</p>
  
  <pre><code>    let credentialProvider = AWSStaticCredentialsProvider(accessKey: "MyAccessKey", secretKey: "MySecretKey")
      let configuration = AWSServiceConfiguration(region: AWSRegionType.USEast1, credentialsProvider: credentialProvider)
      AWSServiceManager.default().defaultServiceConfiguration = configuration
  
      let expression = AWSS3TransferUtilityDownloadExpression()
      expression.progressBlock = {(task, progress) in DispatchQueue.main.async(execute: {
          // Do something e.g. Update a progress bar.
          print("File Progress:",Float(progress.fractionCompleted))
  
          let progress = Float(progress.fractionCompleted)
  
          DispatchQueue.main.async {
              if progress == 1.0 {
                  self.globalAlert.dismiss(animated: true, completion: nil)
              }else{
              self.globalProgressView!.progress = progress
              self.msgProgress = String(Int(progress*100))
              }
          }
  
  
      })
      }
  
      var completionHandler: AWSS3TransferUtilityDownloadCompletionHandlerBlock?
      completionHandler = { (task, URL, data, error) -&gt; Void in
          DispatchQueue.main.async(execute: {
              // Do something e.g. Alert a user for transfer completion.
              // On failed downloads, `error` contains the error object.
              //My other code stuff execution
  
          })
      }
      var refUploadTask: AWSS3TransferUtilityTask?
      let transferUtility = AWSS3TransferUtility.default()
  
      transferUtility.downloadData(
          fromBucket: Bucket_Name,
          key: fileKey,
          expression: expression,
          completionHandler: completionHandler
          ).continueWith {
              (task) -&gt; AnyObject? in if let error = task.error {
                  print("Error: \(error.localizedDescription)")
              }
  
              if let _ = task.result {
                  // Do something with downloadTask.
                  print("download started..")
                  DispatchQueue.main.async(execute: {
  
                      self.globalAlert = UIAlertController(title: "Downloading...", message: "\(String(describing: self.msgProgress))% Completed", preferredStyle: .alert)
                      let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertAction.Style.default, handler: {
                          (action : UIAlertAction!) -&gt; Void in
  
  
                               self.refUploadTask.cancel()
  
  
                      })
  
                      //  Show it to your users
                      self.globalAlert.addAction(cancelAction)
                      self.present(self.globalAlert, animated: true, completion: {
                          //  Add your progressbar after alert is shown (and measured)
                          let margin:CGFloat = 8.0
                          let rect = CGRect(x: margin, y: 72.0, width: self.globalAlert.view.frame.width - margin * 2.0 , height: 2.0)
                          self.globalProgressView = UIProgressView(frame: rect)
                          self.globalProgressView!.tintColor = .blue
                          self.globalAlert.view.addSubview(self.globalProgressView!)
                      })
                  })
  
              }
              return nil;
      }
  </code></pre>