博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【iOS知识学习】_iOS沙盒机制
阅读量:6855 次
发布时间:2019-06-26

本文共 3779 字,大约阅读时间需要 12 分钟。

IOS中的沙盒机制(SandBox)是一种安全体系,它规定了应用程序仅仅能在为该应用创建的目录内读取文件,不能够訪问其它地方的内容。全部的非代码文件都保存在这个地方。比方图片、声音、属性列表和文本文件等。
1.每一个应用程序都在自己的沙盒内
2.不能任意跨越自己的沙盒去訪问别的应用程序沙盒的内容
3.应用程序向外请求或接收数据都须要经过权限认证
显示和隐藏目录的方法:
显示Mac隐藏文件的命令:defaults write com.apple.finder AppleShowAllFiles -bool true
隐藏Mac隐藏文件的命令:defaults write com.apple.finder AppleShowAllFiles -bool false

然后又一次启动Finder,点击屏幕左上角苹果标志——强制退出——选择Finder然后点击又一次启动,这个时候在又一次打开Finder就能够看到被隐藏的文件了。

模拟器里的内容路径例如以下:

每一个应用程序都有自己独有的文件夹:
/var/mobile/Applications/UUID
当应用程序安装时,UUID随机产生;

当删除应用程序又一次安装的时候会又一次生成文件夹

每一个app文件夹下都有下面几个文件夹:

Documents/

Library/

Library/Caches

Library/Preferences

tmp/

.app

每一个文件(目录)的功能例如以下:

.app/,这个就是可执行的应用文件。带有签名的文件包,包括应用程序代码和静态数据;

Documents/
:Use this directory to store critical user documents and app data files. Critical data is any data that cannot be recreated by your app, such as user-generated content.
The contents of this directory can be made available to the user through file sharing. The contents of this directory are backed up by iTunes.
:苹果建议将程序中创建的或在程序中浏览到的文件数据保存在该文件夹下,iTunes备份和恢复的时候会包含此文件夹。
Library/
:This directory is the top-level directory for files that are not user data files. You typically put files in one of several standard subdirectories but you can also create custom subdirectories for files you want backed up but not exposed to the user. You should not use this directory for user data files.
The contents of this directory (with the exception of the Caches subdirectory) are backed up by iTunes.
For additional information about the Library directory, see “The Library Directory Stores App-Specific Files.”
:应用程序支持文件;
Library/Preferences/ :存储程序的默认设置或其他状态信息;
Library/Caches/:存放缓存文件,iTunes不会备份此文件夹,此文件夹下文件不会在应用退出删除;当系统清理磁盘空间的时候会删除里面的内容。

tmp/
:Use this directory to write temporary files that do not need to persist between launches of your app. Your app should remove files from this directory when it determines they are no longer needed. (The system may also purge lingering files from this directory when your app is not running.)
In iOS 2.1 and later, the contents of this directory are not backed up by iTunes.
:创建和存放暂时文件的地方,应用程序成功启动,暂时文件不须要持久保存。

注:

iTunes在与iPhone同步时。备份全部的Documents和Library文件。

iPhone在重新启动时。会丢弃全部的tmp文件。

以下是获取文件文件夹的代码以及读写文件的代码:演示样例Demo免费下载地址:

获取根文件夹:

NSString *homePath = NSHomeDirectory();    NSLog(@"Home文件夹:%@",homePath);

获取Document文件夹:

NSArray *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);    NSString *documentsPath = [docPath objectAtIndex:0];
获取Library文件夹:

NSArray *libsPath = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);    NSString *libPath = [libsPath objectAtIndex:0];
获取Cache文件夹:

NSArray *cacPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);    NSString *cachePath = [cacPath objectAtIndex:0];
获取Tmp文件夹:

NSString *tempPath = NSTemporaryDirectory();    NSLog(@"temp文件夹:%@",tempPath);
写入文件:

NSArray *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);    NSString *documentsPath = [docPath objectAtIndex:0];    //写入文件    if (!documentsPath) {        NSLog(@"文件夹未找到");    }else {        NSString *filePaht = [documentsPath stringByAppendingPathComponent:@"test.txt"];        NSArray *array = [NSArray arrayWithObjects:@"Title",@"Contents", nil];        [array writeToFile:filePaht atomically:YES];    }
读取文件:

NSArray *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);    NSString *documentsPath = [docPath objectAtIndex:0];    NSString *readPath = [documentsPath stringByAppendingPathComponent:@"test.txt"];    NSArray *fileContent = [[NSArray alloc] initWithContentsOfFile:readPath];    NSLog(@"文件内容:%@",fileContent);

演示样例Demo下载地址:http://download.csdn.net/detail/weasleyqi/7508141

你可能感兴趣的文章
Linux-文件处理命令-file
查看>>
关于一个小程序
查看>>
利用dispatch_once创建单例
查看>>
Centos LVS DR模式详细搭建过程
查看>>
失败者共性
查看>>
批量修改文件名和移动文件
查看>>
常用设计模式(C++示例)
查看>>
一段有趣的代码,猜生日
查看>>
SQL SERVER 2005索引自动维护
查看>>
80后的记忆
查看>>
05、AGDLP组的嵌套
查看>>
加密解密与OPENSSL建立私有CA
查看>>
【ZBar】ios错误ignoring file xxx missing required architecture x86_64 in file
查看>>
实例详解top
查看>>
linux 把nginx加入到系统服务,并开机自己启动的方法
查看>>
制作一个按标签首字母分类的WordPress标签页
查看>>
在宿主机查看docker使用cpu、内存、网络、io情况
查看>>
Oracle 数据库入门之----------------------单行函数
查看>>
我的友情链接
查看>>
细细品味Hadoop_Hadoop集群精华文档合集
查看>>