In a sandboxed document based project for macOS, I would like to programmatically save on disk a document. The location have to be not accessible to the users: I would have thought of "CurrentFiles" or such, into Application Support folder. How can I create the folder?
This is the code I 'M using to date for testing:
let directoryURL = FileManager.default.urls(for: .desktopDirectory, in: .userDomainMask).first // then use .applicationSupportDirectory
let docURL = URL(string:"MyFile.test", relativeTo:directoryURL)
if (try? document.write(to: docURL!, ofType: "com.myCompany.test")) != nil {
} else {
print("An error occured")
}
You'll need to use a file URL. Try something like this:
do
{
// Find Application Support directory
let fileManager = FileManager.default
let appSupportURL = fileManager.urls(for: .applicationSupportDirectory, in: .userDomainMask).first!
// Create subdirectory
let directoryURL = appSupportURL.appendingPathComponent("com.myCompany.myApp")
try fileManager.createDirectory (at: directoryURL, withIntermediateDirectories: true, attributes: nil)
// Create document
let documentURL = directoryURL.appendingPathComponent ("MyFile.test")
try document.write (to: documentURL, ofType: "com.myCompany.test")
}
catch
{
print("An error occured")
}