从沙盒应用程序以编程方式在Mavericks中进行标记

时间:2013-11-13 15:20:51

标签: cocoa osx-mavericks appstore-sandbox

我正在写入沙盒应用中的文件,然后尝试使用-[NSURL setResourceValue:theTags forKey:NSURLTagNamesKey error:&theTaggingError];为其设置标记。我没有收到任何错误(即标签成功应用一次)但最终文件被替换,标签丢失。这仅适用于沙盒应用;如果我关闭沙盒的东西工作正常。在沙盒模式下,如果我在不写入文件的情况下设置标签 - 再次可以正常工作。

本质上,我无法在写入文件后设置文件的标签。我该如何解决?任何见解?

谢谢!

示例代码:

#import <Cocoa/Cocoa.h>

@interface AppDelegate : NSObject <NSApplicationDelegate>
{
    NSWindow *_window;
    NSURL *_saveURL;
    NSSavePanel *_savePanel;
}
@property (assign) IBOutlet NSWindow *window;
@property (retain) NSURL *saveURL;
@property (retain) NSSavePanel *savePanel;
- (IBAction)writeAndTag:(id)sender;
- (IBAction)justTag:(id)sender;
@end


@implementation AppDelegate

@synthesize window = _window;
@synthesize saveURL = _saveURL;
@synthesize savePanel = _savePanel;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    self.savePanel = [NSSavePanel savePanel];
}

- (IBAction)writeAndTag:(id)sender
{
    [self.savePanel beginSheetModalForWindow:self.window completionHandler:^(NSInteger result) {
        self.saveURL = [self.savePanel URL];

        NSString *testString = @"Hello!";
        NSError *error = nil;
        [testString writeToFile:[self.saveURL path] atomically:NO encoding:NSUTF8StringEncoding error:&error];
        if(error)
        {
            NSLog(@"Err in saving: %@" ,error);
            error = nil;
        }
        // Tag is lost here
        BOOL success = [self.saveURL setResourceValue:[NSArray arrayWithObjects:@"Test", nil] forKey:NSURLTagNamesKey error:&error];
        NSLog(@"Tagging success: %@", (success)?@"YES":@"NO");
        if(error)
        {
            NSLog(@"Err in tagging: %@" ,error);
        }
    }];
}

- (IBAction)justTag:(id)sender
{
    // Works fine
    [self.saveURL setResourceValue:[NSArray arrayWithObjects:@"Test", nil] forKey:NSURLTagNamesKey error:NULL];
}

@end

0 个答案:

没有答案