沙盒是系统为了ios程序分配的可以读写数据的文件区域。
沙盒里面有三个重要的文件夹:
1documents:用于存储NSUesrDefults之外的数据。
2tmp用于存储临时数据,当数据不再需要的时候我们将删除数据。
3library:NSUserDefults的数据保存在LIbrary/Preferences下面。
获取app的SandBox路径.
- (NSString *)pathOfAppSandBox {
// 核心API: 函数 NSHomeDirectory
return NSHomeDirectory();
}
获取Documents 路径.
- (NSString *)pathOfDocuments {
// 核心API: NSSearch...
return [SandBoxhandler pathOfDocuments];
}
获取Tmp 路径.
- (NSString *)pathOfTmp {
// Core API: NSTemporaryDirectory
return NSTemporaryDirectory();
}
获取Library 路径.
- (NSString *)pathOfLibrary {
// Core API:
return [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];
}
获取Caches 路径.
- (NSString *)pathOfCaches {
// Core API:
return [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
}
获取Preferences 路径.
- (NSString *)pathOfPreFerences {
// 没有直接找到Preferences 的API 拼接字符串
return [[self pathOfLibrary] stringByAppendingString:@"/Preferences"];
}
获取 App 包路径.
- (NSString *)pathOfAppBundle {
return [NSBundle mainBundle].resourcePath;
}
接下来举几个沙盒应用的例子:
字符串使用沙盒进行存储
存入字符串
- (void)saveNSString {
NSString *str = @"Dalian SoftPark #10 Building";
// Core API: writeToFile:
// 文件路径
self.pathOfString = [SandBoxhandler saveInDocumentsWithFileName:@"location.txt"];
[str writeToFile:self.pathOfString atomically:YES encoding:NSUTF8StringEncoding error:nil];
}
读取字符串
- (void)readStringFromDisk {
NSString *str = [NSString stringWithContentsOfFile:self.pathOfString encoding:NSUTF8StringEncoding error:nil];
NSLog(@"string: %@", str);
}
存入数组
- (void)saveArray {
NSArray *arr = @[@"zhangsan", @"lisi", @"Tom"];
self.pathOfArray = [SandBoxhandler saveInDocumentsWithFileName:@"names.plist"];
[arr writeToFile:self.pathOfArray atomically:YES];
}
读取数组
- (void)readingArrayFromDisk {
// 1.找到文件路径
NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"names.plist"];
// 2. 创建Arr对象
NSArray *arr = [NSArray arrayWithContentsOfFile:path];
// 3. 输出Arr
NSLog(@"%@", arr);
}
存入字典
- (void)saveDictionary {
// 1. 要保存的路径
NSDictionary *dic = @{@"name":@"zhangsan",
@"age" :@"18",
@"sex" :@"male"};
// 2. 保存的路径
self.pathOfdic = [SandBoxhandler saveInDocumentsWithFileName:@"people.plist"];
// 3. 保存
[dic writeToFile:self.pathOfdic atomically:YES];
}
读取字典
- (void)readingDicFromDisk {
NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:self.pathOfdic];
NSLog(@"%@", dic);
}
进行字符串的拼接获取地址
+ (NSString *)saveInDocumentsWithFileName:(NSString *)name {
return [[SandBoxhandler pathOfDocuments] stringByAppendingPathComponent:name];
}
存入数据对象
- (void)saveData {
// 数据对象.
UIImage *image = [UIImage imageNamed:@"2126.jpg"];
// API: 函数, 将image对象转化为Data对象.
NSData *data = UIImageJPEGRepresentation(image, 1);
self.pathOfData = [SandBoxhandler saveInDocumentsWithFileName:@"image.jpg"];
[data writeToFile:self.pathOfData atomically:YES];
}
读取数据对象
- (void)readDataFromDisk {
NSData *data = [NSData dataWithContentsOfFile:self.pathOfData];
UIImage *image = [UIImage imageWithData:data];
NSLog(@"%@", image);
}
对象想进行归档, 必须签订NSCoding协议,并实现两个协议方法.
归档
- (void)saveModelToDisk {
ModelOfContact *con1 = [[ModelOfContact alloc] init];
con1.name = @"Tom";
con1.age = 18;
// 归档
NSMutableData *mData = [NSMutableData data];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:mData];
[archiver encodeObject:con1 forKey:@"model"];
[archiver finishEncoding];
// 完成编码
[mData writeToFile:[SandBoxhandler saveInDocumentsWithFileName:@"model.da"] atomically:YES];
}
反归档
- (void)readModelFromDisk {
NSData *data = [NSData dataWithContentsOfFile:[SandBoxhandler saveInDocumentsWithFileName:@"model.da"]];
NSKeyedUnarchiver *unArchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
// 完成解码
ModelOfContact *model = [unArchiver decodeObjectForKey:@"model"];
[unArchiver finishDecoding];
NSLog(@"%@, %ld", model.name, model.age);
}
实现NSCoding协议方法.
归档时调用此方法.
- (void)encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeObject:self.name forKey:@"name"];
[aCoder encodeInteger:self.age forKey:@"age"];
}
反归档时调用此协议方法.
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
self = [super init];
if (self) {
// 属性赋值
self.name = [aDecoder decodeObjectForKey:@"name"];
self.age = [aDecoder decodeIntegerForKey:@"age"];
}
return self;
}