2 mapmapm MapMapM 于 2013.05.08 16:31 提问

程序创建目录,没有禁止用户查看

在applicationSupportDirectory中创建directory 。我认为applicationSupportDirectory 不允许用户看内部数据。因为这样我才选它,但是好像不是这样。

请高手告诉我怎么回事?谢谢

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory,   NSUserDomainMask, YES);
NSString *applicationDirectory = [paths objectAtIndex:0]; // Get directory
NSString *dataPath = [applicationDirectory stringByAppendingPathComponent:@"drawings"];

if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath]){

NSError* error;
if([[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error])
  ;// success
else
{
  NSLog(@"Failed");
}
}
}

3个回答

KindAzrael
KindAzrael   2013.05.08 22:08
已采纳

打印你得dataPath 你会发现 .../Applications/2D....566A/Library/Application Support/drawings中
Application Support目录是不存在的,
所以当你createDirectoryAtPath:filePathAndDirectory withIntermediateDirectories:NO 会返回NO
因为withIntermediateDirectories: 意思是是否创建最终目录的不存在的父目录,
所以改成createDirectoryAtPath:filePathAndDirectory withIntermediateDirectories:YES就可以创建成功了
官方解释:
createIntermediates
If YES, this method creates any non-existent parent directories as part of creating the directory in url. If NO, this method fails if any of the intermediate parent directories does not exist. This method also fails if any of the intermediate path elements corresponds to a file and not a directory.

u010620260
u010620260   2013.05.08 18:06
NSString *directoryName = @"drawing";

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory,   NSUserDomainMask, YES);

NSString *applicationDirectory = [paths objectAtIndex:0];
NSString *filePathAndDirectory = [applicationDirectory stringByAppendingPathComponent:directoryName];
NSError *error;

if (![[NSFileManager defaultManager] createDirectoryAtPath:filePathAndDirectory
                               withIntermediateDirectories:YES
                                                attributes:nil
                                                     error:&error])
{
    NSLog(@"Create directory error: %@", error);

}

suannai0314
suannai0314 帮你编辑了下代码的格式,更多markdown的使用方式见:http://ask.csdn.net/pages/markdown
4 年多之前 回复
love_loveec
love_loveec   2013.05.08 17:04
NSString *directoryName = @"drawing";

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory,   NSUserDomainMask, YES);

NSString *applicationDirectory = [paths objectAtIndex:0];
NSString *filePathAndDirectory = [applicationDirectory stringByAppendingPathComponent:directoryName];
NSError *error;

if (![[NSFileManager defaultManager] createDirectoryAtPath:filePathAndDirectory
                               withIntermediateDirectories:YES
                                                attributes:nil
                                                     error:&error])
{
    NSLog(@"Create directory error: %@", error);
}
Csdn user default icon
上传中...
上传图片
插入图片