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就能够看到被隐藏的文件了。

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

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2Vhc2xleXFp/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

每一个应用程序都有自己独有的文件夹:

/var/mobile/Applications/UUID

当应用程序安装时,UUID随机产生;

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

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

Documents/

Library/

Library/Caches

Library/Preferences

tmp/

.app

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2Vhc2xleXFp/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

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

.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免费下载地址:http://download.csdn.net/detail/weasleyqi/7508141

获取根文件夹:

    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);

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2Vhc2xleXFp/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="" width="380" height="800">

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

【iOS知识学习】_iOS沙盒机制的更多相关文章

  1. IOS开发-UI学习-沙盒机制&文件操作

    ž苹果为软件的运行提供了一个沙盒机制 每个沙盒含有3个文件夹:Documents, Library 和 tmp.因为应用的沙盒机制,应用只能在几个目录下读写文件 žDocuments:苹果建议将程序中 ...

  2. iOS 阶段学习第25天笔记(iOS沙盒机制介绍)

    iOS学习(OC语言)知识点整理 一.iOS沙盒机制介绍 1)概念: 每个ios应用都有自己的应用沙盒,应用沙盒就是文件系统目录,与其他应用放入文件 系统隔离,ios系统不允许访问 其他应用的应用沙盒 ...

  3. IOS 学习之 iOS沙盒(sandbox) 介绍 沙盒机制 文件操作(一)

    1.iOS沙盒机制 iOS应用程序只能在为该改程序创建的文件系统中读取文件,不可以去其它地方访问,此区域被成为沙盒,所以所有的非代码文件都要保存在此,例如图像,图标,声音,映像,属性列表,文本文件等. ...

  4. OS的沙盒机制 --基础知识

    /* iOS的沙盒机制,应用只能访问自己应用目录下的文件. iOS不像android,没有SD卡概念,不能直接访问图像.视频等内容. iOS应用产生的内容,如图像.文件.缓存内容等都必须存储在自己的沙 ...

  5. iOS学习之沙盒

    1.iOS沙盒 iOS应用程序只能在为该改程序创建的文件系统中读取文件,不可以去其它地方访问,此区域被成为沙盒,所以所有的非代码文件都要保存在此,例如图像,图标,声音,映像,属性列表,文本文件等. 1 ...

  6. iOS沙盒机制介绍,Block 的介绍

    一.iOS沙盒机制介绍 (转载) 1)概念:每个ios应用都有自己的应用沙盒,应用沙盒就是文件系统目录,与其他应用放入文件 系统隔离,ios系统不允许访问 其他应用的应用沙盒,但在ios8中已经开放访 ...

  7. IOS 沙盒机制 浅析

    IOS中的沙盒机制(SandBox)是一种安全体系,它规定了应用程序只能在为该应用创建的文件夹内读取文件,不可以访问其他地方的内容.所有的非代码文件都保存在这个地方,比如图片.声音.属性列表和文本文件 ...

  8. iOS之沙盒机制和如何获取沙盒路径

    iOS APP可以在自己的沙盒里读写文件,但是,不可以访问其他APP的沙盒.每一个APP都是一个信息孤岛,相互是不可以进行通信的,唯独可以通过URL Scheme.沙盒里面的文件可以是照片.声音文件. ...

  9. IOS 沙盒机制 && 关于document\library\tmp的灵活使用

    默认情况下,每个沙盒含有3个文件夹:Documents, Library 和 tmp.因为应用的沙盒机制,应用只能在几个目录下读写文件Documents:苹果建议将程序中建立的或在程序中浏览到的文件数 ...

随机推荐

  1. iOS 不同类之间的传值

    iOS是面向对象开发的,有很多不同的类,很多时候会遇到类与类之间的"交流"需求,比如通知.传递数值等等,(通知可以用nsnotificationcenter来做, 以后总结)下面主 ...

  2. 重新开始学习javase_集合_List

    一,List之ArrayList(转:http://blog.csdn.net/zheng0518/article/details/42198205) 1. ArrayList概述: ArrayLis ...

  3. grails框架中读取txt文件内容将内容转换为json格式,出现异常Exception in thread "main" org.json.JSONException: A JSONObject text must begin with '{' at character 1 of [...]

    Exception in thread "main" org.json.JSONException: A JSONObject text must begin with '{' a ...

  4. Subversion 1.7 Eclipse integration in Ubuntu12(转载)

    原文链接:http://steveliles.github.io/subversion_1_7_eclipse_integration_in_ubuntu.html Getting Subversio ...

  5. Llinux-apache安装

    第四章  构建LAMP网站服务平台 实验报告 1.安装apache服务器软件及相关组件 查看系统中是否安装apache服务相关的软件包: [root@www /]# rpm -qa | grep ht ...

  6. 转发:[Python]内存管理

    本文为转发,原地址为:http://chenrudan.github.io/blog/2016/04/23/pythonmemorycontrol.html 本文主要为了解释清楚python的内存管理 ...

  7. saas系统架构经验总结

    2B Saas系统最近几年都很火.很多创业公司都在尝试创建企业级别的应用 cRM, HR,销售, Desk Saas系统.很多Saas创业公司也拿了大额风投.毕竟Saas相对传统软件的优势非常明显. ...

  8. 总结:ARM逻辑和高级C(朱老师物联网学习)

    开始学习朱老师物联网的视频是国庆节的那几天开始的,刚刚开始的时候是想自己在网上找一些嵌入式方面的视频资料,也找了很多的资料臂如“国嵌视频”“达内的视频”,之后也化了十几块钱在淘宝上面买了几十个G的视频 ...

  9. A记录、CNAME记录、MX记录

    A 记录(Address)  (一句话:用来指定域名和服务器IP的对应关系) 是用来指定主机名(或域名)对应的IP地址记录.用户可以将该域名下的网站服务器指向到自己的web server上.同时也可以 ...

  10. ios学习-delegate、传值、跳转页面

    ios学习-delegate.传值.跳转页面     1.打开xcode,然后选择ios--Application--Empty Application一个空项目. 项目目录: 2.输入项目名称以及选 ...