NSURL URLByResolvingBookmarkData 似乎随机失败

Bit*_*ank 5 nsurl ios

我的应用程序是 iOS 的图像查看器。我允许用户从相机胶卷或云文件提供商中选择文件。对于选择的每个文件,我创建一个书签,以便用户稍后返回查看该文件,而无需再次执行相机胶卷/云文件选择器的所有步骤。相机胶卷书签始终工作,但云文件提供程序书签会随机失败,因为从调用返回 nil URLByResolvingBookmarkData。返回的错误是:

“文件未找到”

来自 iCloud、DropBox、Box、Google Drive 等的文件会发生这种情况。以下是我创建和检索书签的方法:

// bookmark created from url returned by UIDocumentPickerViewController
NSData *bookmark = [url bookmarkDataWithOptions:options includingResourceValuesForKeys:nil relativeToURL:url error: nil];

// retrieving the URL from this bookmark works for a short time, then returns nil
url = [NSURL URLByResolvingBookmarkData:bookmark options:NSURLBookmarkResolutionWithoutUI relativeToURL:nil bookmarkDataIsStale:&isStale error:&error];
Run Code Online (Sandbox Code Playgroud)

任何人都可以解释为什么书签会工作一段时间,然后就失败了?存储/RAM 有限?

我有一种感觉,答案是从云存储中保留文件的本地副本,因为不能保证它们可以多次访问。

Dan*_*ull 1

您应该startAccessingSecurityScopedResource在 URL 上使用,确保stopAccessingSecurityScopedResource完成后调用。

或者,Apple 强烈建议使用 FileCoordinator 或 UIDocument 来管理对文件的访问。特别是现在您可以访问其他应用程序也在访问的文件。