macos - 如何创建要保存到新文档的文档范围书签?

标签 macos cocoa appstore-sandbox

我有一个基于沙盒文档的 Mac 应用程序。我的文档包含用户计算机上的图像。我想将文档范围的书签保存到文档中使用的图像,以便在关闭并重新打开文档时可以访问该图像。

以下是我创建书签的方法:

 //path is path to an image
 //for a new document docUrl is set to the location where we will save our document
 NSURL * pathUrl = [NSURL fileURLWithPath:path];
 NSError * error;
 NSData * pathBookmarkData = [pathUrl bookmarkDataWithOptions:
                              (NSURLBookmarkCreationWithSecurityScope 
                             | NSURLBookmarkCreationSecurityScopeAllowOnlyReadAccess)
                              includingResourceValuesForKeys:[NSArray arrayWithObject:NSURLPathKey] 
                             relativeToURL:docUrl error:&error];

这会导致以下错误:

Error Domain=NSCocoaErrorDomain Code=260 "The file “Untitled.mydocext” couldn’t be opened because there is no such file." (Collection URL points to a file that doesn't exist) UserInfo=0x608000070800 {NSURL=file:///Users/myname/Pictures/Untitled.mydocext, NSDebugDescription=Collection URL points to a file that doesn't exist}

如何创建要保存到新文档的文档范围书签?

这是我获取路径的方法:

NSArray* fileTypes = [[NSArray alloc] initWithObjects:@"png", @"jpg", @"jpeg", @"bmp", @"gif", @"tif", @"tiff", @"PNG", @"JPG", @"JPEG", @"BMP", @"GIF", @"TIF", @"TIFF", nil];

NSOpenPanel *panel;

panel = [NSOpenPanel openPanel];
[panel setTitle:@"Select Photos"];

[panel setFloatingPanel:YES];

[panel setCanChooseDirectories:YES];
[panel setCanChooseFiles:YES];
[panel setAllowsMultipleSelection:YES];
[panel setAllowedFileTypes:fileTypes];

[panel beginWithCompletionHandler:^(NSInteger result){
    if (result == NSFileHandlingPanelOKButton) {
        NSMutableArray * pathsArray = [[NSMutableArray alloc] init];
        NSArray * urlArray = [panel URLs];
        for (NSURL * url in urlArray) {
            //this is how I get path to image, assume I am not selecting directories
            NSString * path = [url path];
        }
    }
}];

最佳答案

无需延迟SSB的创建。此外,传输到路径然后返回 URL 可能会导致沙箱出现一些问题。尝试使用以下内容:

    // the URLs come back with access to it. they are added to the sandbox by the panel. no need to do anything like start accessing security scoped.
    NSArray * urlArray = [panel URLs];

    for (NSURL *urlToStore in urlArray) {
        // create a SSB out of the URL
        NSError *erroer = nil;
        NSData *bookmarkData = nil;
        bookmarkData = [urlToStore bookmarkDataWithOptions:(NSURLBookmarkCreationWithSecurityScope| NSURLBookmarkCreationSecurityScopeAllowOnlyReadAccess)
                            includingResourceValuesForKeys:nil
                                             relativeToURL:docUrl
                                                     error:&erroer];
        if (erroer) {
            NSLog(@"couldn't create NSURLBookmarkCreationWithSecurityScope with error: %@", [erroer description]);
        } else if ( bookmarkData ) {
            // store the bookmark data either in the document or internally for later persistency
        } else {
            NSLog(@"no error and no bookmarkData: %@", [self description]);
        }
     }

关于macos - 如何创建要保存到新文档的文档范围书签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27730384/

相关文章:

macos - Haskell 第一步编译错误

cocoa - 如何在 Cocoa AppKit 应用程序中实现缩放/缩放

swift - 在头部 View 中设置按钮

xcode - 通过脚本导入LightRoom中的图片

objective-c - 如何使用 setMessageBody 将 NSString 添加到电子邮件

objective-c - 获取 Mavericks 中 "Displays have separate spaces"选项的值

xcode - iOS 模拟器绘图太大(不是窗口比例问题)

objective-c - 可以使用CoreAudio获取音频流数据到系统输出设备吗?

macos - 沙盒应用程序中的 AppleScript `activate` 不会将窗口带到前台

objective-c - Mac 沙盒应用程序失去对其他应用程序的文件权限