无法创建Security-Scoped Bookmark

时间:2012-04-06 12:15:41

标签: objective-c macos osx-lion nsopenpanel appstore-sandbox

我正在将我的Lion应用转换为使用App Sandbox。我正在尝试使用10.7.3中引入的security-scoped bookmarks功能来允许对文件夹的持久访问。我下面的代码返回一个零书签,并生成以下日志消息:XPC couldn't look up the Mach service for scoped bookmarks agent

我将User Selected File Access权利设置为Read/Write Access,并尝试使用和不使用周围的..AccessingSecurityScopedResource来电。

我认为我根据文档做的一切正常,所以我很感激任何指针。在开始沙盒化应用程序之前,代码正在检索一个普通的URL。

NSOpenPanel *openPanel = [NSOpenPanel openPanel];
[openPanel setCanChooseFiles:NO];
[openPanel setCanChooseDirectories:YES];
[openPanel setAllowsMultipleSelection:NO];

NSInteger result = [openPanel runModal];

if( result == NSFileHandlingPanelCancelButton ) {
    return;
}

NSArray *urls = [openPanel URLs];

if( urls != nil && [urls count] == 1 ) {
    NSURL *url = [urls objectAtIndex:0];

    NSData *bookmark = nil;
    NSError *error = nil;
    bookmark = [url bookmarkDataWithOptions:NSURLBookmarkCreationWithSecurityScope
             includingResourceValuesForKeys:nil
                              relativeToURL:nil // Make it app-scoped
                                      error:&error];
    if (error) {
        NSLog(@"Error creating bookmark for URL (%@): %@", url, error);
        [NSApp presentError:error];
    }

    NSLog(@"bookmark: %@", bookmark);
}

更新(x3)

现在我已经开始工作,我可以验证在上面的代码中不需要调用-startAccessingSecurityScopedResource-stopAccessingSecurityScopedResource,因为Powerbox在用户选择资源后授予对资源的访问权限NSOpenPanel

如果您要从另一个安全范围的URL创建书签,例如从另一个应用会话中创建的应用范围书签制作文档范围的书签,则需要先访问该文件。

1 个答案:

答案 0 :(得分:9)

事实证明我错过了一项重要的权利,未在用户界面中列出,但列在the documentation中:

com.apple.security.files.bookmarks.app-scope

更新12/18/2018

根据this Twitter thread,可能不再需要此权利。感谢@pkamb提醒我注意这一点。