【发布时间】:2020-11-03 23:05:08
【问题描述】:
我在 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
【问题讨论】:
-
UIDocumentPickerViewController通常用于导入/打开文件。您应该首先将其保存在本地,然后使用UIDocumentIteractionController允许用户共享本地文件,但我从未使用 Catalyst 测试过。 stackoverflow.com/questions/46456481/…
标签: swift macos uidocumentpickerviewcontroller