1.沙盒目录结构
沙盒中的目录结构包括:
》Documents:保存应用运行时生成的需要持久化的数据,iTunes备份和恢复的时候会包括此目录。
》Library:存储的程序默认的设置和其他信息。
》Library/Caches:存放缓存文件,iTunes不会备份此目录,此目录下文件不会在应用退出删除
》Library/Preferences:保存应用程序的所有偏好设置iOS的Settings(设置),我们不应该直接在这里创建文件,而是需要通过NSUserDefault这个类来访问应用程序的偏好设置。iTunes会自动备份该文件目录下的内容。比如说:是否允许访问图片,是否允许访问地理位置。
》SystemData:存放系统数据。
》tmp:临时文件目录,在程序重新运行的时候,和开机的时候,会清空tmp文件夹。
2.获取方式1
SandBox Home: NSHomeDirectory()
SandBox Document: NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject
SandBox Library: NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES).lastObject
SandBox Lib/Caches: NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject
SandBox Tmp: NSTemporaryDirectory()
SandBox Lib/Preferences: 这个地方一般使用偏好设置 NSUserDefaults
//主目录获取
NSString *home = [NSHomeDirectory() stringByAppendingPathComponent:@"home"];
//document获取
NSString *document = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"document"];
//lib目录获取
NSString *lib = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"lib"];
//lib子目录cache获取
NSString *libCache = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"libcache"];
//tmp目录获取
NSString *tmp = [NSTemporaryDirectory() stringByAppendingPathComponent:@"tmp"];
//Library/Preferences 偏好设置
[[NSUserDefaults standardUserDefaults] setObject:@"Library/Preferences" forKey:@"Library/Preferences"];
[@"document" writeToFile:document atomically:YES encoding:NSUTF8StringEncoding error:nil];
[@"libCache" writeToFile:libCache atomically:YES encoding:NSUTF8StringEncoding error:nil];
[@"tmp" writeToFile:tmp atomically:YES encoding:NSUTF8StringEncoding error:nil];
[@"home" writeToFile:home atomically:YES encoding:NSUTF8StringEncoding error:nil];
[@"lib" writeToFile:lib atomically:YES encoding:NSUTF8StringEncoding error:nil];
3.获取方式2 <建议采用方式1进行操作>
NSString *main = NSHomeDirectory();
NSString *home = [main stringByAppendingPathComponent:@"home"];
NSString *document = [main stringByAppendingPathComponent:@"Documents/document"];
NSString *libCache = [main stringByAppendingPathComponent:@"Library/Caches/libcache"];
NSString *lib = [main stringByAppendingPathComponent:@"Library/lib"];
NSString *tmp = [main stringByAppendingPathComponent:@"tmp/tmp"];
NSString *libpre = [main stringByAppendingPathComponent:@"Library/Preferences/pre"];
[@"message" writeToFile:document atomically:YES encoding:NSUTF8StringEncoding error:nil];
[@"message" writeToFile:libCache atomically:YES encoding:NSUTF8StringEncoding error:nil];
[@"message" writeToFile:tmp atomically:YES encoding:NSUTF8StringEncoding error:nil];
[@"message" writeToFile:home atomically:YES encoding:NSUTF8StringEncoding error:nil];
[@"message" writeToFile:lib atomically:YES encoding:NSUTF8StringEncoding error:nil];
[@"message" writeToFile:libpre atomically:YES encoding:NSUTF8StringEncoding error:nil];