in storage/StorageReferenceSwift/ViewController.swift [470:530]
func storageDownloadCombinedExample() {
let storageRef = Storage.storage().reference()
let localURL = URL(string: "path/to/image")!
// [START firstorage_download_combined]
// Create a reference to the file we want to download
let starsRef = storageRef.child("images/stars.jpg")
// Start the download (in this case writing to a file)
let downloadTask = storageRef.write(toFile: localURL)
// Observe changes in status
downloadTask.observe(.resume) { snapshot in
// Download resumed, also fires when the download starts
}
downloadTask.observe(.pause) { snapshot in
// Download paused
}
downloadTask.observe(.progress) { snapshot in
// Download reported progress
let percentComplete = 100.0 * Double(snapshot.progress!.completedUnitCount)
/ Double(snapshot.progress!.totalUnitCount)
}
downloadTask.observe(.success) { snapshot in
// Download completed successfully
}
// Errors only occur in the "Failure" case
downloadTask.observe(.failure) { snapshot in
guard let errorCode = (snapshot.error as? NSError)?.code else {
return
}
guard let error = StorageErrorCode(rawValue: errorCode) else {
return
}
switch (error) {
case .objectNotFound:
// File doesn't exist
break
case .unauthorized:
// User doesn't have permission to access file
break
case .cancelled:
// User cancelled the download
break
/* ... */
case .unknown:
// Unknown error occurred, inspect the server response
break
default:
// Another error occurred. This is a good place to retry the download.
break
}
}
// [END firstorage_download_combined]
}