如何在Alamofire中暂停/恢复/取消我的下载请求
我正在下载一个文件使用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 }
请在downloadBtnTapped
使用属性保留对创build的请求的引用,并在pauseBtnTapped
调用该属性的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()
将取消下载进度。 如果你想暂停和继续,你可以使用:
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() }