Я делаю загрузку фотографий и аудио из url
Делаю я это так
extension DownloadAllViewController: URLSessionDownloadDelegate {
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
// print("downloadLocation:", location)
// create destination URL with the original pdf name
guard let url = downloadTask.originalRequest?.url else { return }
let documentsPath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
let destinationURL = documentsPath.appendingPathComponent(url.lastPathComponent)
// delete original copy
try? FileManager.default.removeItem(at: destinationURL)
// copy from temp to Document
do {
try FileManager.default.copyItem(at: location, to: destinationURL)
print(destinationURL)
} catch let error {
print("Copy Error: \(error.localizedDescription)")
}
}}
Само сохранение
guard let url_audio_qq = URL(string: "url") else { return }
let urlSession1 = URLSession(configuration: .default, delegate: self as URLSessionDelegate, delegateQueue: OperationQueue())
let downloadTask1 = urlSession1.downloadTask(with: url_audio_qq)
downloadTask1.resume()
А вот как заполняется прогрессбар
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
// if totalBytesExpectedToWrite > 0 {
let progress = Float(totalBytesWritten) / Float(totalBytesExpectedToWrite)
DispatchQueue.main.async {
self.progressBar.setProgress(progress, animated: true)
self.per_cents.text = "\(Int(progress*100))%"
}
}
Сейчас у меня после каждой загрузки отдельного аудио или изображения заполняется шкала, после того как аудио скачалось, она опять спадает до нуля и показывает прогресс загрузки следуйщего
Как сделать так чтобы он выводил прогресс от общей загрузки всех файлов, а не по отдельности каждого файла?