Created
December 13, 2021 07:15
-
-
Save AhmedMenaim/298760983efb578fc7d4db13c6ae08c5 to your computer and use it in GitHub Desktop.
Download PDF File using Alamofire - Swift - iOS Development
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
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