For download task, the completion handler will be called with nil
value for the location
and the code
value of the URLError
object will be .cancelled
. For example:
let task = session.downloadTask(with: url) { location, response, error in
if let error = error as? URLError {
if error.code == .cancelled {
// canceled
} else {
// some other error
}
return
}
// proceed to move file at `location` to somewhere more permanent
}
task.resume()
Or look for the code
value of the NSError
object of NSURLErrorCancelled
:
let task = session.downloadTask(with: url) { location, response, error in
if let error = error as NSError? {
if error.code == NSURLErrorCancelled {
// canceled
} else {
// some other error
}
return
}
// proceed to move file at `location` to somewhere more permanent
}
task.resume()
The process of casting the error
parameter is the same for data tasks, as the download task above.
For Swift 2, see previous revision of this answer.