IOS沙盒详解

2024-06-04 06:58
文章标签 详解 ios 沙盒

本文主要是介绍IOS沙盒详解,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

例子代码:https://github.com/schelling/YcDemo

升级方案:IOS软件在APP STORE上升级的时候,只需把配置文件放在Document目录下,就可以。其他文件会随着版本的更新被替换掉。

IOS沙盒(sandbox)机制和文件操作(一)


1、IOS沙盒机制

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

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

 

 

2、打开模拟器沙盒目录

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

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

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

<p style="padding-top: 0px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 15px;"><span style="padding: 0px; margin: 0px;">显示Mac隐藏文件的命令:defaults write com.apple.finder AppleShowAllFiles -bool true</span></p>
<p style="padding-top: 0px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 15px;"><span style="padding: 0px; margin: 0px;">隐藏Mac隐藏文件的命令:defaults write com.apple.finder AppleShowAllFiles -bool false</span></p>
<p style="padding-top: 0px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 15px;"><span style="padding: 0px; margin: 0px;">输完单击Enter键,退出终端,重新启动Finder就可以了</span><span style="padding: 0px; margin: 0px;"> 重启Finder:鼠标单击窗口左上角的苹果标志-->强制退出-->Finder--></span></p>
现在能看到资源库文件夹了。

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

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

username这里写你的用户名。

3、目录结构

默认情况下,每个沙盒含有3个文件夹:Documents, Library 和 tmp。因为应用的沙盒机制,应用只能在几个目录下读写文件
Documents:苹果建议将程序中建立的或在程序中浏览到的文件数据保存在该目录下,iTunes备份和恢复的时候会包括此目录
Library:存储程序的默认设置或其它状态信息;

Library/Caches:存放缓存文件,iTunes不会备份此目录,此目录下文件不会在应用退出删除

tmp:提供一个即时创建临时文件的地方。

 

iTunes在与iPhone同步时,备份所有的Documents和Library文件。

iPhone在重启时,会丢弃所有的tmp文件。

 

我们创建一个IosSandbox的项目来展开沙盒和文件读写等操作的练习。

创建后找到模拟器上对应的目录,

这是目录全展开了。

  

这是上面提到的三个目录 :Documents、Library、 tmp


1、获取程序的Home目录

[cpp]  view plain  copy
  1. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
  2. NSString *path = [paths objectAtIndex:0];  
  3. NSLog(@"path:%@", path);  
打印结果: 
[cpp]  view plain  copy
  1. 2012-06-17 14:00:06.098 IosSandbox[3536:f803] /Users/rongfzh/Library/Application Support/iPhone Simulator/5.1/Applications/3B8EC78A-5EEE-4C2F-B0CB-4C3F02B996D2  

那在真机上的目录有是怎么样的呢?我们看看

2012-06-17 14:25:47.059 IosSandbox[4281:f803] /var/mobile/Applications/3B8EC78A-5EEE-4C2F-B0CB-4C3F02B996D2

可见,真机上的目录是/var/mobile/Applications/这个目录下的,和模拟器不一样。这个是Home目录,其他的子目录和模拟器一样。

 

2、获取document目录
[cpp]  view plain  copy
  1. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
  2. NSString *path = [paths objectAtIndex:0];  
  3. NSLog(@"path:%@", path);  
打印结果
[cpp]  view plain  copy
  1. 2012-06-17 14:00:06.099 IosSandbox[3536:f803] path:/Users/rongfzh/Library/Application Support/iPhone Simulator/5.1/Applications/3B8EC78A-5EEE-4C2F-B0CB-4C3F02B996D2/Documents  
3、获取Cache目录
[cpp]  view plain  copy
  1. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);  
  2. NSString *path = [paths objectAtIndex:0];  
  3. NSLog(@"%@", path);  
打印结果 
[cpp]  view plain  copy
  1. 2012-06-17 14:03:50.431 IosSandbox[3628:f803] /Users/rongfzh/Library/Application Support/iPhone Simulator/5.1/Applications/3B8EC78A-5EEE-4C2F-B0CB-4C3F02B996D2/Library/Caches  

4、获取Library目录
[cpp]  view plain  copy
  1. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);  
  2. NSString *path = [paths objectAtIndex:0];  
  3. NSLog(@"%@", path);  
打印结果 
[cpp]  view plain  copy
  1. 2012-06-17 14:07:17.544 IosSandbox[3733:f803] /Users/rongfzh/Library/Application Support/iPhone Simulator/5.1/Applications/3B8EC78A-5EEE-4C2F-B0CB-4C3F02B996D2/Library  
5、获取Tmp目录
[cpp]  view plain  copy
  1. NSString *tmpDir = NSTemporaryDirectory();  
  2.  NSLog(@"%@", tmpDir);  
打印结果
[cpp]  view plain  copy
  1. 2012-06-17 14:08:07.824 IosSandbox[3782:f803] /var/folders/g7/246bh79130zblw0yjjtc55cw0000gn/T/  
6、写入文件
[cpp]  view plain  copy
  1. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
  2.     NSString *docDir = [paths objectAtIndex:0];  
  3.     if (!docDir) {  
  4.         NSLog(@"Documents 目录未找到");          
  5.     }  
  6.     NSArray *array = [[NSArray alloc] initWithObjects:@"内容",@"content",nil];  
  7.     NSString *filePath = [docDir stringByAppendingPathComponent:@"testFile.txt"];  
  8.     [array writeToFile:filePath atomically:YES];  

注:我们在真机上也运行一下,把文件写入,下一步从真机上把内容读取出来。

写入输入 array ,里面是两个字符串,一会我们读出来打印。

写入我们在程序沙盒目录下看到文件 testFile.txt

 

打开文件看到的内容是这样的,是个xml格式的plist文件,数据格式保存了内容。

[cpp]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">  
  3. <plist version="1.0">  
  4. <array>  
  5.     <string>内容</string>  
  6.     <string>content</string>  
  7. </array>  
  8. </plist>  
7、读取文件
[cpp]  view plain  copy
  1. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
  2.     NSString *docDir = [paths objectAtIndex:0];  
  3.     NSString *filePath = [docDir stringByAppendingPathComponent:@"testFile.txt"];  
  4.     NSArray *array = [[NSArray alloc]initWithContentsOfFile:filePath];  
  5.     NSLog(@"%@", array);  
打印结果:

把上面的文件解析后,把内容打印出来了。

[cpp]  view plain  copy
  1. 2012-06-17 14:14:46.249 IosSandbox[3918:f803] (  
  2.     "\U5185\U5bb9",  
  3.     content  
  4. )  

真机上读取并打印文件路径:

2012-06-17 14:25:47.059 IosSandbox[4281:f803] /var/mobile/Applications/3B8EC78A-5EEE-4C2F-B0CB-4C3F02B996D2/Documents/testFile.txt

 (

    "\U5185\U5bb9",

    content

)

真机上也能写入和打印。

 IOS沙盒(sandbox)机制和文件操作(三)

0人收藏此文章, 我要收藏发表于27天前(2012-07-14 21:27) , 已有 271次阅读 共 0个评论

我们看看NSFileManager如何使用。包括创建文件,目录,删除,遍历目录等。

 

1、在Documents里创建目录

创建一个叫test的目录,先找到Documents的目录,

[cpp]  view plain  copy
  1. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);    
  2.    NSString *documentsDirectory = [paths objectAtIndex:0];    
  3.    NSLog(@"documentsDirectory%@",documentsDirectory);    
  4.    NSFileManager *fileManager = [NSFileManager defaultManager];    
  5.    NSString *testDirectory = [documentsDirectory stringByAppendingPathComponent:@"test"];    
  6.    // 创建目录  
  7.    [fileManager createDirectoryAtPath:testDirectory withIntermediateDirectories:YES attributes:nil error:nil];  

启动程序,这时候目录就创建了:

2、在test目录下创建文件

创建文件怎么办呢?接着上面的代码 testPath 要用stringByAppendingPathComponent拼接上你要生成的文件名,比如test00.txt。这样才能在test下写入文件。

testDirectory是上面代码生成的路径哦,不要忘了。我往test文件夹里写入三个文件,test00.txt ,test22.txt,text.33.txt。内容都是写入内容,write String。

实现代码如下:

[cpp]  view plain  copy
  1. NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test00.txt"];    
  2. NSString *testPath2 = [testDirectory stringByAppendingPathComponent:@"test22.txt"];    
  3. NSString *testPath3 = [testDirectory stringByAppendingPathComponent:@"test33.txt"];    
  4.   
  5.   
  6. NSString *string = @"写入内容,write String";  
  7. [fileManager createFileAtPath:testPath contents:[string  dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];  
  8. [fileManager createFileAtPath:testPath2 contents:[string  dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];  
  9. [fileManager createFileAtPath:testPath3 contents:[string  dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];  
看下面的图,三个文件都出来了,内容也对。

在Documents目录下创建就更简单了,不用加test就ok了

3、获取目录列里所有文件名

两种方法获取:subpathsOfDirectoryAtPath 和 subpathsAtPath

[cpp]  view plain  copy
  1. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);    
  2. NSString *documentsDirectory = [paths objectAtIndex:0];    
  3. NSLog(@"documentsDirectory%@",documentsDirectory);    
  4. NSFileManager *fileManage = [NSFileManager defaultManager];    
  5. NSString *myDirectory = [documentsDirectory stringByAppendingPathComponent:@"test"];    
  6. NSArray *file = [fileManage subpathsOfDirectoryAtPath: myDirectory error:nil];   
  7. NSLog(@"%@",file);    
  8. NSArray *files = [fileManage subpathsAtPath: myDirectory ];   
  9. NSLog(@"%@",files);  

获取上面刚才test文件夹里的文件名

打印结果

2012-06-17 23:23:19.684 IosSandbox[947:f803] fileList:(

    ".DS_Store",

    "test00.txt",

    "test22.txt",

    "test33.txt"

)

2012-06-17 23:23:19.686 IosSandbox[947:f803] fileLit(

    ".DS_Store",

    "test00.txt",

    "test22.txt",

    "test33.txt"

)

两个方法都可以,隐藏的文件也打印出来了。

4、fileManager使用操作当前目录

[cpp]  view plain  copy
  1. //创建文件管理器  
  2.     NSFileManager *fileManager = [NSFileManager defaultManager];  
  3.     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
  4.     NSString *documentsDirectory = [paths objectAtIndex:0];  
  5.     //更改到待操作的目录下  
  6.     [fileManager changeCurrentDirectoryPath:[documentsDirectory stringByExpandingTildeInPath]];  
  7.     //创建文件fileName文件名称,contents文件的内容,如果开始没有内容可以设置为nil,attributes文件的属性,初始为nil  
  8.     NSString * fileName = @"testFileNSFileManager.txt";  
  9.     NSArray *array = [[NSArray alloc] initWithObjects:@"hello world",@"hello world1", @"hello world2",nil];  
  10.     [fileManager createFileAtPath:fileName contents:array attributes:nil];  
这样就创建了testFileNSFileManager.txt并把三个hello world写入文件了

changeCurrentDirectoryPath目录更改到当前操作目录时,做文件读写就很方便了,不用加上全路径

5、删除文件

接上面的代码,remove就ok了。

[cpp]  view plain  copy
  1. [fileManager removeItemAtPath:fileName error:nil];  
6、混合数据的读写

用NSMutableData创建混合数据,然后写到文件里。并按数据的类型把数据读出来

6.1写入数据:
[cpp]  view plain  copy
  1. NSString * fileName = @"testFileNSFileManager.txt";  
  2. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
  3. NSString *documentsDirectory = [paths objectAtIndex:0];  
  4. //获取文件路径  
  5. NSString *path = [documentsDirectory stringByAppendingPathComponent:fileName];  
  6. //待写入的数据  
  7. NSString *temp = @"nihao 世界";  
  8. int dataInt = 1234;  
  9. float dataFloat = 3.14f;  
  10. //创建数据缓冲  
  11. NSMutableData *writer = [[NSMutableData alloc] init];  
  12. //将字符串添加到缓冲中  
  13. [writer appendData:[temp dataUsingEncoding:NSUTF8StringEncoding]];     
  14. //将其他数据添加到缓冲中  
  15. [writer appendBytes:&dataInt length:sizeof(dataInt)];  
  16. [writer appendBytes:&dataFloat length:sizeof(dataFloat)];    
  17. //将缓冲的数据写入到文件中  
  18. [writer writeToFile:path atomically:YES];  

我们看看数据怎么样了:


我们看到后面的是乱码,那是中文被转成了NSData后,还有int float的二进制

6.2读取刚才写入的数据:

[cpp]  view plain  copy
  1. //读取数据:  
  2.    int intData;  
  3.    float floatData = 0.0;  
  4.    NSString *stringData;  
  5.      
  6.    NSData *reader = [NSData dataWithContentsOfFile:path];  
  7.    stringData = [[NSString alloc] initWithData:[reader subdataWithRange:NSMakeRange(0, [temp length])]  
  8.                                   encoding:NSUTF8StringEncoding];  
  9.    [reader getBytes:&intData range:NSMakeRange([temp length], sizeof(intData))];  
  10.    [reader getBytes:&floatData range:NSMakeRange([temp length] + sizeof(intData), sizeof(floatData))];  
  11.    NSLog(@"stringData:%@ intData:%d floatData:%f", stringData, intData, floatData);  

打印出来的结果: 

2012-06-17 23:51:14.723 IosSandbox[1285:f803] stringData:nihao hello! intData:1234332 floatData:3.140000

这里把写入的汉字改成了 hello。因为[temp  length]算长度是,把中文算成一位了,出来的结果有误。

这篇关于IOS沙盒详解的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1029383

相关文章

一文详解如何在idea中快速搭建一个Spring Boot项目

《一文详解如何在idea中快速搭建一个SpringBoot项目》IntelliJIDEA作为Java开发者的‌首选IDE‌,深度集成SpringBoot支持,可一键生成项目骨架、智能配置依赖,这篇文... 目录前言1、创建项目名称2、勾选需要的依赖3、在setting中检查maven4、编写数据源5、开启热

Python常用命令提示符使用方法详解

《Python常用命令提示符使用方法详解》在学习python的过程中,我们需要用到命令提示符(CMD)进行环境的配置,:本文主要介绍Python常用命令提示符使用方法的相关资料,文中通过代码介绍的... 目录一、python环境基础命令【Windows】1、检查Python是否安装2、 查看Python的安

HTML5 搜索框Search Box详解

《HTML5搜索框SearchBox详解》HTML5的搜索框是一个强大的工具,能够有效提升用户体验,通过结合自动补全功能和适当的样式,可以创建出既美观又实用的搜索界面,这篇文章给大家介绍HTML5... html5 搜索框(Search Box)详解搜索框是一个用于输入查询内容的控件,通常用于网站或应用程

Python中使用uv创建环境及原理举例详解

《Python中使用uv创建环境及原理举例详解》uv是Astral团队开发的高性能Python工具,整合包管理、虚拟环境、Python版本控制等功能,:本文主要介绍Python中使用uv创建环境及... 目录一、uv工具简介核心特点:二、安装uv1. 通过pip安装2. 通过脚本安装验证安装:配置镜像源(可

C++ 函数 strftime 和时间格式示例详解

《C++函数strftime和时间格式示例详解》strftime是C/C++标准库中用于格式化日期和时间的函数,定义在ctime头文件中,它将tm结构体中的时间信息转换为指定格式的字符串,是处理... 目录C++ 函数 strftipythonme 详解一、函数原型二、功能描述三、格式字符串说明四、返回值五

LiteFlow轻量级工作流引擎使用示例详解

《LiteFlow轻量级工作流引擎使用示例详解》:本文主要介绍LiteFlow是一个灵活、简洁且轻量的工作流引擎,适合用于中小型项目和微服务架构中的流程编排,本文给大家介绍LiteFlow轻量级工... 目录1. LiteFlow 主要特点2. 工作流定义方式3. LiteFlow 流程示例4. LiteF

CSS3中的字体及相关属性详解

《CSS3中的字体及相关属性详解》:本文主要介绍了CSS3中的字体及相关属性,详细内容请阅读本文,希望能对你有所帮助... 字体网页字体的三个来源:用户机器上安装的字体,放心使用。保存在第三方网站上的字体,例如Typekit和Google,可以link标签链接到你的页面上。保存在你自己Web服务器上的字

MySQL存储过程之循环遍历查询的结果集详解

《MySQL存储过程之循环遍历查询的结果集详解》:本文主要介绍MySQL存储过程之循环遍历查询的结果集,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录前言1. 表结构2. 存储过程3. 关于存储过程的SQL补充总结前言近来碰到这样一个问题:在生产上导入的数据发现

MyBatis ResultMap 的基本用法示例详解

《MyBatisResultMap的基本用法示例详解》在MyBatis中,resultMap用于定义数据库查询结果到Java对象属性的映射关系,本文给大家介绍MyBatisResultMap的基本... 目录MyBATis 中的 resultMap1. resultMap 的基本语法2. 简单的 resul

从基础到进阶详解Pandas时间数据处理指南

《从基础到进阶详解Pandas时间数据处理指南》Pandas构建了完整的时间数据处理生态,核心由四个基础类构成,Timestamp,DatetimeIndex,Period和Timedelta,下面我... 目录1. 时间数据类型与基础操作1.1 核心时间对象体系1.2 时间数据生成技巧2. 时间索引与数据