8

I have downloaded a pdf using code below. I am able to find the file in App Data Container, but from app data container my device needs Mac, x code or iTunes etc.

Can I give a distinction path to Documents OR another place to find the pdf in iPhone files? I have an option to open the file with iBook but it is not there.

My code to download the file is here:

func downloadFile(){
        let url = "https://www.tutorialspoint.com/swift/swift_tutorial.pdf"
        let fileName = "MyFile"
        
        savePdf(urlString: url, fileName: fileName)
      
    }

    func savePdf(urlString:String, fileName:String) {
            DispatchQueue.main.async {
                let url = URL(string: urlString)
                let pdfData = try? Data.init(contentsOf: url!)
                let resourceDocPath = (FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)).last! as URL
                let pdfNameFromUrl = "YourAppName-\(fileName).pdf"
                let actualPath = resourceDocPath.appendingPathComponent(pdfNameFromUrl)
                do {
                    try pdfData?.write(to: actualPath, options: .atomic)
                    print("pdf successfully saved!")
    //file is downloaded in app data container, I can find file from x code > devices > MyApp > download Container >This container has the file
                } catch {
                    print("Pdf could not be saved")
                }
            }
        }
Daniel Widdis
  • 8,424
  • 13
  • 41
  • 63
Muaaz Ahmed
  • 369
  • 1
  • 4
  • 21

1 Answers1

12

Configure your app so that its files appear in the Files app by adding below lines to your Info.plist file.

<key>UIFileSharingEnabled</key>
<true/>
<key>LSSupportsOpeningDocumentsInPlace</key>
<true/>

OR

Just like below using Xcode

enter image description here

Note: Remember that you must be running iOS 11 or above.

Prakash Shaiva
  • 1,047
  • 8
  • 12
  • Thanks @Prakash Shiva That worked with swift can you help me doing same thing in objective c I tried but it did't work there – Muaaz Ahmed Aug 19 '20 at 08:51
  • how can I Hide unwanted options from UIDocumentInteractionController appeared from this example currently it shows options : Copy, Markup, Print, AddTag, save file but I want to show only "Save File" option – Muaaz Ahmed Aug 20 '20 at 13:40
  • Where you are implementing UIDocumentInteractionController in your code?? – Prakash Shaiva Aug 20 '20 at 13:53
  • BTW you don’t need a UIDocumentInteractionController as per the requirement you mentioned in your question. – Prakash Shaiva Aug 20 '20 at 13:57
  • I am asking about the same question for which you provided me the solution The same code implemented posted in my Question, and then I added changes to pList as your guidance, That show me the UIDocumentInteractionController, now I want to customise this – Muaaz Ahmed Aug 20 '20 at 13:58