IOS沙盒机制(SandBox)
IOS中的沙盒机制(SandBox)是一种安全体系,它规定了应用程序只能在为该应用创建的文件夹内读取文件,不可以访问其他地方的内容。所有的非代码文件都保存在这个地方,比如图片、声音、属性列表和文本文件等。
1.每个应用程序都在自己的沙盒内
2.不能随意跨越自己的沙盒去访问别的应用程序沙盒的内容
3.应用程序向外请求或接收数据都需要经过权限认证
显示Mac隐藏文件的命令:defaults write com.apple.finder AppleShowAllFiles -bool true
隐藏Mac隐藏文件的命令:defaults write com.apple.finder AppleShowAllFiles -bool false
【注】//在iOS设备下,每个APP拥有自己的沙盒目录,APP包和APP使用的所有文件,都放到这个目录下。
【注】模拟器的沙盒目录
NSString * path = NSHomeDirectory()
这个方法,在模拟器上获得模拟器中APP的目录,在真机上,获得真机的沙盒目录
二.沙盒子目录
Documents:应用程序所使用的文件都存储到这个目录下,如plist文件,数据库,大型图片,音频等。
Library:配置文件
Library/Caches:缓存文件 当应用程序更新, 不会备份该目录下缓存文件。
tmp:临时文件
//获取根目录
NSString *homePath = NSHomeDirectory();
NSLog(@"Home目录:%@",homePath);
//获取Documents文件夹目录,第一个参数是说明获取Doucments文件夹目录,第二个参数说明是在当前应用沙盒中获取,所有应用沙盒目录组成一个数组结构的数据存放
NSArray *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsPath = [docPath objectAtIndex:0];
NSLog(@"Documents目录:%@",documentsPath);
//获取Cache目录
NSArray *cacPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachePath = [cacPath objectAtIndex:0];
NSLog(@"Cache目录:%@",cachePath);
//Library目录
NSArray *libsPath = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *libPath = [libsPath objectAtIndex:0];
NSLog(@"Library目录:%@",libPath);
//temp目录
NSString *tempPath = NSTemporaryDirectory();
NSLog(@"temp目录:%@",tempPath);