【问题标题】:UIDocumentPickerViewController with Catalyst on MACOS在 MACOS 上使用 Catalyst 的 UIDocumentPickerViewController
【发布时间】:2020-02-13 21:39:09
【问题描述】:

我在 MacOS 中使用 Catalyst 显示选择器的代码:

final class DocumentPicker: NSObject, UIViewControllerRepresentable, ObservableObject {
    typealias UIViewControllerType = UIDocumentPickerViewController
    @Published var urlsPicked = [URL]()

    lazy var viewController:UIDocumentPickerViewController = {
        // For picked only folder
        let vc = UIDocumentPickerViewController(documentTypes: ["public.folder"], in: .open)
        vc.allowsMultipleSelection = false
        vc.delegate = self
        return vc
    }()        
    ........

和:

struct ContentView: View {
    @ObservedObject var picker = DocumentPicker()
    @State private var urlPick = ""

    var body: some View {
        HStack {
            Text(urlPicked())
                .padding()
                .overlay(
                    RoundedRectangle(cornerRadius: 10)
                        .stroke(Color.white, lineWidth: 1)
                )
            TextField("", text: $urlPick)
                .textFieldStyle(RoundedBorderTextFieldStyle())
                .font(.system(size: 10))
                .disabled(true)
            Spacer()
            Button(action: {
                #if targetEnvironment(macCatalyst)
                let viewController = UIApplication.shared.windows[0].rootViewController!
                viewController.present(self.picker.viewController, animated: true)
                self.picker.objectWillChange.send()
                #endif
                print("Hai premuto il pulsante per determinare il path della GeoFolder")
            }) {
                Image(systemName: "square.and.arrow.up")
            }
        }
        .padding()
    }

    private func urlPicked() -> String {
        var urlP = ""
        if picker.urlsPicked.count > 0 {
            urlP = picker.urlsPicked[0].path
            urlPick = picker.urlsPicked[0].path
        }
        return urlP
    }
}

如果我运行上面的代码,我会在text 中得到选择的正确路径,而在textfield 中什么也没有,而且我在urlPick = picker.urlsPicked[0].path 中也有错误:Modifying state during view update, this will cause undefined behavior. 如何修改代码以显示在 textfield 中也选择的正确路径?

【问题讨论】:

    标签: macos swiftui mac-catalyst


    【解决方案1】:

    在 urlPick = picker.urlsPicked[0].path 中有错误:正在修改状态 在视图更新期间,这将导致未定义的行为。我怎样才能 修改代码以显示在文本字段中选择的正确路径?

    试试下面的

        if picker.urlsPicked.count > 0 {
            urlP = picker.urlsPicked[0].path
            DispatchQueue.main.async {
                urlPick = picker.urlsPicked[0].path
            }
        }
    

    【讨论】:

      【解决方案2】:

      对于任何寻求创建具有只读权限的 MacOS 文档选择器的人,请使用以下解决方案:

      import Foundation
      import UIKit
      
      extension ViewController: UIDocumentBrowserViewControllerDelegate, UIDocumentPickerDelegate {
          
          @objc func presentDocumentPicker() {
              
              if operatingSystem == .macintosh {
                  let documentPicker = UIDocumentBrowserViewController(forOpening: [.pdf])
                  documentPicker.delegate = self
                  documentPicker.allowsDocumentCreation = false
                  documentPicker.allowsPickingMultipleItems = false
                  // Present the document picker.
                  present(documentPicker, animated: true, completion: nil)
              } else {
                  let documentsPicker = UIDocumentPickerViewController(forOpeningContentTypes: [.pdf])
                  documentsPicker.delegate = self
                  documentsPicker.allowsMultipleSelection = false
                  documentsPicker.modalPresentationStyle = .fullScreen
                  self.present(documentsPicker, animated: true, completion: nil)
              }
      
          }
          
          
          func documentBrowser(_ controller: UIDocumentBrowserViewController, didPickDocumentsAt documentURLs: [URL]) {
              guard let url = documentURLs.first, url.startAccessingSecurityScopedResource() else { return }
              defer {
                  DispatchQueue.main.async {
                      url.stopAccessingSecurityScopedResource()
                  }
              }
              debugPrint("[DocumentPicker] Selected Item with URL : ", url)
              controller.dismiss(animated: true)
          }
          
          public func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
              guard let url = urls.first, url.startAccessingSecurityScopedResource() else { return }
              defer {
                  DispatchQueue.main.async {
                      url.stopAccessingSecurityScopedResource()
                  }
              }
              debugPrint("[DocumentPicker] Selected Item with URL : ", url)
              controller.dismiss(animated: true)
          }
      
          public func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
              controller.dismiss(animated: true)
          }
      }

      请注意,如果权利是读写的(即您还允许用户将文件保存到计算机),那么您可以简单地使用 UIDocumentPicker(我的 sn-p 中的非 .macintosh 示例)。

      【讨论】:

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