在 macOS 上打开 PDF 而不留下任何痕迹 [Devlog #9]

发布: (2026年4月23日 GMT+8 19:59)
2 分钟阅读
原文: Dev.to

Source: Dev.to

背景

所有测试均在一台 8 年老的 MacBook Air 上进行。
在 macOS 上打开 PDF 时,操作系统会悄悄在至少五个位置记录该文件:

  • 最近文档 (NSDocumentController)
  • Finder 的最近项目 (LSSharedFileList)
  • 应用缓存目录
  • QuickLook 缩略图缓存
  • ~/Library/Caches/ 下的渲染缓存

macOS 如何注册已打开的文件

macOS 会通过 NSDocumentController 自动注册已打开的文件。
在 Swift 端覆盖此行为即可抑制最近文档条目:

@implementation HiyokoSanctuaryDocument

- (void)addToRecentDocuments {
    // intentionally empty
}

+ (BOOL)autosavesInPlace {
    return NO;
}

@end

Sanctuary Viewer – 零痕迹 PDF 查看器

查看器将所有数据保存在内存中,并在关闭时删除任何临时文件。

pub struct SanctuaryViewer {
    page_cache: HashMap>, // example cache type
    temp_files: Vec,
}

impl Drop for SanctuaryViewer {
    fn drop(&mut self) {
        // Delete any temp files on close
        for path in &self.temp_files {
            let _ = std::fs::remove_file(path);
        }
        // Zero out in‑memory cache
        self.page_cache.clear();
    }
}

Drop 实现会在查看器关闭的瞬间触发,因此无需手动清理。

避免 QuickLook 缓存

macOS 会在后台运行 QuickLook,并自动将缩略图写入 ~/Library/Caches/com.apple.QuickLook.thumbnailcache
解决办法是 永不将文件路径传递给 QuickLook。所有渲染都通过内部的 Ghost Engine 在内存中完成,文件路径永远不会到达任何会触发缓存的 macOS API。

使用方法

  1. 使用 Sanctuary Viewer 打开 PDF。
  2. 关闭查看器。

从 macOS 的角度来看,文件从未被打开——没有最近文档条目、没有 Finder 历史,也没有缩略图缓存。

下载

Hiyoko PDF Vault


在 Twitter 上关注项目:@hiyoyok

0 浏览
Back to Blog

相关文章

阅读更多 »