1

Apple 审查要求我的应用记住用户上次选择的文件夹。但我无法在沙盒下制作它。-setDirectoryURL 似乎在沙盒下不起作用。我该怎么办?感谢您的帮助!

// read string saveFolder from NSUserDefaults
NSOpenPanel * myPanel = [NSOpenPanel openPanel];
[myPanel setTitle:@"Select Output Folder"];
[myPanel setCanChooseDirectories:YES];
[myPanel setCanCreateDirectories:YES];
[myPanel setAllowsMultipleSelection:NO];
[myPanel setCanChooseFiles:NO];

NSLog(@"before openpanel:folder=%@",saveFolder); // <== return normal
[myPanel setDirectoryURL:[NSURL URLWithString:saveFolder.stringValue]];
NSLog(@"readback:folder=%@",[[myPanel URL] path]); // <== return nil here

if ([myPanel runModal] == NSOKButton)
{
    //  
    saveFolder = [[myPanel URL] path]];
    // then save the saveFolder string to NSUserDefaults
    //
}
4

1 回答 1

4

看来我们应该添加并使用此权利:

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

保存网址:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSError *error = nil;
NSData *bookmarkData = [[myPanel URL] bookmarkDataWithOptions:NSURLBookmarkCreationWithSecurityScope includingResourceValuesForKeys:nil relativeToURL:nil error:&error];
if (!error)
{
    [defaults setObject:bookmarkData forKey:@"iData"];
    [defaults synchronize];
}

回过头再读:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
bookmarkFileURL = nil;
NSData *bookmarkData = [defaults objectForKey:@"iData"];
if (bookmarkData != nil)
{
    NSError *error=nil;
    bookmarkFileURL = [NSURL URLByResolvingBookmarkData:bookmarkData options:NSURLBookmarkResolutionWithSecurityScope relativeToURL:nil bookmarkDataIsStale:nil error:&error];
    if (error != nil) bookmarkFileURL = nil;
}
if (bookmarkFileURL)
{
    [bookmarkFileURL startAccessingSecurityScopedResource];
}
//
//
[bookmarkFileURL stopAccessingSecurityScopedResource];
于 2012-12-05T23:09:22.540 回答