天道酬勤,学无止境

security-scoped-bookmarks

处理陈旧的 NSURL 书签的正确方法是什么?(What is the correct way to handle stale NSURL bookmarks?)

问题 从安全范围的书签解析 NSURL 时,如果用户重命名或移动了该文件或文件夹,则书签将失效。 苹果公司的文件说这是关于陈旧的: 过时返回时,如果是,则书签数据已过时。 您的应用程序应该使用返回的 URL 创建一个新书签,并使用它代替现有书签的任何存储副本。 不幸的是,这对我来说很少有用。 它可能在 5% 的时间内起作用。 尝试使用返回的 URL 创建新书签会导致错误,代码 256,在控制台中查看会显示来自沙盒的消息,说拒绝更新 URL 上的文件读取数据。 注意如果重新生成书签确实有效,它似乎只在第一次重新生成时有效。 如果文件夹/文件再次移动/重命名,它似乎永远不会工作。 我最初如何创建和存储书签 -(IBAction)bookmarkFolder:(id)sender { _openPanel = [NSOpenPanel openPanel]; _openPanel.canChooseFiles = NO; _openPanel.canChooseDirectories = YES; _openPanel.canCreateDirectories = YES; [_openPanel beginSheetModalForWindow:self.window completionHandler:^(NSInteger result) { if (_openPanel.URL

2021-06-12 03:35:07    分类:技术分享    cocoa   nsurl   security-scoped-bookmarks

What is the correct way to handle stale NSURL bookmarks?

When resolving an NSURL from a security scoped bookmark, if the user has renamed or moved that file or folder, the bookmark will be stale. Apple's document says this regarding staleness: isStale On return, if YES, the bookmark data is stale. Your app should create a new bookmark using the returned URL and use it in place of any stored copies of the existing bookmark. Unfortunately, this rarely works for me. It may work 5% of the time. Attempting to create a new bookmark using the returned URL results in an error, code 256, and looking in Console reveals a message from sandboxd saying deny file

2021-05-16 18:51:10    分类:问答    cocoa   nsurl   security-scoped-bookmarks