我想在取消按钮时取消下载请求。 现在当我按下代码后按下载按钮
@IBAction func downloadButtonPressed(sender: UIButton) {
var fileName = noticeAttacchment[0]["fileName"] as! String
var fileUrl = noticeAttacchment[0]["fileUrl"] as! String
if var URL = NSURL(string: fileUrl){
Api.load(URL, fileName: fileName)
}
}
我的下载程序类是
class Api {
class func load(URL: NSURL , fileName : String) {
let sessionConfig = NSURLSessionConfiguration.defaultSessionConfiguration()
let session = NSURLSession(configuration: sessionConfig, delegate: nil, delegateQueue: nil)
let request = NSMutableURLRequest(URL: URL)
request.HTTPMethod = "GET"
let task = session.dataTaskWithRequest(request, completionHandler: { (data: NSData!, response: NSURLResponse!, error: NSError!) -> Void in
if (error == nil) {
FileSave.saveData(
data, directory: NSSearchPathDirectory.CachesDirectory, path: fileName, subdirectory: "edstem")
}
else {
println("Faulure: %@", error.localizedDescription);
}
})
task.resume()
}
}
那么如何在按下取消按钮时取消请求