How to save document to "Files" App in swift

Minimum of iOS 11

The Files app was introduced in iOS 11. You will not be presented with the option to save to Files when you are on a version less than iOS 11. With that being said, documents can be downloaded to a variety of different apps depending on the settings available on the user's phone and the options you allow.

@available(iOS 11.0, *)
func savePDF() {
    guard let documentData = document.dataRepresentation() else { return }
    let activityController = UIActivityViewController(activityItems: [documentData], applicationActivities: nil)
    self.present(activityController, animated: true, completion: nil)
}

This is an example of a way to save a document. You can't force it to be downloaded and show up in their Files app. You have to present a UIActivityViewController that shows options on what they can do with that document.

If you want to exclude any options shown on the UIActivityViewController, then implement excludedActivityTypes like so.

activityController.excludedActivityTypes = [.airDrop, .postToTwitter]

If you have access to your PDFdata then you could present the user with a UIActivityViewController where they can then save it to Files, or share it with any of the other available options:

let activityViewController = UIActivityViewController(activityItems: ["Name To Present to User", pdfData], applicationActivities: nil)
present(activityViewController, animated: true, completion: nil)

enter image description here

Tags:

Xcode

Swift