Last active
June 3, 2021 09:26
-
-
Save labibmuhajir/79cbd52b0b015092f06778cb31814cca to your computer and use it in GitHub Desktop.
alamofire download file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
in info.plist UISupportsDocumentBrowser true