iOS 沙盒(sandbox)结构 使用 实例
声明:该文档是经过自己查找网上的资料以及自己多年的经验后而总结出来的,希望对大家有所帮助,有什么不恰当支出还请大家多指点!
iOS中的沙盒机制(SandBox)是一种安全体系,它规定了应用程序只能在为该应用创建的文件夹内读取文件,不可以访问其他地方的内容。所有的非代码文件都保存在这个地方,比如图片、声音、属性列表和文本文件等。
沙盒(sandbox)的结构:
1、myApp.app
2、documents
3、library
4、tmp
各个目录的作用和是否会被iTunes同步:
1、myApp.app
储存内容: 目录包含了应用程序本身的数据,包含资源文件和可执行文件。整个目录是只读的,防止被篡改,,应用被安装时会将该目录签名。
是否会被iTunes同步: 否
2.0、documents
储存内容:存放不可再生数据文件(存储用户数据或其它应该定期备份的信息)
是否会被iTunes同步: 是
2.1、Documents/Inbox
储存数据:用来保存由外部应用请求当前应用程序打开的文件。例如:现有一个应用book.app
可以打开txt格式的文件,而在另一个应用BookShop.app
有一个a.txt的文件需要用book.app
打开,但沙盒机制不允许book
直接访问BookShop
的沙盒中文件,所以苹果的解决办法是讲BookShop
中的a.txt文件拷贝到book
中的Documents/Inbox
下,再让book
打开a.txt。
是否会被iTunes同步: 是
3.0、Library
储存内容:建议存放默认数据或其他状态信息。
是否会被iTunes同步: 是(不包括Caches子目录)
3.1、Library/Caches
储存内容:主要储存缓存文件,使用过程中的缓存文件可以储存在这里。用于存储体积大可再生的文件。比如网络请求,但一般应用程序要负责清理这些数据。
是否会被iTunes同步: 否
3.2、Library/Preferences
储存内容:储存应用的偏好设置文件,一般我们使用NSUserDefaules写的数据都会存在这个文件的plist文件中。
是否会被iTunes同步: 是
4、tmp
储存内容:各种临时文件,保证再次启动不需要的文件,当应用不再需要这些文件时应该主动将其删除,因为随时可能被系统清理。
是否会被iTunes同步: 否
获取各个目录的方法:
//获取根目录
NSString *homePath = NSHomeDirectory();
NSLog(@"\n根目录:%@",homePath);
//获取documents目录
NSString *docmentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSLog(@"\n获取Documents目录:%@",docmentPath);
//另一种方式
NSURL *documentURL = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] firstObject];
NSLog(@"\n获取Documents目录%@",documentURL);
//获取Librarys
NSString *libraryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) firstObject];
NSLog(@"\n获取Libray目录:%@",libraryPath);
//同样另一种方式
NSURL *libraryURL = [[[NSFileManager defaultManager] URLsForDirectory:NSLibraryDirectory inDomains:NSUserDomainMask] firstObject];
NSLog(@"\n获取Libray目录:%@",libraryURL);
//获取Cache目录
NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
NSLog(@"\n获取Cache目录:%@",cachePath);
NSURL *cacheURL = [[[NSFileManager defaultManager] URLsForDirectory:NSCachesDirectory inDomains:NSUserDomainMask]firstObject];
NSLog(@"\n获取Cache目录:%@",cacheURL);
//temp目录
NSString *tempPath = NSTemporaryDirectory();
NSLog(@"temp目录:%@",tempPath);
实例:
//获取Documents路径
- (NSString *)getDocumentsPath
{
//获取Documents路径
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");
}
}
iOS 沙盒(sandbox)结构 使用 实例的更多相关文章
- IOS学习之IOS沙盒(sandbox)机制和文件操作
IOS学习之IOS沙盒(sandbox)机制和文件操作(一) 1.IOS沙盒机制 IOS应用程序只能在为该改程序创建的文件系统中读取文件,不可以去其它地方访问,此区域被成为沙盒,所以所有的非代码文件都 ...
- IOS沙盒(sandbox)机制和文件操作
IOS学习之IOS沙盒(sandbox)机制和文件操作 作者:totogo2010 ,发布于2012-9-21,来源:CSDN 目录: IOS学习之IOS沙盒(sandbox)机制和文件操作( ...
- iOS沙盒目录结构解析
iOS沙盒目录结构解析 原文地址:http://blog.csdn.net/wzzvictory/article/details/18269713 出于安全考虑,iOS系统的沙盒机制规定每个应 ...
- iOS学习之iOS沙盒(sandbox)机制和文件操作1
iOS学习之iOS沙盒(sandbox)机制和文件操作 接上篇 iOS学习之iOS沙盒(sandbox)机制和文件操作(一) 我们看看如何获取应用程序沙盒目录.包括真机的沙盒的目录. 1.获取程序的H ...
- IOS 学习之 iOS沙盒(sandbox) 介绍 沙盒机制 文件操作(一)
1.iOS沙盒机制 iOS应用程序只能在为该改程序创建的文件系统中读取文件,不可以去其它地方访问,此区域被成为沙盒,所以所有的非代码文件都要保存在此,例如图像,图标,声音,映像,属性列表,文本文件等. ...
- iOS 沙盒(sandbox)机制和文件操作
本文参看了 http://www.uml.org.cn/mobiledev/201209211.asp#1 这篇文章中的介绍,尊重原著. 1.IOS沙盒机制 IOS应用程序只能在本应用程序中创建的文件 ...
- iOS学习之iOS沙盒(sandbox)机制和文件操作(一)
1.iOS沙盒机制 iOS应用程序仅仅能在为该改程序创建的文件系统中读取文件,不能够去其他地方訪问,此区域被成为沙盒,所以全部的非代码文件都要保存在此,比如图像,图标,声音,映像,属性列表,文本文件等 ...
- iOS学习之iOS沙盒(sandbox)机制和文件操作复习
1.iOS沙盒机制 iOS应用程序只能在为该改程序创建的文件系统中读取文件,不可以去其它地方访问,此区域被成为沙盒,所以所有的非代码文件都要保存在此,例如图像,图标,声音,映像,属性列表,文本文件等. ...
- iOS学习7:iOS沙盒(sandBox)机制(一)之获取沙盒路径及目录说明(转)
转:http://my.oschina.net/joanfen/blog/151145 一.iOS沙盒机制 iOS的应用只能访问为该应用创建的区域,不可访问其他区域,应用的其他非代码文件都存在此目录下 ...
- iOS 沙盒目录结构介绍
iOS系统中,每个应用都有自己的沙盒,且应用只能访问其对应的沙盒目录下面的文件.当然,在用户授权的情况下,应用也可以访问其他目录下面的文件.比如,用户授权情况下,应用可以访问相册.通讯录.在开发中,经 ...
随机推荐
- XE7 & IOS开发之开发账号(1):开发证书、AppID、设备、开发授权profile的申请使用,附Debug真机调试演示(XCode所有版本通用,有图有真相)
网上能找到的关于Delphi XE系列的移动开发的相关文章甚少,本文尽量以详细的图文内容.傻瓜式的表达来告诉你想要的答案. 原创作品,请尊重作者劳动成果,转载请注明出处!!! 注意,以下讨论都是以&q ...
- eclipse下的webservice开发
关于eclipse下的webservice开发,有非常多的教程,这里只记下学习过程中的弯路: 1.无论是CXF模式还是AXIS模式,在出现start server之后,点击next报错:"s ...
- windows 服务
一.创建一个Windows Service1)创建Windows Service项目 2)对Service重命名将Service1重命名为你服务名称,这里我们命名为ServiceTest. 二.创建服 ...
- 解决VS2012上面EF字段说明备注没有的方法
VS2012中的EF有一个BUG 如下: 明明在数据库上面是写有字段说明的到了EF上面就没有了很郁闷: 网络上面有一个解决方法如下: http://www.cnblogs.com/stone_w/ar ...
- HTTP头的Expires与Cache-control
HTTP头的Expires与Cache-control 1.概念 Cache-control用于控制HTTP缓存(在HTTP/1.0中可能部分没实现,仅仅实现了Pragma: no-cache) 数据 ...
- Build subversion 1.8 with SSL on OS X Yosemite
mkdir -p /tmp/buildroot && cd /tmp/buildroot # Need to build the latest libtool and automake ...
- python2.7.9基础学习
一. 基 础 python python开头两行注释代码意义: #!/usr/bin/python 是用来说明脚本语言是python的,是要用/usr/bin下面的程序(工具)py ...
- WIn2003的IIS6解决IE11登录问题。
一键批处理文件下载:http://download.csdn.net/detail/hospp/6850835 1.打开文件夹C:\Windows\Microsoft.NET\Framework\v4 ...
- sybase ODBC驱动
windows64位系统ODBC数据源管理器位置 64位 C:\Windows\System32\odbcad32.exe 32位 C:\Windows\SysWOW64\odbcad32.exe s ...
- 关于conky