1

I am using PDFKit to make changes to annotations on a PDF file in an iOS application. When I try to save the file, I am getting no errors and the path looks correct, the print statement returns file:///var/mobile/Containers/Data/Application/6A9CF20F-7785-43F8-8015-C1D547A6236A/Documents/testpdf.pdf. However I cannot find the new PDF file using Files on the iPhone. Please see the code for the save function below:

func makethepdf() {
    let pdfView = PDFView()
    @ObservedObject var globalString = GlobalString()
    // Document
    guard let url = Bundle.main.url(forResource: "template", withExtension: "pdf") else {
        return
    }
    guard let document = PDFDocument(url: url) else {
        return
    }
    pdfView.document = document
    for index in 0..<document.pageCount {
        if let page = document.page(at: index) {
            let annotations = page.annotations
            for annotation in annotations {
                if annotation.fieldName == "Name" {
                    annotation.setValue(globalString.ownername, forAnnotationKey: .widgetValue)
                }
            }
        }
    }
    let paths = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
    let documentsDirectory = paths
    let savefile = documentsDirectory.appendingPathComponent("testpdf.pdf")
    print (savefile.absoluteString)
    if !document.write(to: savefile) {
        print ("file not saved")
    }
}

Solution: If you would like to expose your App document files inside Apple's Files App you need to include the "Supports Document Browser" key in your info plist file and set it to YES

0 Answers0