SandBox+NSBundle
1、iOS的文件系统
1.1、SandBox(沙箱、沙盒)
在iOS中,任何一个App都存放在自己的一个称之为SandBox中
1)SandBox的组成
a、Bundle Container:放应用程序的Bundle(包)
b、Data Container:
Documents:用户的数据基本都会放在这里,其中数据会备份到iCloud或iTunes
library:缓存数据、下载数据…,也会备份
library/Caches:缓存数据,不会备份
tmp:
ofType:(NSString *)
extension
NSFileHandle
//获取App的Sandbox路径
NSString *sandboxPath = NSHomeDirectory();
NSLog(@"%@", sandboxPath);
//获取Documents路径
NSString *doc = [sandboxPath stringByAppendingPathComponent:@"Documents"];
NSLog(@"Doc:%@", doc);
//另一种获取Documents路径
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *sandboxPath = NSHomeDirectory();
NSLog(@"%@", sandboxPath);
//获取Documents路径
NSString *doc = [sandboxPath stringByAppendingPathComponent:@"Documents"];
NSLog(@"Doc:%@", doc);
//另一种获取Documents路径
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
doc = [paths firstObject];
简:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]
NSLog(@"Doc:%@", doc);
//获取Library路径
NSString *lib = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) firstObject];
NSLog(@"%@", lib);
//获取Libary下的Caches目录
NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
NSLog(@"caches:%@", caches);
//获取临时目录tmp
NSString *tmp = NSTemporaryDirectory();
NSLog(@"tmp:%@", tmp);
//获取Library路径
NSString *lib = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) firstObject];
NSLog(@"%@", lib);
//获取Libary下的Caches目录
NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
NSLog(@"caches:%@", caches);
//获取临时目录tmp
NSString *tmp = NSTemporaryDirectory();
NSLog(@"tmp:%@", tmp);
获取应用程序本身的路径:NSBundle类
此类的对象代表一个资源包:
应用程序包。main bundle
应用程序包。main bundle
其他资源包, 图片资源...
//如何获取应用程序本身的路径
NSBundle *mainBundle =[NSBundle mainBundle];//mainBundle对象代表应用程序本身的内容
NSString *path = [mainBundle pathForResource:@"a" ofType:@"png"];
NSLog(@"path:%@", path);
//获取URL
NSURL *url = [mainBundle URLForResource:@"a" withExtension:@"png"];
NSString *path = [mainBundle pathForResource:@"a" ofType:@"png"];
NSLog(@"path:%@", path);
//获取URL
NSURL *url = [mainBundle URLForResource:@"a" withExtension:@"png"];
NSLog(@"url:%@", url);