- 沙盒机制
每一个iOS应用程序都会为自己创建一个文件系统目录(文件夹),这个独立、封闭、安全的空间叫做沙盒。沙盒就是一种安全体系,它规定了应用程序只能在为该应用程序创建的文件夹(沙盒)内访问文件,不可以访问其他沙盒内的内容(iOS8已经部分开放访问)。所有的非代码文件都保存在这个地方,比如图片、声音、属性列表(plist)、sqlite数据库和文本文件等。
总结iOS的沙盒机制的特点:
1、每个应用程序的活动范围都限定在自己的沙盒里
2、不能随意跨越自己的沙盒去访问别的应用程序沙盒中的内容(iOS8已经部分开放访问)
3、应用程序向外请求或接收数据都需要经过权限认证
注:1、每一个应用程序都会拥有一个应用程序沙盒。
沙盒根目录结构:Documents、Library、temp。
Documents:保存应用运行时生成的需要持久化的数据,iTunes备份和恢复的时候会包括此目录,所以苹果建议将程序中建立的或在程序中浏览到的文件数据保存在该目录下。
Library:存储程序的默认设置和其他状态信息,iTunes会自动备份该目录。包含两个子目录:Caches 和 Preferences。
Caches:存放缓存文件,iTunes不会备份此目录,此目录下文件不会在应用退出后删除 。一般存放体积比较大,不是特别重要的资源。
Preferences:保存APP的所有偏好设置,iOS的Settings(设置)应用会在该目录中查找应用的设置信息,iTunes会自动备份该目录。注意:通过NSUserDefaults类来读取和设置。
tmp: 保存应用运行时所需的临时数据,这个可以放一些当APP退出后不再需要的文件。应用没有运行时,系统也有可能会清除该目录下的文件,iTunes不会同步该目录。iPhone重启时,该目录下的文件会被删除。
- 打开沙盒方法
方法一:前往文件夹打开/Users/xuxiaoliu/Library/Application Support/iPhone Simulator/
方法二:通过Finder一步步查找
模拟器上的APP的沙盒是在用户目录下的资源库里面,但是资源库是隐藏文件夹。所以查看沙盒之前,现将隐藏文件夹显示出来。
显示隐藏文件的命令:defaults write com.apple.finder AppleShowAllFiles -bool true
隐藏隐藏文件的命令:defaults write com.apple.finder AppleShowAllFiles -bool false
另:真机上的APP沙盒路径是 /var/mobile/Containers/Data/Application/C778116A-B066-4C5C-8BBC-28E91405DA44/Documents
// 获取Document目录 NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; // 获取Library目录 NSString *LibraryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject]; // 获取Caches目录 NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]; // 获取Preferences目录 通常情况下,Preferences有系统维护,所以我们很少去操作它。 NSString *preferPath = [LibraryPath stringByAppendingPathComponent:@"Preferences"]; // 获取tmp目录 NSString *tmpPath = NSTemporaryDirectory();
一:简单对象的读写(I/O)操作
简单对象:iOS中提供四种类型可以直接进行文件存取:NSString(字符串)、NSArray(数组)、NSDictionary(字典)、NSData(数据)(以上类型包括子类)
注意:数组(可变与不可变)和字典(可变与不可变)中元素对象的类型,也必须是上述四种,否则不能直接写入文件
字符串对象写入文件
//构造字符串文件的存储路径
NSString *strPath = [documentPath stringByAppendingPathComponent:@"text.txt"];
//构造字符串对象
NSString *foo_str = @"this is a test";
//通过将writeToFile:atomically:encoding:error:方法发送给字符串对象完成字符串存储到文件内的功能
[foo_str writeToFile:strPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
从文件中读取字符串对象
NSString *str = [NSString stringWithContentsOfFile:strPath encoding:NSUTF8StringEncoding error:nil];
Plist文件
全名是:Property List,属性列表文件,它是一种用来存储串行化后的对象的文件。属性列表文件的扩展名为.plist,因此通常被成为plist文件。文件是xml格式的
Plist文件通常用于存储用户设置,也可以用于存储捆绑的信息。
NSData写入/读取
NSData对象写入文件
UIImage *image = [UIImage imageNamed:@"图片名"];
NSData *foo_data = UIImagePNGRepresentation(image);
//构造二进制文件的存储路径
NSString *dataPath = [documentPath stringByAppendingPathComponent@"data"];
//通过将writeToFile:atomically:方法发送给二进制对象完成二进制存储到文件内的功能
[foo_data writeToFile:dataPath atomically:YES];
注:二进制对象可能存储的是图像、字符串等等。
从文件中读取NSData数据
NSData *data = [NSData dataWithContentsOfFile:dataPath];
UIImage *image = [UIImage imageWithData:data];
二、文件管理器与文件对接器
文件管理器(NSFileManager):此类主要是对文件进行的操作(创建/删除/改名等)以及文件信息的获取。
文件连接器(NSFileHandle):此类主要是对文件内容进行读取和写入操作。
// 获取Documents路径 - (NSString *)getDocumentsPath { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *path = [paths objectAtIndex:0]; NSLog(@"path:%@", path); return path; } // 创建文件夹 - void)createDirectory { NSString *documentsPath =[self getDocumentsPath]; NSFileManager *fileManager = [NSFileManager defaultManager]; NSString *iOSDirectory = [documentsPath stringByAppendingPathComponent:@"iOS"]; BOOL isSuccess = [fileManager createDirectoryAtPath:iOSDirectory withIntermediateDirectories:YES attributes:nil error:nil]; if (isSuccess) { NSLog(@"success"); } else { NSLog(@"fail"); } } // 创建文件 - (void)createFile { NSString *documentsPath =[self getDocumentsPath]; NSFileManager *fileManager = [NSFileManager defaultManager]; NSString *iOSPath = [documentsPath stringByAppendingPathComponent:@"iOS.txt"]; BOOL isSuccess = [fileManager createFileAtPath:iOSPath contents:nil attributes:nil]; if (isSuccess) { NSLog(@"success"); } else { NSLog(@"fail"); } } // 写入文件 - (void)writeFile { NSString *documentsPath =[self getDocumentsPath]; NSString *iOSPath = [documentsPath stringByAppendingPathComponent:@"iOS.txt"]; NSString *content = @"我要写数据啦"; BOOL isSuccess = [content writeToFile:iOSPath atomically:YES encoding:NSUTF8StringEncoding error:nil]; if (isSuccess) { NSLog(@"write success"); } else { NSLog(@"write fail"); } } // 读取文件 - (void)readFileContent { NSString *documentsPath =[self getDocumentsPath]; NSString *iOSPath = [documentsPath stringByAppendingPathComponent:@"iOS.txt"]; NSString *content = [NSString stringWithContentsOfFile:iOSPath encoding:NSUTF8StringEncoding error:nil]; NSLog(@"read success: %@",content); } // 判断文件是否存在 - (BOOL)isSxistAtPath:(NSString *)filePath { NSFileManager *fileManager = [NSFileManager defaultManager]; BOOL isExist = [fileManager fileExistsAtPath:filePath]; return isExist; } // 计算文件大小 - (unsigned long long)fileSizeAtPath:(NSString *)filePath { NSFileManager *fileManager = [NSFileManager defaultManager]; BOOL isExist = [fileManager fileExistsAtPath:filePath]; if (isExist) { unsigned long long fileSize = [[fileManager attributesOfItemAtPath:filePath error:nil] fileSize]; return fileSize; } else { NSLog(@"file is not exist"); return 0; } } // 计算整个文件夹中所有文件大小 - (unsigned long long)folderSizeAtPath:(NSString*)folderPath { NSFileManager *fileManager = [NSFileManager defaultManager]; BOOL isExist = [fileManager fileExistsAtPath:folderPath]; if (isExist) { NSEnumerator *childFileEnumerator = [[fileManager subpathsAtPath:folderPath] objectEnumerator]; unsigned long long folderSize = 0; NSString *fileName = @""; while ((fileName = [childFileEnumerator nextObject]) != nil){ NSString* fileAbsolutePath = [folderPath stringByAppendingPathComponent:fileName]; folderSize += [self fileSizeAtPath:fileAbsolutePath]; } return folderSize / (1024.0 * 1024.0); } else { NSLog(@"file is not exist"); return 0; } } // 删除文件 - (void)deleteFile { NSString *documentsPath =[self getDocumentsPath]; NSFileManager *fileManager = [NSFileManager defaultManager]; NSString *iOSPath = [documentsPath stringByAppendingPathComponent:@"iOS.txt"]; BOOL isSuccess = [fileManager removeItemAtPath:iOSPath error:nil]; if (isSuccess) { NSLog(@"delete success"); }else{ NSLog(@"delete fail"); } } // 移动文件 - (void)moveFileName { NSString *documentsPath =[self getDocumentsPath]; NSFileManager *fileManager = [NSFileManager defaultManager]; NSString *filePath = [documentsPath stringByAppendingPathComponent:@"iOS.txt"]; NSString *moveToPath = [documentsPath stringByAppendingPathComponent:@"iOS.txt"]; BOOL isSuccess = [fileManager moveItemAtPath:filePath toPath:moveToPath error:nil]; if (isSuccess) { NSLog(@"rename success"); }else{ NSLog(@"rename fail"); } } // 重命名 - (void)renameFileName { //通过移动该文件对文件重命名 NSString *documentsPath =[self getDocumentsPath]; NSFileManager *fileManager = [NSFileManager defaultManager]; NSString *filePath = [documentsPath stringByAppendingPathComponent:@"iOS.txt"]; NSString *moveToPath = [documentsPath stringByAppendingPathComponent:@"rename.txt"]; BOOL isSuccess = [fileManager moveItemAtPath:filePath toPath:moveToPath error:nil]; if (isSuccess) { NSLog(@"rename success"); }else{ NSLog(@"rename fail"); } }
什么是复杂对象?
在Foundation框架内不存在的数据类,如:自定义Person类无法在程序内通过writeToFile:这个方法写入到文件内
归档与反归档
如何将复杂对象写入文件?
复杂对象无法通过writeToFile:方法进行数据持久化,只能通过将复杂对象转换为NSData(这个步骤就是归档),然后在通过writeToFile:写入文件
如何从文件中读取复杂对象?
从文件中读取NSData数据,将NSData转换为复杂对象(这个步骤就是反归档)
记住:1、复杂对象写入文件的过程(复杂对象->归档->NSData->writeToFile)
2、从文件中读取出复杂对象过程(读取文件->NSData->反归档->复杂对象)
如何进行归档与反归档
1、首先,复杂对象所属的类要遵守协议
2、其次,实现协议中的两个方法:①、- (void)encodeWithCoder:(NSCoder *)aCoder;//序列化 ②、- (id)initWithCoder:(NSCoder *)aDecoder;//反序列化