首页 > 解决方案 > 使用 Mac Catalyst 打开 Finder

问题描述

我正在尝试使用 Mac Catalyst 打开 finder 并获取图像。

起初,我尝试了下面的代码,但 Xcode 说'NSOpenPanel' is unavailable in Mac Catalyst.

 private func selectFile() {
        NSOpenPanel.openImage { (result) in
            if case let .success(image) = result {
                self.image = image
            }
        }
    }

所以我尝试了这个解决方案,这次编译成功,但是当我点击按钮打开查找器时,我得到了这个错误信息:Thread 1: EXC_BREAKPOINT (code=EXC_I386_BPT, subcode=0x0)

我的最终代码如下

import SwiftUI

struct ContentView: View {
    var body: some View {
        VStack{
 
            Button("Choose file") {
                let picker = DocumentPickerViewController(
                    supportedTypes: ["log"],
                    onPick: { url in
                        print("url : \(url)")
                    },
                    onDismiss: {
                        print("dismiss")
                    }
                )
                UIApplication.shared.windows.first?.rootViewController?.present(picker, animated: true)
            }
            
        }
        
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

class DocumentPickerViewController: UIDocumentPickerViewController {
    private let onDismiss: () -> Void
    private let onPick: (URL) -> ()

    init(supportedTypes: [String], onPick: @escaping (URL) -> Void, onDismiss: @escaping () -> Void) {
        self.onDismiss = onDismiss
        self.onPick = onPick

        super.init(documentTypes: supportedTypes, in: .open)

        allowsMultipleSelection = false
        delegate = self
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension DocumentPickerViewController: UIDocumentPickerDelegate {
    func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
        onPick(urls.first!)
    }

    func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
        onDismiss()
    }
}

我该如何解决这个问题?

标签: swiftswiftuimac-catalyst

解决方案


在我的 macCatalyst 应用程序中,我使用 aUIViewControllerRepresentable来显示UIDocumentPickerViewController

@Binding到您的主视图,以便您可以从所选文件中引用该数据

struct DocumentImportViewController: UIViewControllerRepresentable {
@Binding var imgData:Data! //This doesn't need to be a "Data"

func makeCoordinator() -> Coordinator {
    return Coordinator(self)
}

func makeUIViewController(context: UIViewControllerRepresentableContext<DocumentImportViewController>) -> UIDocumentPickerViewController {
    let vc = UIDocumentPickerViewController(forOpeningContentTypes: [.image], asCopy: true)
    vc.delegate = context.coordinator
    return vc
}

func updateUIViewController(_ uiViewController: UIDocumentPickerViewController, context: UIViewControllerRepresentableContext<DocumentImportViewController>) {
    
}
}

代表协调员

extension DocumentImportViewController{
class Coordinator: NSObject, UIDocumentPickerDelegate {
    var parent: DocumentImportViewController
    
    init(_ parent: DocumentImportViewController) {
        self.parent = parent
    }

    func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
        parent.imgData = //get image from url
    }
}}

推荐阅读