-1

I am doing a scanner app that scans a picture and converts it into a PDF. I want to be able to save it to "On My IPhone" but I do not know that path URL. Here is what I have so far:

let data = pdfDocument.dataRepresentation()
            let documentsDirectoryURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
            let currentTimeStamp = String(Int(NSDate().timeIntervalSince1970))
            let docURL = documentsDirectoryURL.appendingPathComponent("Scan\(currentTimeStamp).pdf")

            do{
            try data?.write(to: docURL)
            }catch(let error)
            {
                print("error is \(error.localizedDescription)")
            }

However, the I cannot find my file on my phone because I do not know what "Directory" URL is. How can I get my PDF to save to my iPhone Storage? What is the URL to my "On My IPhone" folder?

Thanks in advance.

Galen BlueTalon
  • 173
  • 4
  • 20

1 Answers1

1

In order to retrieve the urls of the files contained in documents directory, you can use the following code snippet.

guard let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else { return }

do {
    let directoryContents = try FileManager.default.contentsOfDirectory(at: documentsDirectory, includingPropertiesForKeys: nil, options: [])

    // Print the urls of the files contained in the documents directory
    print(directoryContents)
} catch {
    print("Could not search for urls of files in documents directory: \(error)")
}
user3233109
  • 206
  • 2
  • 3