You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Mac Catalyst平台SwiftUI .fileImporter无法触发回调、获取图片URL的问题排查求助

Mac Catalyst平台SwiftUI .fileImporter无法触发回调、获取图片URL的问题排查求助

大家好,我正在开发一款支持iOS和Mac Catalyst的跨平台SwiftUI应用,核心功能之一是通过原生的.fileImporter让用户选择图片文件。在iOS端测试时一切正常:能从文档选择器拿到图片URL,顺利跳转到后续页面。但切换到Mac Catalyst平台后,问题出现了:.fileImporter能正常打开,也可以选中.jpg、.png等格式的图片,但点击「打开」按钮后完全没有任何响应,结果处理的回调逻辑根本没触发,我既拿不到选中的图片URL,也无法执行后续的页面导航操作。

以下是我当前的实现代码,包含平台相关的分支处理:

iOS端核心调用逻辑

.sheet(isPresented: $displayDocumentPicker) {
    ImageFilePicker(onImagesPicked: { urls in
        print("HomeView: Received \(urls.count) URLs from ImageFilePicker. First URL: \(urls.first?.lastPathComponent ?? "N/A")")
        // showLoader = true
        DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
            let localURLs = urls.compactMap { persistLocally($0) }
            print("HomeView: After persistLocally, \(localURLs.count) local URLs. First local URL: \(localURLs.first?.lastPathComponent ?? "N/A")")
            // if !localURLs.isEmpty {
                pendingSelections.append(contentsOf: localURLs)
                print("HomeView: pendingSelections count updated to \(pendingSelections.count)")
                // showLoader = false
                navigateToFormatView = true
                print("HomeView: navigateToFormatView set to true. Navigating to FormatSelectionView.")
            // } else {
            //     print("HomeView: No local URLs were persisted. Not navigating.")
            //     showLoader = false
            // }
        }
    }, selectionLimit: 40)
}

Mac Catalyst端自定义实现

#elseif os(macOS)
import AppKit
import UniformTypeIdentifiers

// SwiftUI is already imported at the top of the file, but it's good practice to include
// platform-specific imports within their #if blocks if they are only needed there.
// For NSViewControllerRepresentable, SwiftUI is necessary.
struct ImageFilePicker: NSViewControllerRepresentable {
    var onImagesPicked: ([URL]) -> Void
    var selectionLimit: Int = 40

    func makeNSViewController(context: Context) -> NSViewController {
        let viewController = NSViewController()
        DispatchQueue.main.async {
            let panel = NSOpenPanel()
            if #available(macOS 12.0, *) {
                panel.allowedContentTypes = [
                    UTType.jpeg,
                    UTType.png,
                    UTType.heic,
                    UTType.gif,
                    UTType.tiff,
                    UTType.bmp,
                    UTType.webP
                ]
            } else {
                panel.allowedFileTypes = ["jpg", "jpeg", "png", "heic", "gif", "tiff", "bmp", "webp"]
            }
            panel.canChooseDirectories = false
            panel.allowsMultipleSelection = true
            
            if panel.runModal() == .OK {
                let selected = Array(panel.urls.prefix(selectionLimit))
                print("ImageFilePicker: Selected \(selected.count) URLs from NSOpenPanel. First URL: \(selected.first?.lastPathComponent ?? "N/A")")
                // onImagesPicked(selected)
            } else {
                print("ImageFilePicker: NSOpenPanel cancelled or failed.")
                //
            }
        }
        return viewController
    }

    func updateNSViewController(_ nsViewController: NSViewController, context: Context) {}
}
#endif

目前我已经检查了权限配置、UTI类型的设置,但还是找不到Mac Catalyst端回调不触发的原因,有没有遇到过类似问题的朋友能帮忙排查一下?

内容来源于stack exchange

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.07 12:29:31