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

如何让沙盒外文件保存操作填充Open Recent菜单?

问题描述

按照相关建议,填充“Open Recent”菜单十分简单:

UIDocument.open()会自动将打开的文件添加至“Open Recents”菜单

但沙盒外的文件导出操作有所不同,我使用如下代码将沙盒内的文件移至沙盒外:

let documentPicker = UIDocumentPickerViewController(forExporting: [urlOfFileInSandbox], asCopy: false)
topVC.present(documentPicker, animated: true)

该方案功能正常,但导出后的文件URL并未出现在“Open Recent”菜单中。请问是否有简便方法实现这一需求?(我可通过UIDocumentPickerDelegate获取导出后的URL)

解决方案

直接使用系统提供的LSAddItemToRecentDocuments API即可手动将文件添加到“Open Recent”菜单,具体操作如下:

  1. 引入CoreServices框架
  2. 在UIDocumentPickerDelegate的回调中获取导出后的目标URL,调用该API:
import CoreServices

func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
    guard let targetURL = urls.first else { return }
    LSAddItemToRecentDocuments(targetURL as CFURL, nil)
}

这是系统原生支持的方案,无需额外复杂逻辑,就能让导出的文件出现在“Open Recent”列表里。

内容的提问来源于stack exchange,提问作者Dan Rosenstark

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.20 09:39:22