【发布时间】:2020-03-23 12:09:45
【问题描述】:
所以我一直在忙于将一个小型 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()
}
}
}
但是当在 Mac 上时,UIDocumentPickerViewControllerdoesnt 显示并且我们得到这个空白模式:
【问题讨论】:
-
如果我将 UIKit 与 Catalyst 一起使用,它会打开一个 macos 文件选择器。当使用带有催化剂的 SwiftUI 时,我得到了空白视图。我希望 SwiftUI 有同样的行为。
-
我无法重现您的问题。它显示了您在这里的期望。我遇到了另一个选择文件的问题。它什么也不做并记录错误:
Failed to create an FPSandboxingURLWrapper for file ... Error: Error Domain=NSPOSIXErrorDomain Code=1 "couldn't issue sandbox extension com.apple.app-sandbox.read-write for '/..fileName': Operation not permitted" UserInfo={NSDescription=couldn't issue sandbox extension com.apple.app-sandbox.read-write for '/../fileName.png': Operation not permitted}。改用UIDocumentBrowserViewController可以解决我的问题。 -
我遇到了同样的问题。这必须是 SwiftUI 中的一个错误。它应该真正将 UIDocumentPickerViewController 转换为 macOS 上的 NSOpenPanel。肯定有错误!
-
如果您像我一样使用基于文档的应用程序,您可以通过让 UIDocumentBrowserViewController 显示对话框来解决此问题。这很痛苦......但至少它有效;-)
-
let controller = UIDocumentPickerViewController(url: tempURL, in: .moveToService) controller.delegate = self if let presentedViewController = self.presentedViewController { // let the ContentView present the self.modalSelection = .save presentedViewController.present(controller, animated: true) }
标签: ios swift macos swiftui mac-catalyst