Friends
I have generated a PDF document that I wish to make available to the Files app
This is my code that works well, in that it puts up the dialogue from which I can select "Save to files". (pdfURL
is the URL for the PDF document saved in the bundle. I can pass the data directly but it means I cannot programmatically set the file name)
Button("Share app") {
self.isSharePresented = true
}
.sheet(isPresented: $isSharePresented, onDismiss: {
print("Dismiss")
}, content: {
PDFSaveController(activityItems: [pdfURL])
})
But there is no directory for my App.
The user can create it (there is an option to create a directory), but my users will never see that and it will trigger endless angst.
I need to create the directory in the app.
I have followed https://stackoverflow.com/a/61631421/16675319 but the answer there is incorrect.
I have set the "Application supports iTunes file sharing" (Peculiar)
I use:
let documentDirectoryURLs = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
let documentDirectoryURL = documentDirectoryURLs[0]
let directoryURL = documentDirectoryURL.appendingPathComponent("TestPDF_Shared", isDirectory: true)
if !FileManager.default.fileExists(atPath: directoryURL.path) {
do {
try FileManager.default.createDirectory(at: directoryURL, withIntermediateDirectories: true, attributes: nil)
return directoryURL
} catch {
print(error.localizedDescription)
assert(false)
}
which creates a directory, but it is not visible in the files app.
There are other directories visible in the Files app from other applications that I have installed but I did not create those directories as an app user, the apps created them on installation (I assume).
I am using: iOS 15.2.1 Xcode Version 13.2.1 Swift