因此,我一直在“移动”一个小型SwiftUI iPad应用程序到Mac,并与UIDocumentPickerViewController发生了一点碰撞。我已经将UIDocumentPickerViewController封装在一个UIViewControllerRepresentable中,如下所示:
struct DocumentPickerView: UIViewControllerRepresentable {
func makeUIViewController(context: Context) -> UIDocumentPickerViewController {
let documentPicker = UIDocumentPickerViewController(documentTypes: [(kUTTypeImage as String)], in: .import)
return documentPicker
}
func updateUIViewController(_ uiViewController: UIDocumentPickerViewController, context: Context) {
}
}并以如下方式显示:
struct ContentView: View {
@State var shows = false
var body: some View {
Button(action: { self.shows.toggle() }) {
Text("Select File")
}
.sheet(isPresented: self.$shows) {
DocumentPickerView()
}
}
}On iPad all运行良好,

但是在Mac上,UIDocumentPickerViewController没有显示,我们得到了这个空白模式:

发布于 2022-05-02 17:56:09
我替换了不推荐的方法,并为提供的主题启动代码启用了询问权限,它可以工作。
用Xcode 13.3 / macOS 12.2测试
import UniformTypeIdentifiers
struct DocumentPickerView: UIViewControllerRepresentable {
func makeUIViewController(context: Context) -> UIDocumentPickerViewController {
let documentPicker = UIDocumentPickerViewController(forOpeningContentTypes: [UTType.image], asCopy: true)
return documentPicker
}
func updateUIViewController(_ uiViewController: UIDocumentPickerViewController, context: Context) {
}
}

并演示开放标准macOS选择器

发布于 2021-06-24 19:53:51
由于某些原因(我不知道更多的具体细节),以下内容对我起了作用(对于催化剂):
而不是使用in: .import
let documentPicker = UIDocumentPickerViewController(
documentTypes: [(kUTTypeImage as String)],
in: .import
)使用in: .open
let documentPicker = UIDocumentPickerViewController(
documentTypes: [(kUTTypeImage as String)],
in: .open
)我还没有检查功能是否在iOS中维护,但也许您可以使用某种“标志”来确定是使用.import还是.open
https://stackoverflow.com/questions/59078623
复制相似问题