How to pause/resume/cancel my download request in Alamofire

后端 未结 2 771
滥情空心
滥情空心 2021-02-01 20:15

I am downloading a file using Alamofire download with progress but i have no idea how to pause / resume / cancel the specific request.

@IBAction func downloadBtn         


        
相关标签:
2条回答
  • 2021-02-01 20:41

    Keep a reference to the request created in downloadBtnTapped with a property, and call cancel on that property in pauseBtnTapped.

    var request: Alamofire.Request?
    
    @IBAction func downloadBtnTapped() {
     self.request = Alamofire.download(.GET, "http://httpbin.org/stream/100", destination: destination)
    }
    
    @IBAction func pauseBtnTapped(sender : UIButton) {
      self.request?.cancel()
    }
    
    0 讨论(0)
  • 2021-02-01 20:46

    request.cancel() will cancel the download progress. If you want to pause and continue, you can use:

    var request: Alamofire.Request?
    
    @IBAction func downloadBtnTapped() {
     self.request = Alamofire.download(.GET, "http://yourdownloadlink.com", destination: destination)
    }
    
    @IBAction func pauseBtnTapped(sender : UIButton) {
      self.request?.suspend()
    }
    
    @IBAction func continueBtnTapped(sender : UIButton) {
      self.request?.resume()
    }
    
    @IBAction func cancelBtnTapped(sender : UIButton) {
      self.request?.cancel()
    }
    
    0 讨论(0)
提交回复
热议问题