查看沙盒方法:
前往 --> (按住option键) 资源库 --> Application Support --> iPhone Simulator --> ...
在沙盒目录下创建文夹,可以用
- (BOOL)createDirectoryAtPath:(NSString *)path withIntermediateDirectories:(BOOL)createIntermediates attributes:(NSDictionary *)attributes error:(NSError **)error 方法,
为了搞清楚这个withIntermediateDirectories:什么意思,专写了一Demo:
// ViewController.m
#import "ViewController.h"
@interface ViewController ()
{
NSString *_directoryPath;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *yesButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[yesButton setFrame:CGRectMake(20, 20, 280, 30)];
[yesButton setTitle:@"withIntermediateDirectories:YES" forState:UIControlStateNormal];
[yesButton addTarget:self action:@selector(yesButtonClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:yesButton];
UIButton *noButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[noButton setFrame:CGRectMake(20, 80, 280, 30)];
[noButton setTitle:@"withIntermediateDirectories:NO" forState:UIControlStateNormal];
[noButton addTarget:self action:@selector(noButtonClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:noButton];
NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
_directoryPath = [[documentPath stringByAppendingPathComponent:@"1/2"] copy];
}
- (void)yesButtonClick:(id)sender
{
if (![[NSFileManager defaultManager] fileExistsAtPath:_directoryPath]) {
[[NSFileManager defaultManager] createDirectoryAtPath:_directoryPath withIntermediateDirectories:YES attributes:nil error:nil];
}
}
- (void)noButtonClick:(id)sender
{
if (![[NSFileManager defaultManager] fileExistsAtPath:_directoryPath]) {
[[NSFileManager defaultManager] createDirectoryAtPath:_directoryPath withIntermediateDirectories:NO attributes:nil error:nil];
}
}
- (void)dealloc
{
[_directoryPath release], _directoryPath = nil;
[super dealloc];
}
@end
发现点击yesButton时会创建文件夹成功,但点击noButton发现创建文件夹不成功。