从包含它的目录之一为文件创建安全范围书签

Dam*_*kis 11 cocoa sandbox appstore-sandbox

我有一个目录的安全范围书签,由用户通过openDialog请求提供.

我正在尝试为此目录中的文件创建另一个安全范围书签:

NSURL *musicFolder = /* Secured URL Resolved from a NSData, bookmark not stale */;

if (![musicFolder startAccessingSecurityScopedResource]) {
    NSLog(@"Error accessing bookmark.");
}

NSString *file = @"myfile.txt"; /* This file exists inside the directory */
NSURL *pathURL = [musicFolder URLByAppendingPathComponent:file];

NSError *systemError;
NSData *bookmarkData = [pathURL bookmarkDataWithOptions:NSURLBookmarkCreationWithSecurityScope
                         includingResourceValuesForKeys:nil
                                          relativeToURL:nil
                                                  error:&systemError];

[musicFolder stopAccessingSecurityScopedResource];

if (!bookmarkData) {
    NSLog(@"%@", systemError);
}
Run Code Online (Sandbox Code Playgroud)

双方bookmarkDatasystemError最终为零这是不是很有用...

这甚至是支持还是只能从系统获得有效的安全范围书签?

Dad*_*Dad 6

在我的测试程序中,这很好用.我怀疑在你的情况下文件名附加到URL是失败的(但这是一个巨大的猜测),因为它是唯一看起来实质上不同的东西.

我注意到,为解决位置的安全网址是:文件://本地主机/用户/爸爸/桌面/ TestFolder applesecurityscope = 343335323030663066393432306234363030346263613464636464643130663635353065373030373b30303030303030303b303030303030303030303030303032303b636f6d2e6170706c652e6170702d73616e64626f782e726561642d77726974653b30303030303030313b30313030303030323b303030303030303030326461363838663b2f75736572732f74796c65722f6465736b746f702f74657374666f6c646572

这是我想知道追加是否是问题的另一个原因.

在我的测试中,我让用户选择文件夹,创建安全范围的书签,然后将其保存在用户默认值中.

然后我退出并重新启动应用程序,通过菜单命令,我得到该书签,然后解决它.然后我添加了一个案例,我将已解析的书签用于文件夹,并为该文件夹中的文件创建一个新书签.

它似乎工作正常.


在我的测试工作中,我正在获取文件的路径,如下所示:

NSURL * resolvedURL = [NSURL URLByResolvingBookmarkData: data
                        options: NSURLBookmarkResolutionWithSecurityScope
                        relativeToURL: nil
                        bookmarkDataIsStale: &isStale
                        error: &error];
... // (error checking)

[resolvedURL startAccessingSecurityScopedResource];

NSArray * files = [[NSFileManager defaultManager] 
                     contentsOfDirectoryAtURL: resolvedURL
                     includingPropertiesForKeys: @[NSURLLocalizedNameKey, NSURLCreationDateKey]
                     options:  NSDirectoryEnumerationSkipsHiddenFiles
                     error: &error];
if ( files != nil )
{
    NSURL * fileURL = [files objectAtIndex: 0]; // hard coded for my quick test
    NSData * newData = [fileURL bookmarkDataWithOptions: NSURLBookmarkCreationWithSecurityScope
                          includingResourceValuesForKeys: nil
                          relativeToURL: nil
                          error: &error];

   if ( newData != nil )
   {
       NSLog(@"it's good!");
   }
   .... // error checking and logging.
Run Code Online (Sandbox Code Playgroud)

如果这不能让你走上正轨,我将需要看到更多的代码(你可能需要做一个简单的例子).

请注意,在我的情况下,我正在解析书签和调用,startAccessingSecurityScopedResource即使我刚刚获得网址并创建了书签(当我尝试从我刚从PowerBox(openPanel)获取的路径创建书签时,它失败并出现错误256).

一些配置细节:OS X 10.8.4,Xcode 5(从今天9/18/2013开始的第一个公开发布).


小智 5

要为锁定文件创建书签,请在用于创建书签的 API 调用中结合使用NSURLBookmarkCreationSecurityScopeAllowOnlyReadAccess标志和NSURLBookmarkCreationWithSecurityScope标志。

例如:

NSURL* fileURL = [NSURL fileURLWithPath:filePath];
NSError* error = NULL;

NSData* bookmarkData = [fileURL bookmarkDataWithOptions:NSURLBookmarkCreationWithSecurityScope|NSURLBookmarkCreationSecurityScopeAllowOnlyReadAccess includingResourceValuesForKeys:nil relativeToURL:nil error:&error];
Run Code Online (Sandbox Code Playgroud)

我在 Mac OS 10.9.5 中尝试过这个