I have the following code that allows me to download a PDF file from a URL, it works correctly:
class ViewController: UIViewController {
@IBOutlet weak var progressView: UIProgressView!
override func viewDidLoad() {
let _ = DownloadManager.shared.activate()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
DownloadManager.shared.onProgress = { (progress) in
OperationQueue.main.addOperation {
self.progressView.progress = progress
}
}
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
DownloadManager.shared.onProgress = nil
}
@IBAction func startDownload(_ sender: Any) {
let url = URL(string: "https://d0.awsstatic.com/whitepapers/KMS-Cryptographic-Details.pdf")!
let task = DownloadManager.shared.activate().downloadTask(with: url)
task.resume()
}
}
The file is currently going to: file:///Users/cybermac/Library/Developer/CoreSimulator/Devices/CAEC75D0-423A-4FB2-B0D6-9E7CADB190A1/data/Containers/Data/Application/8B5CBFC8-7058-48DB-A1C4-872302A80610/Library/Caches/com.apple.nsurlsessiond/Downloads/com.example.DownloadTaskExample/CFNetworkDownload_Q7OVlf.tmp
How do I save it in /Documents/
?
Something like this: file:///Users/cybermac/Library/Developer/CoreSimulator/Devices/CAEC75D0-423A-4FB2-B0D6-9E7CADB190A1/data/Containers/Data/Application/64370B29-2C01-470F-AE76-17EF1A7BC918/Documents/
The idea is that the file saved in that directory can be used to read it offline (with PDFKit or webKit). It will only be deleted if the application is deleted.
PDF files on your Android device are stored in the Downloads folder. However, some apps may send their PDF files to the Documents folder instead. You can access these through your File Manager, by going to internal storage and then "Downloads" or "Documents".
After editing, click the File menu but instead of just hitting Save, choose the Save As option. 4. When File Explorer opens, navigate to the desired folder and select it. Click Save to save the PDF in that specific folder.
In the PDF documents section, under Default behavior, you may choose between Download PDFs to send downloads to your Downloads folder or Open PDFs in Chrome to open them in a new tab.
If you don’t have a dedicated PDF editor such as PDFelement, the easiest way to save a PDF file to a specific folder location is to open it in Chrome. Google Chrome is also a PDF reader, so when you have a document open in Chrome, you can directly download it using the icon. Here are the steps: 1.
If you’ve been saving to a specific folder, you’ll see that the recent folder will be the default location for the next PDF that you save. The process works this way. 1. Open a PDF file and then save it to the new folder location. 2. Now, click File → Open or use the Open File folder icon at the top to select and open a new file.
PDFelement has a powerful conversion tool to create PDFs from over 300 different file types, so you may find this useful at some point. 1. On the welcome page, click the Create PDF icon. 2. Drag and drop the file to be converted to PDF. 3. When the file is open, click File → Save As 4. Choose the location and hit Save. Part 4.
The file should automatically open in the browser window. To save (download) the PDF, click the icon, located near the top-right corner of the browser window. Similar to Microsoft Edge Legacy, Internet Explorer displays PDF files by default, instead of offering to download them for you. However, downloading a PDF is still possible.
You need to move the file to your custom location after the download. Implement URLSessionDownloadDelegate
and you will receive the location of your downloaded file.
Delegate method:
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL)
Code to move the file:
do {
let documentsURL = try
FileManager.default.url(for: .documentDirectory,
in: .userDomainMask,
appropriateFor: nil,
create: false)
let savedURL = documentsURL.appendingPathComponent("yourCustomName.pdf")
try FileManager.default.moveItem(at: location, to: savedURL)
} catch {
print ("file error: \(error)")
}
To learn more refer to this repo.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With