'Gmail app converting PDF on to Email not working properly on UIActivityViewController using PDFKit

Ran into some pdfkit issues when you share a PDF with UIActivityViewController and Gmail doesnt convert the PDF file properly it works on every other platform (Mail app, Yahoo Mail, Outlook, etc)

 if let pdfData = document.dataRepresentation() {
            print("Ran")
            let objectsToShare = [pdfData]
            let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
            activityVC.popoverPresentationController?.sourceView = self.shareBtn
            //activityVC.popoverPresentationController?.sourceRect = CGRect(x: 200, y: 200, width: 100, height: 100)
            activityVC.popoverPresentationController?.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY,width: 0,height: 0)
            //self.presentedViewController?.present(activityVC, animated: true)
            print("Print presented view")
            activityVC.isModalInPresentation = true
            self.present(activityVC, animated: true, completion: nil)
        }

Pdf file

SOLUTION:

 let temporaryFolder = FileManager.default.temporaryDirectory
    let fileName = "document.pdf" //whatever you want to call the file name
    let temporaryFileURL = temporaryFolder.appendingPathComponent(fileName)
    print("print: ", temporaryFileURL.path)


    let activityVC = UIActivityViewController(activityItems: [temporaryFileURL], applicationActivities: nil)
    activityVC.popoverPresentationController?.sourceView = self.shareBtn
    //activityVC.popoverPresentationController?.sourceRect = CGRect(x: 200, y: 200, width: 100, height: 100)
    activityVC.popoverPresentationController?.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY,width: 0,height: 0)
    //self.presentedViewController?.present(activityVC, animated: true)
    print("Print presented view")
    activityVC.isModalInPresentation = true
    self.present(activityVC, animated: true, completion: nil)
    


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source