12

因此,我一直在忙于将一个小型 SwiftUI iPad 应用程序“移动”到 Mac 上,并且在使用UIDocumentPickerViewController. 我已经像这样包裹UIDocumentPickerViewControllerUIViewControllerRepresentable

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()
    }
  }
}

在 iPad 上一切正常, iPad

但是当在 Mac 上时,UIDocumentPickerViewController不显示,我们得到这个空白模式:

苹果电脑

4

1 回答 1

0

出于某种原因(我不知道更多的具体细节),以下对我有用(对于催化剂):

而不是使用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

于 2021-06-24T19:53:51.880 回答