I'm trying to simply save a PDF to the device using UIActivityViewController.
I had to first create a temp item because if I just pass the PDF data, I don't have any way to change the name of the item.
The code I'm using (below) is extremenly simple, however, whenever I save the PDF to the "Files" app on the device, I get 2 copies of it!
Am I doing something wrong? Is this a bug?
@objc func downloadPdfTapped() {
if let fileURL = createTempPdfFile() {
let activityVC = UIActivityViewController(activityItems: [fileURL], applicationActivities: nil)
activityVC.excludedActivityTypes = [.assignToContact, .markupAsPDF]
present(activityVC, animated: true, completion: nil)
}
else {
print("\n\nERROR CREATING PDF\n\n")
}
}
private func createTempPdfFile() -> URL? {
let tempFile = "temp_statement.pdf"
guard let tempFolder = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else { return nil }
self.tempFileURL = tempFolder.appendingPathComponent(tempFile)
guard let currentFileURL = self.tempFileURL else { return nil }
do {
try pdfData.write(to: currentFileURL)
return currentFileURL
}
catch {
print(error.localizedDescription)
return nil
}
}