Get immediate access to this and over 1,500+ other videos and books.

Boost your skills with a raywenderlich.com Beginner subscription. With over 60+ video courses and our core foundational programming books bundled in one subscription, it’s simply the best investment you can make in your development career.

Show Download Progress

Performing lengthy uploads or downloads without any visual indication of progress is not the best user experience. This episiode will show you how to provide a progress indicator for a download.

Contributors

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

You can unlock the rest of this video course, and our entire catalogue of books and videos, with a raywenderlich.com Professional subscription.

When downloading images or large over the network, the user will often times want to know the progress of the total download. If using the closure-based task downloads on URLSession, you have delegate methods for this.

@MainActor @State private var downloadProgress: Float = 0.0
if isDownloading {
  ProgressView(value: downloadProgress)
}
func downloadSongBytes(at url: URL, progress: Binding<Float>) async throws {
    
}
try await downloader.downloadSongBytes(at: previewURL, progress: $downloadProgress)
let (asyncBytes, response) = try await session.bytes(from: url)
let contentLength = Float(response.expectedContentLength)
var data = Data(capacity: Int(contentLength)) 
for try await byte in asyncBytes {
 
}
data.append(byte)
progress.wrappedValue = Float(data.count) / Float(contentLength)
let currentProgress = Float(data.count) / contentLength
if Int(progress.wrappedValue * 100) != Int(currentProgress * 100) {
  progress.wrappedValue = currentProgress
}
let fileManager = FileManager.default

guard let documentsPath = fileManager.urls(for: .documentDirectory,
                                           in: .userDomainMask).first
else {
  throw SongDownloadError.documentDirectoryError
}
let lastPathComponent = url.lastPathComponent
let destinationURL = documentsPath.appendingPathComponent(lastPathComponent)
do {
  if fileManager.fileExists(atPath: destinationURL.path) {
    try fileManager.removeItem(at: destinationURL)
  }
    
  try data.write(to: destinationURL)
} catch {
 throw SongDownloadError.failedToStoreSong
}
await MainActor.run {
  downloadLocation = destinationURL
}

Reviews

Comments