从包含文件的目录之一创建文件的安全范围书签 [英] Creating a security scope bookmark for a file from one of a directory containing it

查看:241
本文介绍了从包含文件的目录之一创建文件的安全范围书签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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

  NSURL * musicFolder = / *安全URL从NSData解析,书签不过时* /; 

if(![musicFolder startAccessingSecurityScopedResource]){
NSLog(@错误存取书签。
}

NSString * file = @myfile.txt; / *此文件存在于目录* /
NSURL * pathURL = [musicFolder URLByAppendingPathComponent:file];

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

[musicFolder stopAccessingSecurityScopedResource];

if(!bookmarkData){
NSLog(@%@,systemError);
}

两个 bookmarkData systemError end up nil这不是很有用...



这是支持的,

解决方案

在我的测试程序中,这个工作正常。我怀疑文件名附加到URL是失败在你的情况下(但这是一个巨大的猜测),因为它是唯一的东西看起来有很大的不同。



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



这是其他的原因,我不知道



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



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



它似乎工作得很好。 >




在我的测试中,它正在工作,我得到这样的文件的路径:

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

[resolvedURL startAccessingSecurityScopedResource];

NSArray * files = [[NSFileManager defaultManager]
contentsOfDirectoryAtURL:resolvedURL
includePropertiesForKeys:@ [NSURLLocalizedNameKey,NSURLCreationDateKey]
选项:NSDirectoryEnumerationSkipsHiddenFiles
错误:& ;错误];
if(files!= nil)
{
NSURL * fileURL = [files objectAtIndex:0]; // hard code for my quick test
NSData * newData = [fileURL bookmarkDataWithOptions:NSURLBookmarkCreationWithSecurityScope
includeResourceValuesForKeys:nil
relativeToURL:nil
error:&

if(newData!= nil)
{
NSLog(@it's good!
}
.... //错误检查和日志记录。

如果这不会让你在正确的轨道,我将需要看到更多代码(你可能需要做一个简单的例子)。



注意,在我的case我解决的书签,并调用 startAccessingSecurityScopedResource 即使我只是有url&创建书签(当我试图从我刚刚从PowerBox(openPanel)获取的路径创建一个书签它失败,错误256)。



某些配置详细信息:OS X 10.8.4,Xcode 5(从今天9/18/2013首次公开发布)。


I have a security scope bookmark for a directory, provided by a user via an openDialog request.

I'm trying to create another security scope bookmark for a file inside this directory:

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);
}

Both bookmarkData and systemError end up nil which is not very useful...

Is this even supported or can you only get valid secured scope bookmarks from the system?

解决方案

In my test program this works fine. I suspect the append of the file name to the URL is failing in your case (but that's a huge guess) because it's the only thing that seems materially different.

I notice that the url for a security resolved location is: file://localhost/Users/dad/Desktop/TestFolder?applesecurityscope=343335323030663066393432306234363030346263613464636464643130663635353065373030373b30303030303030303b303030303030303030303030303032303b636f6d2e6170706c652e6170702d73616e64626f782e726561642d77726974653b30303030303030313b30313030303030323b303030303030303030326461363838663b2f75736572732f74796c65722f6465736b746f702f74657374666f6c646572

which is the other reason I wonder if the append is the issue.

In my test I have the user choose the folder, create the security scoped bookmark and then save that in user defaults.

Then I quit & relaunch the app and via a menu command I get that bookmark and then resolve it. Then I added a case where I use the resolved bookmark to a folder and make a new bookmark to the file within the folder.

It seems to work fine.


In my test where it's working I'm getting the path to the file like this:

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.

if that doesn't get you on the right track, I'm going to need to see more code (you'll probably need to make a simple example).

Note that in my case I'm resolving the bookmark and calling startAccessingSecurityScopedResource even when I just got the url & created the bookmark (when I tried to create a bookmark from the path I'd just acquired from PowerBox (openPanel) it failed with an error 256).

Some configuration details: OS X 10.8.4, Xcode 5 (first public release from today 9/18/2013).

这篇关于从包含文件的目录之一创建文件的安全范围书签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆