简体   繁体   中英

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

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 downloadBtnTapped() {

 Alamofire.download(.GET, "http://httpbin.org/stream/100", destination: destination)
     .progress { (bytesRead, totalBytesRead, totalBytesExpectedToRead) in
         println(totalBytesRead)
     }
     .response { (request, response, _, error) in
         println(response)
     }
}


@IBAction func pauseBtnTapped(sender : UIButton) {        
    // i would like to pause/cancel my download request here
}

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()
}

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()
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM