1.沙盒目录
1.1 Documents: 默认备份的 itunes iCloud 注意:存储大文件(视频),可能会被审核拒绝
1.2 Libuary: Caches(系统缓存的:网络数据)、Preferences(系统是默认备份的 NSUserDefaults)
1.3 tmp:临时文件夹
1.4 SystemData:暂时没发现啥用处
对于默认备份,可以设置为非备份,代码如下:
NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSLog(@"%@", filePath);
[self addSkipBackupAttributeToItemAtURL:[NSURL fileURLWithPath:filePath]];
// 设置非备份
- (BOOL)addSkipBackupAttributeToItemAtURL:(NSURL*)URL
{
const char* filePath = [[URL path] fileSystemRepresentation];
const char* attrName = "com.apple.MobileBackup";
u_int8_t attrValue = 1;
int result = setxattr(filePath, attrName, &attrValue, sizeof(attrValue), 0, 0);
return result == 0;
}
对于通过NSUserDefaults数据修改,它会有一段时间去合并之前与现在的更改,所有会导致有时候读取数据出错。需要进行同步操作[[NSUserDefaults standardUserDefaults] synchronize];
- (void)saveUserDefaultOne {
[[NSUserDefaults standardUserDefaults] setObject:@"QQQQ" forKey:@"TG"];
}
- (void)saveUserDefaultTwo {
[[NSUserDefaults standardUserDefaults] setObject:@"TTTT" forKey:@"TGG"];
[[NSUserDefaults standardUserDefaults] setObject:@"AAAAA" forKey:@"TG"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
对于网络请求,系统会把请求的信息数据保存在Libuary: Caches文件夹下面
查看方法,1. cd Libuary/Caches/assdada 2. sqlite3 Cache.db 3. .table 4. select * from cfurl_cache_blob_data 5 select * from cfurl_cache_receiver_data
2. Bundle:资源文件,不参与编译;
黄色/蓝色文件夹 蓝色文件夹不参与编译
加载bundle里面的资源文件
NSString *boudlePath = [[NSBundle mainBundle] pathForResource:@"TGBundle" ofType:@"bundle"];
NSBundle *tgBoundle = [NSBundle bundleWithPath:boudlePath];
NSString *imagePath = [tgBoundle pathForResource:@"11" ofType:@"png"];
//_imageView.image = [UIImage imageWithContentsOfFile:imagePath];
// _imageView.image = [UIImage imageNamed:@"TGBundle.bundle/11.png"];
_imageView.image = [UIImage imageNamed:@"11.png"]; // 加载不到图片文件
3. 归档解档
遵循代理<NSCoding>
实现方法 :
归档: - (void)encodeWithCoder:(NSCoder *)aCoder
解档:- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder
.h
@interface TGObject : NSObject<NSCoding>
@property (nonatomic, strong)NSString *name;
@property (nonatomic, strong)NSString *age;
@end
.m
#import "TGObject.h"
@implementation TGObject
- (void)encodeWithCoder:(NSCoder *)aCoder{
[aCoder encodeObject:self.name forKey:@"name"];
[aCoder encodeObject:self.age forKey:@"age"];
}
// 解当
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder{
TGObject *object = [TGObject new];
object.name = [aDecoder decodeObjectForKey:@"name"];
object.age = [aDecoder decodeObjectForKey:@"age"];
return object;
}
@end
4. 钥匙串的存储
4.1 不会因为应用的删除而导致数据丢失,底层用的sql,操作数据库 ,加密的存储,存密码之类,实现增删改查
4.2 跨应用间的数据访问