I built an iOS app in Swift, and I'm adding some functionality for macOS Catalyst.
In my app I create a .txt file upon clicking a button. I want to present a UIDocumentPickerViewController
that allows the user to specify the save directory, and file name. So far I am only able to display the UIDocumentPickerViewController
without the option to name the file or save. Is UIDocumentPickerViewController
the right view controller to accomplish this? If so, how do I specify the save directory and file name?
Here is the code I'm using to present the UIDocumentPickerViewController
#if targetEnvironment(macCatalyst)
let str = "Hello boxcutter"
let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0] as NSURL
let fileURL = documentsURL.appendingPathComponent("boxCutter.txt")
try! str.write(to: fileURL!, atomically: true, encoding: String.Encoding.utf8)
let types: [String] = [kUTTypeFolder as String]
let documentPicker = UIDocumentPickerViewController(documentTypes: types, in: .open)
documentPicker.delegate = self
documentPicker.allowsMultipleSelection = false
documentPicker.modalPresentationStyle = .formSheet
self.present(documentPicker, animated: true, completion: nil)
#endif