iOS中用沙盒存储数据,安全性高
沙盒路径每运行一次,文件名就会变化一次,保证在以后的使用中,不会被入侵,保证存储数据的安全性
沙盒里有三个文件夹
Documents文件夹:用来保存用户信息,比如收藏,或者自己设置的一些信息,所以我们在做收藏的功能时,一般会放在这个文件夹里
Library文件夹:一般保存一些开发者使用的东西,里面包含两个文件夹,一个是Catches,一个是Preferences
catches:保存的是工程的缓存文件,通过sd加载图片一般都保存在这个文件夹里
Preference:保存的像NSUserDefaults这样的数据
tmp文件夹:保存的是临时文件,一般在做图片视频选取的时候,会把文件临时保存在这个文件下
文件的对象分为两种
1.简单对象:字符串,数组,字典等
步骤:(1)获取沙盒路径 参数一:指定前往哪个文件夹,NSDocumentDirectory前往的是沙盒的Document文件夹,NSCatchesDirectory前往catches文件夹 参数二:指定文件类型,就是用户类型 参数三:设置绝对路径还是相对路径
// NSArray *sandBoxPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
YES对应的绝对路径,根据这个路径可以直接找到路径所对应的文件,这个是系统用的,NO对应的是相对路径,是给我们自己看的,通过相对路径可以看见目标文件是哪个
(2)创建一个存储数据的文件,文件需要先拼接路径
(3)写入:字符串写入的参数一:路径 参数二:是否保护数据 参数三:编码格式 参数四:错误信息
(4)根据路径把本地的数据再读取出来
// 根据路径把本地的数据再读取出来
// NSString *string = [NSString stringWithContentsOfFile:docPath encoding:NSUTF8StringEncoding error:nil];
// NSLog(@"%@", string);
// bundle是获取当前工程的所有文件的路径,这个路径就是根据当前程序的状态来找的,不受影响
// 用户信息如果写在bundle里,会被泄露
// NSString *path = [[NSBundle mainBundle] pathForResource:@"area" ofType:@"txt"];
// NSLog(@"%@", path);
// 图片的路径
// NSString *picPath = [[NSBundle mainBundle] pathForResource:@"3" ofType:@"jpg"];
// NSLog(@"%@", picPath);
// NSData *data = [NSData dataWithContentsOfFile:picPath];
// [UIImage imageWithData:data];
// 沙盒
// NSLog(@"%@", NSHomeDirectory());
// 沙盒路径每运行一次,文件名就会变化一次,保证在以后的使用中,不会被入侵,保证存储数据的安全性
// 沙盒里有三个文件夹
//Documents文件夹:用来保存用户信息,比如收藏,或者自己设置的一些信息,所以我们在做收藏的功能时,一般放在这个问价夹里
// Library文件夹:一般是保存一些开发者使用的东西,里面包含两个文件夹,一个是Catches,一个是Preferences
// catches:保存的是工程的缓存文件,通过sd加载图片一般都保存在这个文件夹里
// Preferences保存的像NSUserDefaults这样的数据
// tmp:保存的是临时文件,一般在做图片视频选取的时候,会把文件临时保存在这个文件下
// 文件的对象分为两种
// 1.简单对象:字符串,数组,字典等
// NSString *str = @"郡主是我们的精神支柱!!郡主,郡主";
// // (1)获取沙盒路径
// // 参数一:指定前往哪个文件夹,NSDocumentDirectory前往的是沙盒的Documents文件夹, NSCachesDirectory只是前往caches文件夹
// // 参数二:指定文件类型,就是用户类型
// // 参数三:设置绝对路径还是相对路径
// NSArray *sandBoxPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
// // 如果改成NO,打印结果是~/Documents
// // YES对应的是绝对路径,根据这个路径可以直接找到路径所对应的文件,这个是系统用的,NO对应的是相对路径,是给我们自己看的,通过相对路径可以看见目标文件是哪个
// NSString *sandBox = sandBoxPath[0];
// NSLog(@"%@", sandBox);
//
// // (2)创建一个存储数据的文件,文件需要先拼接路径
// NSString *docPath = [sandBox stringByAppendingString:@"/郡主.html"];
// NSLog(@"%@", docPath);
//
// // (3)写入
// // 参数一:路径
// // 参数二:是否保护数据
// // 参数三:编码格式
// // 参数四:错误信息
// [str writeToFile:docPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
//
// // 根据路径把本地的数据再读取出来
// NSString *string = [NSString stringWithContentsOfFile:docPath encoding:NSUTF8StringEncoding error:nil];
// NSLog(@"%@", string);
把数组作为对象写入本地 过程都相似,在读取的时候用的方法不一样
// // 把数组对象写入本地
// NSArray *arr = @[@"1", @"2", @"3", @"4", @"5"];
// // 1.找到沙盒的路径(数组中只有一个元素)
// NSArray *sandBox = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
// NSLog(@"%@", sandBox);
// // 将路径放入沙盒中
// NSString *sandBoxPath = sandBox[0];
// // 2.拼接文件的路径
// // 拼接时不需要加/
// NSString *docPath = [sandBoxPath stringByAppendingPathComponent:@"arr.plist"];
// NSLog(@"%@", docPath);
// // 3.把数组写入本地
// [arr writeToFile:docPath atomically:YES];
//
// // 把数组从本地读出来
// NSArray *newArr = [NSArray arrayWithContentsOfFile:docPath];
// NSLog(@"%@", newArr);
字典作为对象写入
// 字典
// NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"1", @"2", @"3", @"4", nil];
// // 沙盒路径
// NSArray *sandBox = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
// NSString *sandBoxPath = sandBox[0];
// // 拼接文件路径
// NSString *docPath = [sandBoxPath stringByAppendingPathComponent:@"dic.txt"];
// NSLog(@"%@", docPath);
// // 写入
// [dic writeToFile:docPath atomically:YES];
//
// // 读取
// NSDictionary *newDic = [NSDictionary dictionaryWithContentsOfFile:docPath];
// NSLog(@"%@", newDic);
将自己写的类作为对象时,需要归档和反归档,需要签协议<NSCoding>,必须实现的两个协议方法,第一个方法是存入本地的时候用,第二个从本地读取的时候用(归档也叫序列化)
- (void)encodeWithCoder:(NSCoder *)aCoder{
// 先对这些内容进行编码,然后存储
[aCoder encodeObject:self.name forKey:@"name"];
[aCoder encodeObject:self.sex forKey:@"sex"];
[aCoder encodeObject:self.hobby forKey:@"hobby"];
[aCoder encodeInteger:self.age forKey:@"age"];
}
// 从本地读取的时候用
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder{
self = [super init];
if (self) {
self.name = [aDecoder decodeObjectForKey:@"name"];
self.sex = [aDecoder decodeObjectForKey:@"sex"];
self.hobby = [aDecoder decodeObjectForKey:@"hobby"];
self.age = [aDecoder decodeIntegerForKey:@"age"];
}
return self;
}
创建路径的过程都很相似,在归档和反归档的时候,方法有些变化
// Student *stu = [[Student alloc] init];
// stu.name = @"apple";
// stu.sex = @"女";
// stu.age = 20;
// stu.hobby = @"睡觉";
// // 1.找到沙盒路径
// NSArray *sandBox = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
// NSString *sandBoxPath = sandBox[0];
// // 2.拼接路径
// NSString *docPath = [sandBoxPath stringByAppendingPathComponent:@"尹德建.av"];
// NSLog(@"%@", docPath);
// // 3.归档
// [NSKeyedArchiver archiveRootObject:stu toFile:docPath];
//
// // 反归档
// Student *newStu = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];
// NSLog(@"%@", newStu);
NSArray已经实现了NSCoding协议,所以可以直接用归档和反归档进行操作,如果数组里是字符串这种简单对象,用writeToFile和归档都可以,但是如果数组里放的是Student这种自定义的对象,只能用归档
NSUsrDefaults的用法和字典类似,用于存储一些简单字符串等
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:@"1" forKey:@"2"];
NSLog(@"%@", docPath);
NSLog(@"%@", [defaults objectForKey:@"2"]);
文件管理(需要四步)
1.获取沙盒路径
2.拼接文件夹路径(拼接的时候,没有后缀)
3.写入的时候需要一个管理者
4.管理者创建文件夹
// 1.获取沙盒路径
NSArray *sandBox = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 1, YES);
NSString *sandBoxPath = sandBox[0];
// 2.拼接文件夹路径
NSString *docPatch = [sandBoxPath stringByAppendingPathComponent:@"yindejian"];
//
// 写入的时候需要一个管理者
NSFileManager *manager = [NSFileManager defaultManager];
// 创建文件夹
[manager createDirectoryAtPath:docPatch withIntermediateDirectories:YES attributes:nil error:nil];
NSLog(@"%@", docPatch);
判断文件夹是否已经创建过[manager fileExistAtPath:]
// // 判断文件夹是否已经创建过
// BOOL result = [manager fileExistsAtPath:docPatch];
// NSLog(@"%d", result);
删除
BOOL result = [manager removeItemAtPath:docPatch error:nil];