我正在开发一个沙盒化的 Mac App Store 应用程序,它会询问用户从其他地方下载的文件保存在哪里。我有这段代码可以从用户那里获取文件夹(去掉一些错误检查):
NSOpenPanel* openPanel = [NSOpenPanel openPanel];
[openPanel setAllowsMultipleSelection:NO];
[openPanel setCanChooseDirectories:YES];
[openPanel setResolvesAliases:YES];
NSInteger result = [openPanel runModal];
NSArray* urls = [openPanel URLs];
NSURL* folderURL = [urls objectAtIndex:0];
NSError* error;
NSData* bookmakeData = [folderURL bookmarkDataWithOptions:NSURLBookmarkCreationWithSecurityScope
includingResourceValuesForKeys:nil
relativeToURL:nil
error:&error];
_saveFolderBookmark = bookmakeData;
当需要将文件移动到此文件夹时,我有以下代码:
BOOL isStale;
NSError* error;
NSURL* saveFolder = [NSURL URLByResolvingBookmarkData:_saveFolderBookmark
options:NSURLBookmarkCreationWithSecurityScope
relativeToURL:nil
bookmarkDataIsStale:&isStale
error:&error];
BOOL success = [saveFolder startAccessingSecurityScopedResource];
// Move the file somewhere else
NSWorkspace* workspace = [NSWorkspace sharedWorkspace];
NSInteger operationTag;
BOOL copied = [workspace performFileOperation:NSWorkspaceMoveOperation
source:[[self getDocumentsFolder] path]
destination:[saveFolder path]
files:[NSArray arrayWithObject:filename]
tag:&operationTag];
[saveFolder stopAccessingSecurityScopedResource];
有很多代码要列出来说明 startAccessingSecurityScopedResource 永远不会为我返回成功,无论是在从 NSOpenPanel 获取 ULR 后立即还是在以后运行时,书签数据都保存在 NSUserDefaults 中。
在权利文件中,除其他项目外,我有:
<key>com.apple.security.files.bookmarks.app-scope</key>
<true/>
这段代码有什么问题吗?