UIActivityViewController saving 2 identical PDFs

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
        }
    }
Not a bug, but I really didn't find much documentation or anything about it.

The reason I was getting 2 copies is because I was saving my "temp" file in the Documents directory instead of the temp directory.

So, for the tempFolder, use this instead:

Code Block Swift
let tempFolder = FileManager.default.temporaryDirectory


I hope this helps anyone else who may come across this.


UIActivityViewController saving 2 identical PDFs
 
 
Q