【问题标题】:Why is UIDocumentPickerViewController working on iOS, but showing the wrong folder on Mac Catalyst?为什么 UIDocumentPickerViewController 在 iOS 上工作,但在 Mac Catalyst 上显示错误的文件夹?
【发布时间】:2021-04-07 11:14:45
【问题描述】:

下面的代码在 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)
}

【问题讨论】:

  • 在我的情况下,当执行self.documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:targetURL]; 崩溃:由于未捕获的异常“NSGenericException”而终止应用程序,原因:“UIDocumentInteractionController 不可用”。你怎么能解决这个问题?

标签: macos catalyst uidocumentpickerviewcontroller


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-22
    • 2021-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多