Skip to content

Instantly share code, notes, and snippets.

@labibmuhajir
Last active June 3, 2021 09:26
Show Gist options
  • Save labibmuhajir/79cbd52b0b015092f06778cb31814cca to your computer and use it in GitHub Desktop.
Save labibmuhajir/79cbd52b0b015092f06778cb31814cca to your computer and use it in GitHub Desktop.
alamofire download file
class NetworkDownload {
static let shared = NetworkDownload()
func download(url: String, result: @escaping (URL) -> Void, onProgress: ((Double) -> Void)? = nil) {
let fileName = url.components(separatedBy: "/").last ?? "File \(Date.currentDateTime())"
let destination: DownloadRequest.DownloadFileDestination = { _, _ in
let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let fileURL: URL = documentsURL.appendingPathComponent(fileName)
return (fileURL, [.removePreviousFile, .createIntermediateDirectories])
}
Alamofire.download(url, to: destination).downloadProgress { (progress) in
let completed = progress.fractionCompleted
onProgress?(completed)
}.responseData { (response) in
guard let url = response.destinationURL else { return }
result(url)
}
}
}
@labibmuhajir
Copy link
Author

in info.plist UISupportsDocumentBrowser true

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment