Skip to content

Instantly share code, notes, and snippets.

@AhmedMenaim
Created December 13, 2021 07:15
Show Gist options
  • Save AhmedMenaim/298760983efb578fc7d4db13c6ae08c5 to your computer and use it in GitHub Desktop.
Save AhmedMenaim/298760983efb578fc7d4db13c6ae08c5 to your computer and use it in GitHub Desktop.
Download PDF File using Alamofire - Swift - iOS Development
import Foundation
import Alamofire
class APIService {
static let shared = APIService()
func downloadPDF (completion: @escaping(_ error: Error?) -> Void) {
let destination: DownloadRequest.Destination = { _ , _ in
let documentURLs = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
let fileURL = documentURLs.appendingPathComponent("RandomFile\(Int.random(in: 1..<10000)).pdf")
return (fileURL, [.removePreviousFile, .createIntermediateDirectories])
}
AF.download(URL(string: "http://www.africau.edu/images/default/sample.pdf")!, method: .get, to: destination).downloadProgress(closure: { (progress) in
}).validate()
.response {response in
if response.error == nil {
debugPrint(response)
completion(nil)
}
else {
completion(response.error)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment