为什么 UIDocumentPickerViewController 在 iOS 上工作,但在 Mac Catalyst 上显示错误的文件夹?

Why is UIDocumentPickerViewController working on iOS, but showing the wrong folder on Mac Catalyst?

下面的代码在 iPad 或 iPhone 上运行良好——saveDocument()(见下文)写入 /Users/me/Library/Containers/My-App/Data/Documents,而 openDocument() 向我展示了它的内容文件夹。

但是,在 macOS 上,openDocument() 显示 iCloud Drive 根级别的文档文件夹,而不是我计算机本地的沙盒版本

好像 documentPickerVC.directoryURL 被忽略了。这是为什么?

如果有帮助,文档 URL 如下所示:

file:///Users/me/Library/Containers/org.me.My-App/Data/Documents/

非常非常感谢任何帮助!

func saveDocument(){
    let encoder = JSONEncoder()
    encoder.outputFormatting = .prettyPrinted
    do {
        let puzzleData = try? encoder.encode(puzzle)
        print("Saving: \(documentURL)")
        let saveableURL = documentURL!.appendingPathComponent("\( .puzzle.date)")
        try puzzleData!.write(to: saveableURL)
    } catch {
        displayError(title: "Error while saving document", message: "There was an error: \(error)")
    }
}
    // when the user taps on the Open button
@objc func openDocument(){
    documentPickerVC = UIDocumentPickerViewController(forOpeningContentTypes: [UTType("public.item")!, UTType("public.data")!], asCopy: true )
    documentPickerVC.delegate = self
    documentPickerVC.modalPresentationStyle = .formSheet
    documentPickerVC.directoryURL = documentURL
    self.present(documentPickerVC, animated: true)
}

因此,documentURL 不起作用的原因是 Apple doesn't support it:具体来说,“此 属性 在使用 Mac Catalyst 构建的 Mac 应用程序中无效。 ”