iosswiftqlpreviewcontroller

QLPreviewController - Hide 'Save to Files' option


I am trying to open a PDF from the app in QLPreviewController. Following is the code:

func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem {
    return getFileURLFromFileModel()! as QLPreviewItem
}

func displayPDFForAnnotation(){
    let previewController = QLPreviewController()
    previewController.dataSource = self
    self.present(previewController, animated: true)
}

It's behaving as expected. Then if I add an annotation, that is, if I edit the PDF and then press the "Done" button, then it's asking to "Save to Files"

Here is the image for reference:

image

I don't want the option of "Save to Files" as it would be saved within the app on the press of "Done" button itself. How can I hide the "Save to Files" option from the "Done" button menu?


Solution

  • You should implement the delegate method previewController(_:editingModeFor:).

    func previewController(_ controller: QLPreviewController, editingModeFor previewItem: QLPreviewItem) -> QLPreviewItemEditingMode {
        return .updateContents // Save updates to original file
    }
    
    func previewController(_ controller: QLPreviewController, didUpdateContentsOf previewItem: QLPreviewItem) {
        // The original file has been updated
    }
    
    func previewController(_ controller: QLPreviewController, didSaveEditedCopyOf previewItem: QLPreviewItem, at modifiedContentsURL: URL) {
        // The original file couldn't be overwritten do it has been saved at URL
    }
    

    The documentation states:

    If you want to update the underlying file in place, return QLPreviewItemEditingMode.updateContents, and implement both the previewController(_:didUpdateContentsOf:) and previewController(_:didSaveEditedCopyOf:at:) delegate methods. The Quick Look feature calls previewController(_:didUpdateContentsOf:) first and updates the preview item’s content. If it can’t update the item directly, it invokes previewController(_:didSaveEditedCopyOf:at:) and creates a copy of the preview item’s content that contains the edits.

    Note that if you want the original PDF to be updated when the user saves, make sure the PDF is writable. This means that it can't be a PDF stored in the app's resource bundle.