Apple审核要求我的应用记住上次用户选择的文件夹。但我无法在Sandbox下制作它。 -setDirectoryURL似乎无法在Sandbox下运行。 我该怎么办?谢谢你的帮助!
// 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
//
}
答案 0 :(得分:4)
似乎我们应该添加并使用此权利:
com.apple.security.files.bookmarks.app-范围
保存网址:
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];