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

问题描述

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

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

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

如果有帮助,documentURL 如下所示:

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 应用程序中无效。”