20

我正在使用 Alamofire 下载有进度的文件,但我不知道如何暂停/恢复/取消特定请求。

@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
}
4

2 回答 2

36

保留对使用属性创建的请求的引用downloadBtnTapped,并在 中调用cancel该属性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()
}
于 2014-10-13T16:24:16.463 回答
22

request.cancel()将取消下载进度。如果要暂停并继续,可以使用:

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()
}
于 2015-12-14T12:41:18.697 回答