【发布时间】:2012-10-12 00:55:04
【问题描述】:
我的 mac os 应用程序通过用户交互(拖放)获取别名的 NSURL,因此该应用程序有权读取别名文件,但它无权读取应用程序沙箱中的原始文件(Mac OS X 10.7/8)。
我通过
解析别名NSData* bookmark = [NSURL bookmarkDataWithContentsOfURL:aliasURL error:nil];
origURL = [NSURL URLByResolvingBookmarkData:bookmark
options:NSURLBookmarkResolutionWithoutUI
relativeToURL:nil
bookmarkDataIsStale:nil
error:&error];
当我尝试读取 origURL 文件时,我收到错误:The file couldn’t be opened because you don’t have permission to view it。
我也尝试在 origURL 上调用start/stopAccessingSecurityScopedResource,但没有帮助。
我也尝试使用 NSURLBookmarkResolutionWithSecurityScope 选项解析书签数据,但从 URLByResolvingBookmarkData 方法中得到 "The file couldn’t be opened because it isn’t in the correct format." 错误。
那么,怎么做呢?谢谢。
【问题讨论】:
-
最初创建的
NSURL是否具有安全范围?如果您有生成原始别名 URL 的代码,那么查看它会很有帮助。 URL 来自哪些应用程序? -
别名文件由Finder创建。我从 NSPasteboard(NSDraggingInfo),
aliasURL = [NSURL URLFromPasteboard:pb]获得别名文件的 URL -
从 OS X 10.10 开始有一个新的
+ URLByResolvingAliasFileAtURL:options:error:。然而,它声明NSURLBookmarkResolutionWithSecurityScope选项不受支持。所以,仍然无法通过别名访问。