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

/*

iOS的沙盒机制,应用只能访问自己应用目录下的文件。

iOS不像android,没有SD卡概念,不能直接访问图像、视频等内容。
 iOS应用产生的内容,如图像、文件、缓存内容等都必须存储在自己的沙盒内。
 默认情况下,每个沙盒含有3个文件夹:Documents, Library 和 tmp。Library包含Caches、Preferences目录。
 上面的完整路径为:用户->资源库->Application Support->iPhone Simulator->7.1->Aplications
 
 Documents:苹果建议将程序创建产生的文件以及应用浏览产生的文件数据保存在该目录下,iTunes备份和恢复的时候会包括此目录
 Library:存储程序的默认设置或其它状态信息;
 Library/Caches:存放缓存文件,保存应用的持久化数据,用于应用升级或者应用关闭后的数据保存,不会被itunes同步,所以为了减少同步的时间,可以考虑将一些比较大的文件而又不需要备份的文件放到这个目录下。
 tmp:提供一个即时创建临时文件的地方,但不需要持久化,在应用关闭后,该目录下的数据将删除,也可能系统在程序不运行的时候清除
 */
#import "NSFileManagerViewController.h"

@interface NSFileManagerViewController ()

@end

@implementation NSFileManagerViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //获取沙盒目录
    NSString *dirHome = NSHomeDirectory();
    NSLog(@"APP_home:%@",dirHome);
    
    [self dirDoc];
    [self dirLib];
    [self dirCache];
    [self dirTmp];
    [self createDir];
    [self createFile];
    [self redFile];
    [self  fileAttriutes];
    [self deleteFile];
}

//获取Document目录路径
- (NSString *)dirDoc {
    //[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [path objectAtIndex:0];
    NSLog(@"app_home_doc:%@",documentsDirectory);
    return documentsDirectory;
}

-(void)dirLib{
    //[NSHomeDirectory() stringByAppendingPathComponent:@"Library"];
    NSArray *path = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    NSString *libraryDirectory = [path objectAtIndex:0];
    NSLog(@"app_home_lib:%@",libraryDirectory);
}

- (void)dirCache{
    NSArray *cacPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *cachePath = [cacPath  objectAtIndex:0];
    NSLog(@"app_home_lib:%@",cachePath);
}

- (void)dirTmp{
    //[NSHomeDirectory() stringByAppendingPathComponent:@"tmp"];
    NSString *tmpDirectory = NSTemporaryDirectory();
    NSLog(@"app_home_tmp: %@",tmpDirectory);
}

// 穿件文件夹
-(void)createDir{
    NSString *documentsPAth = [self dirDoc];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *testDirectory = [documentsPAth stringByAppendingString:@"test"];
    //创建目录
    BOOL res = [fileManager createDirectoryAtPath:testDirectory
                      withIntermediateDirectories:YES
                                       attributes:nil
                                            error:nil];
    if(res)
    {
        NSLog(@"文件夹创建成功");
    }
    else
    {
        NSLog(@"文件夹创建失败");
    }
}

//创建文件
- (void)createFile {
    NSString *documentPath = [self dirDoc];
    NSString *testDirectory = [documentPath stringByAppendingPathComponent:@"test"];
    NSFileManager *filemanager = [NSFileManager defaultManager];
    NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"];
    BOOL res = [filemanager createFileAtPath:testPath contents:nil attributes:nil];
    if (res) {
        NSLog(@"文件创建成功:%@",testPath);
    }else{
        NSLog(@"文件创建失败");
    }
}
- (void)writeFile{
    NSString *documentsPath = [self dirDoc];
    NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];
    NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"];
    NSString *[email protected]"测试写入内容!";
    BOOL res=[content writeToFile:testPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
    if (res) {
        NSLog(@"文件写入成功");
    }else
        NSLog(@"文件写入失败");
}

//读文件
- (void)redFile
{
    NSString *documentsPath =[self dirDoc];
    NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];
    NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"];
    //    NSData *data = [NSData dataWithContentsOfFile:testPath];
    //    NSLog(@"文件读取成功: %@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
    NSString *content=[NSString stringWithContentsOfFile:testPath encoding:NSUTF8StringEncoding error:nil];
    NSLog(@"文件读取成功: %@",content);
}

//文件属性
-(void)fileAttriutes{
    NSString *documentsPath =[self dirDoc];
    NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"];
    NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:testPath error:nil];
    NSArray *keys;
    id key, value;
    keys = [fileAttributes allKeys];
    int count = [keys count];
    for (int i = 0; i < count; i++)
    {
        key = [keys objectAtIndex: i];
        value = [fileAttributes objectForKey: key];
        NSLog (@"Key: %@ for value: %@", key, value);
    }
}
//删除文件
-(void)deleteFile{
    NSString *documentsPath =[self dirDoc];
    NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"];
    BOOL res=[fileManager removeItemAtPath:testPath error:nil];
    if (res) {
        NSLog(@"文件删除成功");
    }else
        NSLog(@"文件删除失败");
    NSLog(@"文件是否存在: %@",[fileManager isExecutableFileAtPath:testPath][email protected]"YES":@"NO");
}

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

时间: 2024-08-10 18:03:56

OS的沙盒机制 --基础知识的相关文章

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

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

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

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

UI 沙盒机制 文件的读写操作

沙盒机制: 数据持久化的本质:将数据读成文件,存储在本地 沙盒机制:沙盒就是系统针对于每个应用程序在本地生成的文件夹(名字随机生成).对于不同的应用程序,不能访问其他应用程序沙盒内的内容.对于该应用内容起到保护作用. 沙盒内的文件夹: (1)Documents:用来存储长久保存的数据 (2)XXX.app: 应用程序的包,包含应用程序加载所需的所有资源,(readonly 只读,不可修改).平时使用的NSBundle就是该包. (3)Library: A: Caches:本地缓存,存储想暂时保存

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

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

iOS之沙盒机制

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

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

IOS 沙盒机制 浅析

IOS中的沙盒机制(SandBox)是一种安全体系,它规定了应用程序只能在为该应用创建的文件夹内读取文件,不可以访问其他地方的内容.所有的非代码文件都保存在这个地方,比如图片.声音.属性列表和文本文件等. 1.每个应用程序都在自己的沙盒内(提示:在IOS8中已经开放访问) 2.不能随意跨越自己的沙盒去访问别的应用程序沙盒的内容 3.应用程序向外请求或接收数据都需要经过权限认证 //获取home目录 NSString *homePath = NSHomeDirectory(); NSLog(@"h

iOS的沙盒机制

数据持久化:就是把数据从内存中写入到本地(对于iOS开发来说,就是把数据写入到应用的沙盒文件夹在中). 沙盒(SandBox):沙盒的本质就是文件夹,iOS采用沙盒机制来管理每一个安装到手机上的应用,每一个应用都有一个沙盒,而且这个沙盒相对独立,封闭的,安全的,通常情况下只有自己才能访问自己. 沙盒安全机制:沙盒的文件是随机生成的,而且每次都会生成一个沙盒的文件名,且采用十六进制数字命名法. #import "ViewController.h" #define kName @"