【iOS知识学习】_iOS沙盒机制

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免费下载地址: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);

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

【iOS知识学习】_iOS沙盒机制

时间: 2024-08-02 05:11:46

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

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

?苹果为软件的运行提供了一个沙盒机制 每个沙盒含有3个文件夹:Documents, Library 和 tmp.因为应用的沙盒机制,应用只能在几个目录下读写文件 ?Documents:苹果建议将程序中建立的或在程序中浏览到的文件数据保存在该目录下,iTunes备份和恢复的时候会包括此目录 ?Library:存储程序的默认设置或其它状态信息: ?Library/Caches:存放缓存文件,iTunes不会备份此目录,此目录下文件不会在应用退出删除: ?tmp:提供一个即时创建临时文件的地方, 这三

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

IOS学习(OC语言)知识点整理 一.IOS沙盒机制介绍 1)概念:每个ios应用都有自己的应用沙盒,应用沙盒就是文件系统目录,与其他应用放入文件 系统隔离,ios系统不允许访问 其他应用的应用沙盒,但在ios8中已经开放访问(extension) 2)extension是ios8新开放的一种对几个固定系统区域的拓展机制,它可以在一定程度上弥补ios的沙盒机制对应用间的通信限制 3)应用沙盒一般包括以下几个文件目录: 1.应用程序包:包含所有资源文件和可执行文件 2.Documents:保存应用

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

1.iOS沙盒机制 iOS应用程序只能在为该改程序创建的文件系统中读取文件,不可以去其它地方访问,此区域被成为沙盒,所以所有的非代码文件都要保存在此,例如图像,图标,声音,映像,属性列表,文本文件等. 1.1.每个应用程序都有自己的存储空间 1.2.应用程序不能翻过自己的围墙去访问别的存储空间的内容 1.3.应用程序请求的数据都要通过权限检测,假如不符合条件的话,不会被放行.     通过这张图只能从表层上理解sandbox是一种安全体系,应用程序的所有操作都要通过这个体系来执行,其中核心内容是

【学习ios之路:UI系列】iOS沙盒机制,文件读取,归档与反归档

1.IOS中的沙盒机制 IOS中的沙盒机制是一种安全体系,它规定了应用程序只能在为该应用创建的文件夹内读取文件,不可以访问其他地方的内容.所有的非代码文件都保存在这个地方,比如图片.声音.属性列表和文本文件等. 特点: 1.每个应用程序都在自己的沙盒内 2.不能随意跨越自己的沙盒去访问别的应用程序沙盒的内容 3.应用程序向外请求或接收数据都需要经过权限认证 每个沙盒含有3个文件夹:Documents, Library 和 tmp.Library包含Caches.Preferences目录.如下图

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

/* iOS的沙盒机制,应用只能访问自己应用目录下的文件. iOS不像android,没有SD卡概念,不能直接访问图像.视频等内容. iOS应用产生的内容,如图像.文件.缓存内容等都必须存储在自己的沙盒内. 默认情况下,每个沙盒含有3个文件夹:Documents, Library 和 tmp.Library包含Caches.Preferences目录.  上面的完整路径为:用户->资源库->Application Support->iPhone Simulator->7.1->

iOS之沙盒机制

今天有讲到数据持久化的问题,就有涉及到数据存储位置的问题.iOS对于数据安全问题做的很严谨,使用的沙盒机制(Sandbox),相对于安卓系统而言,一下子真的是难以消化. 在iOS8之前的沙盒文件夹中存在四个文件夹 Document  存储用户数据,需要备份的信息(数据持久化操作的文件夹) Library Library/Caches  存储缓存文件,程序专用的支持文件 Library/Preferences  存储应用程序的偏好设置文件 .app   程序包(iOS8时,app不存储在沙盒中,有

【iOS知识学习】_iOS动态改变TableView Cell高度

在做tableView的时候,我们有时候需要根据cell的高度动态来调整,最近在网上看到一段代码不错,跟大家Share一下. 在 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 类中获取cell的高度: CGSize boundSize = CGSizeMake(216, CGFLOAT_MAX); cell.textLabel.text

IOS-沙盒机制(一 简述)

一 IOS沙盒机制 出于安全的目的,应用程序只能将自己的数据和偏好设置写入到几个特定的位置上.当应用程序被安装到设备上时,系统会为其创建一个家目录,这个家目录就是应用程序的沙盒.所以的文件都要保存在此,例如图像,图标,声音,映像,属性列表,文本文件等.(For security reasons, iOS places each app (including its preferences and data) in a sandbox at install time. A sandbox is a

IOS-沙盒机制(二 文件读写)

一 目录说明 如下图所示,一个沙盒中典型存在下面的目录和文件 各个目录及文件说明: 1.Documents      您应该将所有的应用程序数据文件写入到这个目录下,这个目录用于存储用户数据或其它应该定期备份的信息. 2.AppName.app   这是应用程序的程序包目录,包含应用程序的本身.由于应用程序必须经过签名,所以您在运行时不能对这个目录中的内容进行修改,否则可能会使应用程序无法启动. 3.Library              这个目录下有两个子目录:Caches 和 Prefer