ios 模拟器沙盒_举例详解iOS开发过程中的沙盒机制与文件
iOS沙盒机制
iOS应用程序只能在为该改程序创建的文件系统中读取文件,不可以去其它地方访问,此区域被成为沙盒,所以所有的非代码文件都要保存在此,例如图像,图标,声音,映像,属性列表,文本文件等。
每个应用程序都有自己的存储空间
应用程序不能翻过自己的围墙去访问别的存储空间的内容
打开模拟器沙盒目录
方法1、可以设置显示隐藏文件,然后在Finder下直接打开。设置查看隐藏文件的方法如下:打开终端,输入命名
显示Mac隐藏文件的命令:
defaults write com.apple.finder AppleShowAllFiles -bool true
隐藏Mac隐藏文件的命令:
defaults write com.apple.finder AppleShowAllFiles -bool false
现在能看到资源库文件夹了。
打开资源库后找到/Application Support/iPhone Simulator/文件夹。这里面就是模拟器的各个程序的沙盒目录了。
方法2、这种方法更方便,在Finder上点->前往 然后按住"option"键,就会出现"资源库",其他同上
目录结构
默认情况下,每个沙盒含有3个文件夹:Documents, Library 和 tmp。因为应用的沙盒机制,应用只能在几个目录下读写文件
Documents:苹果建议将程序中建立的或在程序中浏览到的文件数据保存在该目录下,iTunes备份和恢复的时候会包括此目录
Library:存储程序的默认设置或其它状态信息;
Library/Caches:存放缓存文件,iTunes不会备份此目录,此目录下文件不会在应用退出删除
tmp:提供一个即时创建临时文件的地方。
iTunes在与iPhone同步时,备份所有的Documents和Library文件。
iPhone在重启时,会丢弃所有的tmp文件。
这是上面提到的三个目录 :Documents、Library、 tmp
几个常用的代码示例:
1、获取程序的Home目录
NSString *homeDirectory = NSHomeDirectory();
NSLog(@"path:%@", homeDirectory);
2、获取document目录
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [paths objectAtIndex:0];
NSLog(@"path:%@", path);
3、获取Cache目录
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *path = [paths objectAtIndex:0];
NSLog(@"%@", path);
4、获取Library目录
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *path = [paths objectAtIndex:0];
NSLog(@"%@", path);
5、获取Tmp目录
NSString *tmpDir = NSTemporaryDirectory();
NSLog(@"%@", tmpDir);
6、写入文件
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDir = [paths objectAtIndex:0];
if (!docDir) {
NSLog(@"Documents 目录未找到");
}
NSArray *array = [[NSArray alloc] initWithObjects:@"内容",@"content",nil];
NSString *filePath = [docDir stringByAppendingPathComponent:@"testFile.txt"];
[array writeToFile:filePath atomically:YES];
7、写入文件
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDir = [paths objectAtIndex:0];
NSString *filePath = [docDir stringByAppendingPathComponent:@"testFile.txt"];
NSArray *array = [[NSArray alloc]initWithContentsOfFile:filePath];
NSLog(@"%@", array);
8、判断一个文件是否存在,传入全路径(fileExistsAtPath)
// 创建文件管理器
NSFileManager * fileManager = [NSFileManager defaultManager];
NSString * documents = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
NSString * filePath = [documents stringByAppendingPathComponent:@"test"];
// 判断一个文件是否存在,传入全路径
if ([fileManager fileExistsAtPath:filePath]) {
NSLog(@"it is exit");
}
9、在Documents里创建目录
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSLog(@"documentsDirectory%@",documentsDirectory);
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *testDirectory = [documentsDirectory stringByAppendingPathComponent:@"test"];
// 创建目录
[fileManager createDirectoryAtPath:testDirectory withIntermediateDirectories:YES attributes:nil error:nil];
10、在目录下创建文件
NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test00.txt"];
NSString *testPath2 = [testDirectory stringByAppendingPathComponent:@"test22.txt"];
NSString *testPath3 = [testDirectory stringByAppendingPathComponent:@"test33.txt"];
NSString *string = @"写入内容,write String";
[fileManager createFileAtPath:testPath contents:[string dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
[fileManager createFileAtPath:testPath2 contents:[string dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
[fileManager createFileAtPath:testPath3 contents:[string dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
11、获取目录列里所有文件名
两种方法获取:subpathsOfDirectoryAtPath 和subpathsAtPath
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSLog(@"documentsDirectory%@",documentsDirectory);
NSFileManager *fileManage = [NSFileManager defaultManager];
NSString *myDirectory = [documentsDirectory stringByAppendingPathComponent:@"test"];
NSArray *file = [fileManage subpathsOfDirectoryAtPath: myDirectory error:nil];
NSLog(@"%@",file);
NSArray *files = [fileManage subpathsAtPath: myDirectory ];
NSLog(@"%@",files);
12、fileManager使用操作当前目录
//创建文件管理器
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//更改到待操作的目录下
[fileManager changeCurrentDirectoryPath:[documentsDirectory stringByExpandingTildeInPath]];
//创建文件fileName文件名称,contents文件的内容,如果开始没有内容可以设置为nil,attributes文件的属性,初始为nil
NSString * fileName = @"testFileNSFileManager.txt";
NSArray *array = [[NSArray alloc] initWithObjects:@"hello world",@"hello world1", @"hello world2",nil];
[fileManager createFileAtPath:fileName contents:array attributes:nil];
13、删除文件
[fileManager removeItemAtPath:fileName error:nil];
ios 模拟器沙盒_举例详解iOS开发过程中的沙盒机制与文件相关推荐
- java web几百万分页_举例详解用Java实现web分页功能的方法
举例详解用Java实现web分页功能的方法 发布于 2020-11-25| 复制链接 摘记: 分页问题是一个非常普遍的问题,开发者几乎都会遇到,这里不讨论具体如何分页,说明一下Web方式下分页的原理. ...
- python 中split函数的应用_举例详解Python中的split()函数的使用方法
函数:split() python中有split()和os.path.split()两个函数,具体作用如下: split():拆分字符串.通过指定分隔符对字符串进行切片,并返回分割后的字符串列表(li ...
- python中rstrip是什么意思_【详解】python中字符串的strip(),lstrip(),rstrip()的含义...
[解答] 主要是: string. lstrip ( s[, chars] ) Return a copy of the string with leading characters removed. ...
- find linux 目录深度_浪里淘沙,详解Linux系统中Find命令的实用技巧
知了小巷:浪里淘沙,详解Linux系统中Find命令的实用技巧. 啊哈,找到了! 当我们需要在Linux系统上定位某个文件或目录时,find命令通常是必备之选. 它使用起来非常简单,但有许多不同的可选 ...
- iOS教程:详解iOS多图下载的缓存机制
ios教程,ios的干货一直来不及给大家分享,小编也是一直在忙啊!今天给大家献上ios:详解iOS多图下载的缓存机制 1. 需求点是什么? 这里所说的多图下载,就是要在tableview的每一个cel ...
- 程序阅读_全面详解LTE:MATLAB建模仿真与实现_自学笔记(1)调制与编码_程序阅读
程序阅读_全面详解LTE:MATLAB建模仿真与实现_自学笔记(1)调制与编码_程序阅读 在粗浅地掌握了LTE知识后,从今天开始对<全面详解LTE:MATLAB建模仿真与实现>一书的学习. ...
- 深拷贝与浅拷贝(mutableCopy与Copy)详解 iOS
深拷贝与浅拷贝(mutableCopy与Copy)详解 iOS ios中并不是所有的对象都支持copy,mutableCopy,遵守NSCopying 协议的类可以发送copy消息,遵守NSMutab ...
- Objective-C ,ios,iphone开发基础:UIAlertView使用详解
UIAlertView使用详解 Ios中为我们提供了一个用来弹出提示框的类 UIAlertView,他类似于javascript中的alert 和c#中的MessageBox(); UIAlertVi ...
- 李洪强iOS经典面试题156 - Runtime详解(面试必备)
李洪强iOS经典面试题156 - Runtime详解(面试必备) 一.runtime简介 RunTime简称运行时.OC就是运行时机制,也就是在运行时候的一些机制,其中最主要的是消息机制. 对于C ...
最新文章
- 送书 | 2020年新一天,用这本书开启你的NLP学习之路!
- SAP QM 执行事务代码QS23为检验特性分配Selected Set的时候报错 - You cannot use entries from catalogs 1 and 3-
- bps计算机,bps指的是计算机的什么
- git每次操作提示输入密码问题解决
- C# 校验帮助类-正则表达式
- VTK:网格之WindowedSincPolyDataFilter
- 怎样将1900-01-00变成-_《转生成蜘蛛又怎样》第2集预告:人类到来,蜘蛛子遭遇大危机...
- Unity之流光效果
- 所有程序员都应该遵守的11条规则
- SpringCloud 从菜鸟到大牛之四 应用通信 Feign Ribbon
- 使用Fsharp 探索 Dotnet 平台
- 二叉树的遍历实验报告C语言,二叉树的建立与遍历实验报告(c语言编写,附源代码)...
- CCF201809-2 买菜(100分)【序列处理+差分】
- C++ tbb::atomic
- Perl入门学习(一)运行和基本语法
- 基于matlab 的电力系统潮流仿真
- 将GeoIP的region_code列表也复制过来一份
- Android mc怎么和win10联机,大更新!我的世界手机版/win10版联机完美互通
- 苹果7p更新系统老是服务器出错,7p为什么无法更新系统 7p无法更新系统怎么办...
- 物联网时代的智慧燃气解决方案
热门文章
- MediaSession分析
- 计算机网络网上花店毕业论文,网上花店(毕业论文).doc
- TCP/IP 三次握手超详细总结
- outlook服务器信息查看器,允许最终用户在 Outlook Web App 中查看 POP3、IMAP4 和 SMTP 服务器设置...
- python你已经是个成熟的软件了_你们已经是成熟的软件了表情包下载
- NASA‘s Eyes软件介绍与使用教程
- realsense R200转成costmap_2d
- 我听很多人说JAVA已经过时了,下一个要淘汰的语言就是JAVA,真的是这样吗?
- 复选框、单行文本框、组合框
- DVD转换MPEG-4三部曲