第一、什么是沙盒
IOS应用程序只能在为该改程序创建的文件系统中读取文件,不可以去其它地方访问,此区域被成为沙盒
第二、保存内容
所有的非代码文件都要保存在此,例如图像,图标,声音,映像,属性列表,文本文件等
第三、作用
iOS沙盒为程序运行提供了很好的安全保障
第四、目录
1、Documents目录:这个目录用于存储用户数据或其它应该定期备份的信息,苹果建议将程序中建立的或在程序中浏览到的文件数据保存在该目录下,iTunes备份和恢复的时候会包括此目录。
2、AppName.app 目录:这是应用程序的程序包目录,包含应用程序的本身。由于应用程序必须经过签名,所以您在运行时不能对这个目录中的内容进行修改,否则可能会使应用程序无法启动。
3、Library目录:这个目录下有两个子目录:Caches 和 Preferences
Preferences 目录包含应用程序的偏好设置文件。您不应该直接创建偏好设置文件,而是应该使用NSUserDefaults类来取得和设置应用程序的偏好
Caches 目录用于存放应用程序专用的支持文件,保存应用程序再次启动过程中需要的信息。
4、tmp 目录:这个目录用于存放临时文件,保存应用程序再次启动过程中不需要的信息,重启后清空
itues和iphone同步时,备份所有的Document和library文件
第五、获取不同目录的方法
<span style="color:#333333;">#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *textLabel;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)getHome:(id)sender {
NSString *homePath = NSHomeDirectory();
_textLabel.text = homePath;
NSLog(@"</span><span style="color:#ff0000;">Home目录</span><span style="color:#333333;">:%@",homePath);
}
- (IBAction)getDocument:(id)sender {
NSArray *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [docPath objectAtIndex:0];
NSLog(@"</span><span style="color:#ff0000;">Documents目录</span><span style="color:#333333;">:%@",documentsPath);
_textLabel.text = documentsPath;
}
- (IBAction)getLibrary:(id)sender {
NSArray *libsPath = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *libPath = [libsPath objectAtIndex:0];
NSLog(@"</span><span style="color:#ff0000;">Library目录</span><span style="color:#333333;">:%@",libPath);
_textLabel.text = libPath;
}
- (IBAction)getCache:(id)sender {
NSArray *cachPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachePath = [cachPath objectAtIndex:0];
NSLog(@"</span><span style="color:#ff0000;">Cache目录</span><span style="color:#333333;">:%@",cachePath);
_textLabel.text = cachePath;
}
- (IBAction)getTmp:(id)sender {
NSString *tempPath = NSTemporaryDirectory();
NSLog(@"</span><span style="color:#ff0000;">temp目录</span><span style="color:#333333;">:%@",tempPath);
_textLabel.text = tempPath;
}
- (IBAction)writeFile:(id)sender {
NSArray *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsPath = [docPath objectAtIndex:0];
//写入文件
if (!documentsPath) {
_textLabel.text = @"目录未找到";
}else {
NSString *filePaht = [documentsPath stringByAppendingPathComponent:@"test.txt"];
NSArray *array = [NSArray arrayWithObjects:@"Title",@"Contents", nil];
[array writeToFile:filePaht atomically:YES];
_textLabel.text = @"写入成功";
}
}
- (IBAction)readFile:(id)sender {
NSArray *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsPath = [docPath objectAtIndex:0];
NSString *readPath = [documentsPath stringByAppendingPathComponent:@"test.txt"];
NSArray *fileContent = [[NSArray alloc] initWithContentsOfFile:readPath];
NSLog(@"文件内容:%@",fileContent);
_textLabel.text = @"读取成功";
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
</span>