iOS沙盒

来源:互联网 发布:java api 1.6 中文 编辑:程序博客网 时间:2024/05/28 15:07

• iOS应⽤用程序只能对⾃自⼰己创建的⽂文件系统读取⽂文件,这个独⽴立、封闭、安全的空间称 做沙盒。
• 一般存放着程序包⽂文件(可执⾏行⽂文件)、图⽚片、⾳音频、视频、plist⽂文件、sqlite数据库 以及其他⽂文件
iOS文件系统
iOS沙盒
iOS沙盒
Bundle容器: 只读; xxx.app(图片/可执行文件/Info.plist等)
[[NSBundle mainbundle] pathForResource:@“test”withType:@“png”];
Data容器: 读/写;
/Documents/: iTunes备份 自动上传到iCloud
/Library/Caches: iTunes不备份 下载的文件(音频、视频,) 不会copy到iCloud上
/Library/Preferences: 设置的文件
/tmp/

一.iOS沙盒机制特性
1. 应⽤用程序有独⽴立的存储空间 (沙盒)
2. 程序之间不可以互相访问 3.针对每个应⽤用,系统都会为其创建唯⼀一的⽂文件夹
二. iOS模拟器沙盒路径例⼦子:
/Users/tarena/Library/Developer/CoreSimulator/Devices/62B0D10D-D2AA-4FE5-B467-0C2A31BF21C9
/data/Containers/Data/Application/D6FFE5A8-D6B8-4396-8271-3EB9AB076F9B/
1.获取沙盒根目录
NSString *homePath = NSHomeDirectory();
2.获取沙盒数据容器的Documents目录
/*第一个参数:指定要搜索的目录
第二个参数:指定搜索域(当前登录用户下面)
第三个参数:YES(绝对路径);NO(非绝对路径)
返回:数组(iOS沙盒只有一个目录)
*/
NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSLog(@”Documents路径:%@”, documentsPath);
//3.获取沙盒数据容器的Library目录
NSString *libraryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) firstObject];
NSLog(@”Library路径:%@”, libraryPath);
//4.获取沙盒数据容器的tmp目录
NSString *tmpPath = NSTemporaryDirectory();
NSLog(@”tmp路径:%@”, tmpPath);

//5.获取bundle容器中的图片test.png(xxx.app)NSString *testPath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"png"];NSLog(@"test图片的路径:%@", testPath);//5.1方式二NSString *testPathStr = [[NSBundle mainBundle] pathForResource:@"test.png" ofType:nil];
0 0
原创粉丝点击