IOS 开发 之 沙盒机制
来源:互联网 发布:东方购物软件 编辑:程序博客网 时间:2024/05/04 07:54
如果此文帮助了您,请点击喜欢或评论,如果您喜欢我的文章请关注我,您的支持永远都是我前行的动力.
iOS应用程序只能在为该改程序创建的文件中读取文件,不可以去其它地方访问,此区域被成为沙盒,所以所有的非代码文件都要保存在此,例如图像,图标,声音,映像,属性列表,文本文件等。
1、每个应用程序都有自己的存储空间
2、应用程序不能翻过自己的围墙去访问别的存储空间的内容
3、应用程序请求的数据都要通过权限检测,假如不符合条件的话,不会被放行
沙盒主路径
NSString *homePath = NSHomeDirectory();
程序运行期间,系统会生成一个专属的沙盒路径,应用程序在使用期间的非代码文件都存储在当前的沙盒路径里面
documents 文件:用来存储永久性的数据的文件,程序运行时必要的文件都存储在这里(数据库) ,itunes会自动备份这里面的文件.
NSArray *documentPathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, NO);
第一个参数是:你要查询的文件的路径.第二个参数是要查询路径所属的用户(iOS为单用户).第三个参数YES是绝对路径,NO是相对路径.
document是数组是为了区别于os X系统,iOS文件夹中通常只有一个文件路径,由于OC同时支持苹果系列产品的开发,在mac OS里面,会同时存在很多软件,生成的路径放在一个数组里面
iOS端一次只有一个应用,所以取数组的唯一一个元素即可.
NSString *documentPath = [documentPathArray firstObject];
library 文件:用于保存程序运行期间生成的文件
NSString *libraryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES)firstObject];
NSLog(@"libraryPath = %@",libraryPath);
caches文件:用于保存程序运行期间产生的缓存文件
NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)firstObject];
NSLog(@"caches = %@",cachesPath);
preference 文件
通过上面的方法打印的路径多了path 没法访问,所以我们需要使用拼接字符串的方式,找到Preferences文件路径
NSString *preferencesPath = [NSSearchPathForDirectoriesInDomains(NSPreferencePanesDirectory, NSUserDomainMask, YES)lastObject];
NSLog(@"preferences = %@",preferencesPath);
单例 NSUserDefaults,通常情况下并不直接打开,而是通过NSUserDefaults进行偏好设置的存取.
NSString *preferencesPath = [libraryPath stringByAppendingString:@"/Preferences"];
NSLog(@"%@",preferencesPath);
tmp临时文件夹 程序运行期间产生的临时碎片会保存到这个文件夹里面,通常文件下载完之后或者程序推出会自动清空此文件夹,itus不会备份这里的数据.
tips:由于系统会清空此文件夹,所以下载或者其他临时文件若需要请及时移走. NSString *tmpPath = NSTemporaryDirectory();
NSLog(@"%@",tmpPath);
.app路径(bundle)
iOS8之前,bundle和tmp等文件统一放在主路径下(home)
iOS8之后,bundle放在了container文件夹下面
NSString *bundlePath = [[NSBundle mainBundle] resourcePath];
NSLog(@"%@",bundlePath);
NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"222" ofType:@"jpg"];
NSLog(@"%@",imagePath);
简单对象写入文件
NSString 写入文件
1.第一步 准备字符串 NSString *string = @"I love my iOS teacher";
NSString *path = NSHomeDirectory();
2.第二步 准备路径 path = [path stringByAppendingPathComponent:@"/咒语.txt"];
3,第三步 写入文件
第一个参数:路径
第二个参数:是否进行线性操作(NO如果关机停电下次重新下载,YES是保证发生意外时有中转文件来保存信息,直至写入完成,但是损耗大,NO时写入速度快,但是没有安全保障)
第三个参数:编码方式
第四个参数:错误对象 [string writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];
NSString 读取文件
第一个参数:路径
第二个参数:编码方式(切记要和写入时用相同的编码方式)
第三个参数:错误信息
NSString *contentString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
NSLog(@"%@",contentString);
NSArray 写入文件
NSArray *array = [NSArray arrayWithObjects:@"Lily",@"Yucui",@"Star",@"Ling",@"WenQi",@"Yangyang", nil];
NSString *docuPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)firstObject];
docuPath = [docuPath stringByAppendingString:@"/Lady.txt"];
[array writeToFile:docuPath atomically:YES];写入文件
NSLog(@"%@",docuPath);
NSArray 读取文件
NSArray *array1 = [NSArray arrayWithContentsOfFile:docuPath];
NSLog(@"%@",array1);
NSDictionary 写入文件
NSString *nn = @"ddd" ;
NSDictionary *dic = [NSDictionary dictionaryWithObject:array forKey:nn];
NSString *douPath2 =[libraryPath stringByAppendingPathComponent:@"/Preferences/add"];
[dic writeToFile:douPath2 atomically:YES];
NSLog(@"%@",douPath2);
NSDictionary 读取文件
NSDictionary *dic2 = [NSDictionary dictionaryWithContentsOfFile:douPath2];
NSLog(@"%@",dic2);
NSData 写入文件
1.获取图片对象 UIImage *image = [UIImage imageNamed:@"222.jpg"];
2.准备路径 stringByAppendingPathComponent 专业拼接 不用加 “/”;
NSString *homePath1 = NSHomeDirectory();
homePath = [homePath1 stringByAppendingPathComponent:@"love.jpg"];
将图片对象转化为data NSData *data = UIImageJPEGRepresentation(image, 1);//后面1 为压缩比.
[data writeToFile:homePath atomically:YES];
NSLog(@"%@",homePath1);
UIImageView *imageView = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
imageView.image = [UIImage imageWithContentsOfFile:homePath];
[self.view addSubview:imageView];
复杂对象写入文件
简单对象可以通过writeToFile的方式写入,但是复杂对象没有writeToFile的方式写入文档,所以我们要借助归档和反归档的方法,将复杂对象转换成简单对象写入文档.
归档和反归档不是数据持久化的方式.
SingleVip *vip = [SingleVip new];
vip.name = @"小明";
vip.assets = @"若干";
vip.car = @"兰博基尼";
vip.age = 18;
准备路径 NSString *homepath = NSHomeDirectory();
homePath = [homePath stringByAppendingPathComponent:@"钻石王老五.txt"];
创建数据对象,用来存放Vip对象 NSMutableData *data = [NSMutableData data];
创建归档对象 NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
开始归档 [archiver encodeObject:vip forKey:@"vip"];
完成归档 [archiver finishEncoding];
写入文件
[data writeToFile:homepath atomically:YES];
NSLog(@"%@",homePath);
反归档
将文件里面的data对象读取出来 NSData *_data = [NSData dataWithContentsOfFile:homePath];
创建反归档对象
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:_data];
SingleVip *_vip = [unarchiver decodeObjectForKey:@"vip"];
完成反归档 [unarchiver finishDecoding];
NSLog(@"%@",_vip.name);
归档并不是数据持久化,而是辅助复杂对象转化成简单对象的一种方式,真正实现数据持久化的仍然是writeToFile写入文件
数据持久化的方式 1.plist 属性列表 2.NSUserDefaults 偏好设置(单例) 3.WriteToFile 写入文件 4.SQLite 数据库5.CordData
downLoadTask
NSURLSessionDownloadTask *task = [[NSURLSession sharedSession]downloadTaskWithURL: [NSURL URLWithString: IMG_URL] completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSData *data = [NSData dataWithContentsOfURL:location];//1.先获取data对象,因为data可以直接写入文件
NSString *homePath = NSHomeDirectory();
homePath = [homePath stringByAppendingPathComponent:@"我朋友"]; //2.创建一个文件夹
NSFileManager *fileManager = [NSFileManager defaultManager]; //3.创建fileManager
[fileManager createDirectoryAtPath:homePath withIntermediateDirectories:YES attributes:nil error:nil];
homePath = [homePath stringByAppendingString:@"/god"]; //创建文件
[fileManager createFileAtPath:homePath contents:data attributes:nil];
dispatch_async(dispatch_get_main_queue(), ^{
_table.image = [UIImage imageWithData:data];
});
}];
[task resume];
}
编码
- (void)encodeWithCoder:(NSCoder *)aCoder{
[aCoder encodeObject:_name forKey:kName];
[aCoder encodeObject:_assets forKey:kAssets];
[aCoder encodeObject:_car forKey:kCar];
[aCoder encodeInt:_age forKey:kAge];
}
反编码
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder{
if (self = [super init]) {
_name = [aDecoder decodeObjectForKey:kName];
_assets = [aDecoder decodeObjectForKey:kAssets];
_car = [aDecoder decodeObjectForKey:kCar];
_age = [aDecoder decodeIntForKey:kAge];
}
return self;
}
转载请注明出处.
- iOS开发之沙盒机制(SandBox)
- iOS开发之沙盒机制(SandBox)
- iOS开发之沙盒机制(SandBox)
- iOS开发之沙盒机制(SandBox)
- iOS开发之沙盒机制
- IOS 开发 之 沙盒机制
- iOS开发之沙盒机制
- iOS开发之沙盒机制
- iOS开发之沙盒(sandbox)机制
- iOS开发之沙盒机制
- iOS 开发之沙盒机制
- iOS 开发之沙盒机制 & 文件操作 (NSFielManager)
- iOS开发之通信机制
- iOS开发之通知机制
- iOS开发之KVO机制
- iOS开发之APNS机制
- IOS开发中的沙盒机制
- iOS开发沙盒机制文件操作
- 数据结构与算法系列----二叉排序树
- 【Memcached】集群搭建——memcached内存管理机制
- 结合遗传和蚁群算法的图像边缘特征提取
- 进程等待
- 工具(1):使用lua生成vs2015的工程文件/将已有的C++文件生成vs2015工程文件
- IOS 开发 之 沙盒机制
- JAVA环境变量设置
- Codeforces 527C 线段树 /set
- PAT (Advanced Level) Practise 1020 Tree Traversals (25)
- IOS 开发 之 NSFileManager
- 函数对象
- 占座
- JavaScript:this 的工作原理
- 编写一个函数,返回一个int数组中存储的最大数值,并在一个简单的程序中测试这个函数。(有缺陷)