【问题标题】:UIDocumentPickerViewController in SwiftUI on mac (macCatalyst)Mac 上 SwiftUI 中的 UIDocumentPickerViewController (macCatalyst)
【发布时间】: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()
    }
  }
}

在 iPad 上一切正常,

但是当在 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


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-08
    • 2021-09-10
    • 1970-01-01
    • 2021-03-22
    • 2020-10-09
    • 1970-01-01
    • 1970-01-01
    • 2020-12-10
    相关资源
    最近更新 更多