问题描述
好的,是的,我现在知道您不能在沙箱中使用硬编码的路径.到目前为止,我还没有使用沙箱,所以我从未遇到过.
Ok, yes I know now that you can not use hardcoded paths with sandbox. Up to this point I have not delt with sandbox, so I never encountered it.
我有一个Coredata应用程序(Mac OSx),我使用了默认的保存代码和默认的路径位置(用户/...../applicationsupport/...),这在沙盒中是不可接受的.
I have a Coredata App (Mac OSx) and I used the default save code and the default path location (user/...../applicationsupport/... This, of coarse, is not acceptable in the sandbox.
在每次启动程序时都不需要用户手动打开数据文件,是否有另一种方法可以解决此问题?
Without requiring the user to manually open the data file each time the program is launched, is there another way to deal with this?
我将不胜感激.
谢谢
推荐答案
沙箱并不意味着没有用户选择就无法访问文件和文件夹.如深度"文章中的应用程序沙箱" ,您仍然可以访问容器目录.
Sandbox doesn't mean there isn't any access to files and folders without user selection. As it said in App Sandbox in Depth article there's container directory you still having access to.
要获取Application Support
目录的路径,无论是否使用沙箱,都应使用相同的代码.
For taking a path to your Application Support
-directory you should use the same code whenever you use Sandboxing or not.
+ (NSString *)executableName
{
NSString *executableName = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleExecutable"];
if(!executableName || executableName.length==0)
return nil;
return executableName;
}
- (NSString *)findOrCreateDirectory:(NSSearchPathDirectory)searchPathDirectory
inDomain:(NSSearchPathDomainMask)domainMask
appendPathComponent:(NSString *)appendComponent
error:(NSError **)errorOut
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(searchPathDirectory,domainMask,YES);
if ([paths count]==0)
return nil;
NSString *resolvedPath = [paths objectAtIndex:0];
if (appendComponent)
resolvedPath = [resolvedPath stringByAppendingPathComponent:appendComponent];
NSError *error;
BOOL success = [self createDirectoryAtPath:resolvedPath withIntermediateDirectories:YES attributes:nil error:&error];
if (!success)
{
if (errorOut)
*errorOut = error;
return nil;
}
return resolvedPath;
}
- (NSString *)applicationSupportDirectory
{
NSError *error;
NSString *result = [self findOrCreateDirectory:NSApplicationSupportDirectory inDomain:NSUserDomainMask
appendPathComponent:[self executableName] error:&error];
if (error)
return nil;
return result;
}
这篇关于如何在沙箱中使用硬编码的文件路径名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!