iOS沙盒机制

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

1.1、每个应用程序都有自己的存储空间
 1.2、应用程序不能翻过自己的围墙去访问别的存储空间的内容
 1.3、应用程序请求的数据都要通过权限检测,假如不符合条件的话,不会被放行。
     通过这张图只能从表层上理解sandbox是一种安全体系,应用程序的所有操作都要通过这个体系来执行,其中核心内容是:sandbox对应用程序执行各种操作的权限限制。

 //获得document文件路径,名字方便记忆
+(NSString *)documentsPath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
return [paths objectAtIndex:];
} //获取Cache目录
+(NSString *)cachePath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
return [paths objectAtIndex:];
}
//获取Library目录
+(NSString *)libraryPath{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
return [paths objectAtIndex:];
}
//获取tmp目录
+(NSString *)tmpPath{
NSString *tmpDir = NSTemporaryDirectory();
return tmpDir;
}
+(NSString *)homePath{
NSString *paths = NSHomeDirectory();
return paths;
}
//获得document文件路径,名字方便记忆
+(NSString *) DocumentPath:(NSString *)filename {
NSString *documentsPath = [self documentsPath];
//NSLog(@"documentsPath=%@",documentsPath);
return [documentsPath stringByAppendingPathComponent:filename];
} //====================================================================================//
//获得document文件路径
+(NSString *)fullpathOfFilename:(NSString *)filename {
NSString *documentsPath = [self documentsPath];
// NSLog(@"documentsPath=%@",documentsPath);
return [documentsPath stringByAppendingPathComponent:filename];//这里就是 documentsPath + filePath
} //写入文件沙盒位置NSDictionary
+(void)saveNSDictionaryForDocument:(NSDictionary *)list FileUrl:(NSString*) FileUrl {
NSString *f = [self fullpathOfFilename:FileUrl];//调用了前面一个方法
[list writeToFile:f atomically:YES];
}
/*
* resourcePath: /Users/HeYang/Library/Developer/CoreSimulator/Devices/-
* -A089E19C-0F9D-4D69-AA33-D253157E4B94/data/Containers/Bundle/Application/088B4C6E-A765-4CFE-AD9B-2F12E9E0AB28/OCUITest.app
*/
//====================================================================================// //NSDictionary对象写入文件存放到工程位置
+(void)saveNSDictionaryForProduct:(NSDictionary *)list FileUrl:(NSString*) FileUrl{
NSString *ProductPath =[[NSBundle mainBundle] resourcePath];
NSString *f=[ProductPath stringByAppendingPathComponent:FileUrl];//这里就是 resourcePath + filePath
[list writeToFile:f atomically:YES];
} //Array写入文件 -> 工程
+(void)saveOrderArrayListProduct:(NSMutableArray *)list FileUrl:(NSString*)FileUrl {
NSString *ProductPath =[[NSBundle mainBundle] resourcePath];
NSString *f=[ProductPath stringByAppendingPathComponent:FileUrl];
[list writeToFile:f atomically:YES];
}
//Array对象 写入文件 -> 沙盒
+(void)saveOrderArrayList:(NSMutableArray *)list FileUrl:(NSString*) FileUrl{
NSString *f = [self fullpathOfFilename:FileUrl];
[list writeToFile:f atomically:YES];
}
//加载文件沙盒的文件 ->NSDictionary
+(NSDictionary *)loadNSDictionaryForDocument:(NSString*)FileUrl {
NSString *f = [self fullpathOfFilename:FileUrl];
NSDictionary *list = [[NSDictionary alloc] initWithContentsOfFile:f];
return [list autorelease];
}
//加载文件工程位置的文件 ->NSDictionary
+(NSDictionary *)loadNSDictionaryForProduct:(NSString*)FileUrl {
NSString *f = [self ProductPath:FileUrl];
NSDictionary *list =[NSDictionary dictionaryWithContentsOfFile:f];
return list;
}
//加载文件沙盒的文件 -> NSArray
+(NSArray *)loadArrayList:(NSString*) FileUrl {
NSString *f = [self fullpathOfFilename:FileUrl];
NSArray *list = [NSArray arrayWithContentsOfFile:f];
return list;
}
//加载文件工程位置的文件 -> NSArray
+(NSArray *)loadArrayListProduct:(NSString*) FileUrl {
NSString *f = [self ProductPath:FileUrl];
NSArray *list = [NSArray arrayWithContentsOfFile:f];
return list;
} //====================================================================================//
//获得document文件路径
+(NSString *)fullpathOfFilename:(NSString *)filename {
NSString *documentsPath = [self documentsPath];
// NSLog(@"documentsPath=%@",documentsPath);
return [documentsPath stringByAppendingPathComponent:filename];//这里就是 documentsPath + filePath
}
//拷贝文件到沙盒
+(int) CopyFileToDocument:(NSString*)FileName{
NSString *appFileName =[self fullpathOfFilename:FileName];
NSFileManager *fm = [NSFileManagerdefaultManager];
//判断沙盒下是否存在
BOOL isExist = [fm fileExistsAtPath:appFileName];
if (!isExist) //不存在,把工程的文件复制document目录下
{
//获取工程中文件
NSString *backupDbPath = [[NSBundle mainBundle]
pathForResource:FileName
ofType:@""];
//这一步实现数据库的添加,
// 通过NSFileManager 对象的复制属性,把工程中数据库的路径复制到应用程序的路径上
BOOL cp = [fm copyItemAtPath:backupDbPath toPath:appFileName error:nil];
return cp;
} else {
return -; //已经存在
}
}
//====================================================================================// //判断文件是否存在
+(BOOL) FileIsExists:(NSString*)checkFile{
if([[NSFileManager defaultManager] fileExistsAtPath:checkFile])
{
return true;
}
return false;
} //读取工程文件
+(NSString *) ProductPath:(NSString*)filename{
NSString *path = [[NSBundle mainBundle] pathForResource:filename ofType:@""];
return path;
}

打开模拟器沙盒目录

下面看看模拟器的沙盒文件夹在mac电脑上的什么位置。

文件都在个人用户名文件夹下的一个隐藏文件夹里,中文叫资源库,他的目录其实是Library。

  方法1、可以设置显示隐藏文件,然后在Finder下直接打开。设置查看隐藏文件的方法如下:打开终端,输入命名

显示Mac隐藏文件的命令:defaults write com.apple.finder AppleShowAllFiles -bool true

隐藏Mac隐藏文件的命令:defaults write com.apple.finder AppleShowAllFiles -bool false

输完单击Enter键,退出终端,重新启动Finder就可以了
重启Finder:鼠标单击窗口左上角的苹果标志-->强制退出-->Finder-->

现在能看到资源库文件夹了。

打开资源库后找到/Application Support/iPhone Simulator/文件夹。这里面就是模拟器的各个程序的沙盒目录了。

  方法2、这种方法更方便,在Finder上点->前往->前往文件夹,输入/Users/username/Library/Application Support/iPhone Simulator/  前往。

username这里写你的用户名。

 
 
 
 
 
 
 
 

IOS应用沙盒文件操作的更多相关文章

  1. iOS关于沙盒文件拷贝manager.copyItem的一个坑

    记录一下: 沙盒文件操作,当需要拷贝文件时,我们可以使用如下类似方式: // 文件拷贝 func copyFile(from:String,to:String)->Bool{ if !manag ...

  2. 【iOS系列】-iOS查看沙盒文件图文教程(真机+模拟器)

    [iOS系列]-iOS查看沙盒文件图文教程(真机+模拟器) 1:模拟器 1.1 方法1: 程序中打印一下的地址,能直接前往沙盒路径. NSString *path = [NSSearchPathFor ...

  3. 【转】【iOS系列】-iOS查看沙盒文件图文教程(真机+模拟器)

    原文网址:http://www.cnblogs.com/fengtengfei/p/5090276.html 1:模拟器 1.1 方法1: 程序中打印一下的地址,能直接前往沙盒路径. NSString ...

  4. iOS 沙盒文件操作

    //获得document +(NSString *)documentsPath { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDoc ...

  5. iOS 获取沙盒文件路径及 写入/删除 沙盒文件

    出于安全考虑,iOS系统的沙盒机制规定每个应用都只能访问当前沙盒目录下面的文件(也有例外,比如系统通讯录能在用户授权的情况下被第三方应用访问),这个规则把iOS系统的封闭性展现的淋漓尽致. 一.沙盒中 ...

  6. iOS_SN_沙盒文件操作及位置

    转载:http://blog.csdn.net/hello_hwc/article/details/44916909 沙盒的结构如下所示 一 访问Bundle 注意Bundle只读,不能写入 创建一个 ...

  7. iOS开发——数据持久化Swift篇&(二)沙盒文件

    沙盒文件 //******************** 5.2 文件操作 func use_FileOperations() { //1.获取程序的Home目录 let homeDirectory = ...

  8. 关于ios项目沙盒中的文件和Xcode项目创建的文件

    //1.1获取在Xcode项目打开的情况下创建的Plist文件 NSString *path = [[NSBundle mainBundle]pathForResource:@"Profes ...

  9. iOS 开发查看应用的沙盒文件

    在iOS开发中,常常需要将一些信息保存到本地,比如说用户的一些搜索历史等.那么,如何查看所保存的文件呢? 这里介绍两种途径来查看应用的沙盒文件. 方法一:通过Xcode来查看,步骤如下: (1): X ...

随机推荐

  1. 红黑树(四)之 C++的实现

    概要 前面分别介绍红黑树的理论知识和红黑树的C语言实现.本章是红黑树的C++实现,若读者对红黑树的理论知识不熟悉,建立先学习红黑树的理论知识,再来学习本章. 目录1. 红黑树的介绍2. 红黑树的C++ ...

  2. FAQ: Machine Learning: What and How

    What: 就是将统计学算法作为理论,计算机作为工具,解决问题.statistic Algorithm. How: 如何成为菜鸟一枚? http://www.quora.com/How-can-a-b ...

  3. ReflectionToStringBuilder

    1. 使用背景 后台接口需要在接口调用的初期,记录下面查询(修改)对象的参数. 2. 使用方法 通过Apache 工具类 输出entity对象所有属性值 <dependency> < ...

  4. [Node.js] Node + Redis 实现分布式Session方案

    原文地址: http://www.moye.me/?p=565 Session是什么? Session 是面向连接的状态信息,是对 Http 无状态协议的补充. Session 怎么工作? Sessi ...

  5. Java 中文字符判断 中文标点符号判断

    Java Character 实现Unicode字符集介绍  CJK中文字符和中文标点判断 主要内容: 1. Java Character类介绍: 2. Unicode 简介及 UnicodeBloc ...

  6. CentOS6.5菜鸟之旅:安装输入法(小呀小企鹅)

    一.前言 假如在登录系统的时候语言选择了中文,那么是系统会自带ibus的中文输入法.但由于我打算用英文版,于是就被小企鹅输入法(FCITX)折腾了两个晚上. 二.检查系统编码 在bash中输入 loc ...

  7. Activating Browser Modes with Doctype

    原文地址:https://hsivonen.fi/doctype/ In order to deal both with content written according to Web standa ...

  8. IOS开发UI基础之UIScrollView

    什么是UIScrollView ● 移动设备的屏幕⼤大⼩小是极其有限的,因此直接展⽰示在⽤用户眼前的内容也相当有限 ● 当展⽰示的内容较多,超出⼀一个屏幕时,⽤用户可通过滚动⼿手势来查看屏幕以外的内容 ...

  9. Linux文件打包压缩、解压缩、备份命令使用方法(转载)

    对于刚刚接触Linux的人来说,一定会给Linux下一大堆各式各样的文件名给搞晕.别个不说,单单就压缩文件为例,我们知道在Windows下最常见的压缩文件就只有两种,一是,zip,另一个是.rar.可 ...

  10. 常用的android弹出对话框

    我们在平时做开发的时候,免不了会用到各种各样的对话框,相信有过其他平台开发经验的朋友都会知道,大部分的平台都只提供了几个最简单的实现,如果我们想实现自己特定需求的对话框,大家可能首先会想到,通过继承等 ...