34

I just created a PDF document in my app and I want to save the document on the device in a way that the "Files" app can access the saved document.

When you download a PDF document in Safari there is an option to save the document to the "Files" app, which is what I'm trying to do.

I Googled for several hours but wasn't able to find any useful methods.

Cronay
  • 4,875
  • 3
  • 13
  • 18
ARUN KUMAR
  • 428
  • 1
  • 8
  • 19

3 Answers3

34

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

pkamb
  • 33,281
  • 23
  • 160
  • 191
rbaldwin
  • 4,581
  • 27
  • 38
16

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]
Marwen Doukh
  • 1,946
  • 17
  • 26
Devbot10
  • 1,193
  • 18
  • 33
1

Try UIDocumentInteractionController:

let doc = UIDocumentInteractionController(url: url)
doc.presentOptionsMenu(from:in:animated:)

There's an option named save to Files.

Klein Mioke
  • 1,261
  • 2
  • 14
  • 23