我在 Swift 中构建了一个 iOS 应用程序,并且正在为 macOS Catalyst 添加一些功能。
在我的应用程序中,我在单击按钮时创建了一个 .txt 文件。我想提出一个UIDocumentPickerViewController
允许用户指定保存目录和文件名的方法。到目前为止,我只能显示UIDocumentPickerViewController
没有命名文件或保存的选项。是UIDocumentPickerViewController
正确的视图控制器来完成这个吗?如果是这样,我如何指定保存目录和文件名?
这是我用来展示的代码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