IOS应用沙盒文件操作

简介:

iOS沙盒机制

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

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


复制代码
 1 //获得document文件路径,名字方便记忆  2 +(NSString *)documentsPath {
 3 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
 4 return [paths objectAtIndex:0];
 5 }
 6  7 //获取Cache目录  8 +(NSString *)cachePath {
 9 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
 10 return [paths objectAtIndex:0];
 11 }
 12 //获取Library目录  13 +(NSString *)libraryPath{
 14 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
 15 return [paths objectAtIndex:0];
 16 }
 17 //获取tmp目录  18 +(NSString *)tmpPath{
 19 NSString *tmpDir = NSTemporaryDirectory();
 20 return tmpDir;
 21 }
 22 +(NSString *)homePath{
 23 NSString *paths = NSHomeDirectory();
 24 return paths;
 25 }
 26 //获得document文件路径,名字方便记忆  27 +(NSString *) DocumentPath:(NSString *)filename {
 28 NSString *documentsPath = [self documentsPath];
 29 //NSLog(@"documentsPath=%@",documentsPath);  30 return [documentsPath stringByAppendingPathComponent:filename];
 31 }
 32  33 //====================================================================================//  34 //获得document文件路径  35 +(NSString *)fullpathOfFilename:(NSString *)filename {
 36 NSString *documentsPath = [self documentsPath];
 37 // NSLog(@"documentsPath=%@",documentsPath);  38 return [documentsPath stringByAppendingPathComponent:filename];//这里就是 documentsPath + filePath   39 }
 40  41 //写入文件沙盒位置NSDictionary  42 +(void)saveNSDictionaryForDocument:(NSDictionary *)list FileUrl:(NSString*) FileUrl {
 43 NSString *f = [self fullpathOfFilename:FileUrl];//调用了前面一个方法  44  [list writeToFile:f atomically:YES];
 45 }
 46 /*  47  * resourcePath: /Users/HeYang/Library/Developer/CoreSimulator/Devices/-
 48  * -A089E19C-0F9D-4D69-AA33-D253157E4B94/data/Containers/Bundle/Application/088B4C6E-A765-4CFE-AD9B-2F12E9E0AB28/OCUITest.app
 49 */  50 //====================================================================================//  51  52 //NSDictionary对象写入文件存放到工程位置  53 +(void)saveNSDictionaryForProduct:(NSDictionary *)list FileUrl:(NSString*) FileUrl{
 54 NSString *ProductPath =[[NSBundle mainBundle] resourcePath];
 55 NSString *f=[ProductPath stringByAppendingPathComponent:FileUrl];//这里就是 resourcePath + filePath   56  [list writeToFile:f atomically:YES];
 57 }
 58  59 //Array写入文件 -> 工程  60 +(void)saveOrderArrayListProduct:(NSMutableArray *)list FileUrl:(NSString*)FileUrl {
 61 NSString *ProductPath =[[NSBundle mainBundle] resourcePath];
 62 NSString *f=[ProductPath stringByAppendingPathComponent:FileUrl];
 63  [list writeToFile:f atomically:YES];
 64 }
 65 //Array对象 写入文件 -> 沙盒  66 +(void)saveOrderArrayList:(NSMutableArray *)list FileUrl:(NSString*) FileUrl{
 67 NSString *f = [self fullpathOfFilename:FileUrl];
 68  [list writeToFile:f atomically:YES];
 69 }
 70 //加载文件沙盒的文件 ->NSDictionary  71 +(NSDictionary *)loadNSDictionaryForDocument:(NSString*)FileUrl {
 72 NSString *f = [self fullpathOfFilename:FileUrl];
 73 NSDictionary *list = [[NSDictionary alloc] initWithContentsOfFile:f];
 74 return [list autorelease];
 75 }
 76 //加载文件工程位置的文件 ->NSDictionary  77 +(NSDictionary *)loadNSDictionaryForProduct:(NSString*)FileUrl {
 78 NSString *f = [self ProductPath:FileUrl];
 79 NSDictionary *list =[NSDictionary dictionaryWithContentsOfFile:f];
 80 return list;
 81 }
 82 //加载文件沙盒的文件 -> NSArray  83 +(NSArray *)loadArrayList:(NSString*) FileUrl {
 84 NSString *f = [self fullpathOfFilename:FileUrl];
 85 NSArray *list = [NSArray arrayWithContentsOfFile:f];
 86 return list;
 87 }
 88 //加载文件工程位置的文件 -> NSArray  89 +(NSArray *)loadArrayListProduct:(NSString*) FileUrl {
 90 NSString *f = [self ProductPath:FileUrl];
 91 NSArray *list = [NSArray arrayWithContentsOfFile:f];
 92 return list;
 93 }
 94  95 //====================================================================================//  96 //获得document文件路径  97 +(NSString *)fullpathOfFilename:(NSString *)filename {
 98 NSString *documentsPath = [self documentsPath];
 99 // NSLog(@"documentsPath=%@",documentsPath); 100 return [documentsPath stringByAppendingPathComponent:filename];//这里就是 documentsPath + filePath  101 }
102 //拷贝文件到沙盒 103 +(int) CopyFileToDocument:(NSString*)FileName{
104 NSString *appFileName =[self fullpathOfFilename:FileName];
105 NSFileManager *fm = [NSFileManagerdefaultManager];
106 //判断沙盒下是否存在  107 BOOL isExist = [fm fileExistsAtPath:appFileName];
108 if (!isExist) //不存在,把工程的文件复制document目录下 109  {
110 //获取工程中文件 111 NSString *backupDbPath = [[NSBundle mainBundle] 
112  pathForResource:FileName 
113 ofType:@""];
114 //这一步实现数据库的添加, 
115 // 通过NSFileManager 对象的复制属性,把工程中数据库的路径复制到应用程序的路径上  116 BOOL cp = [fm copyItemAtPath:backupDbPath toPath:appFileName error:nil];
117 return cp;
118 } else {
119 return -1; //已经存在 120  }
121 }
122 //====================================================================================// 123 124 //判断文件是否存在 125 +(BOOL) FileIsExists:(NSString*)checkFile{
126 if([[NSFileManager defaultManager] fileExistsAtPath:checkFile])
127  {
128 return true;
129  }
130 return false;
131 }
132 133 //读取工程文件 134 +(NSString *) ProductPath:(NSString*)filename{
135 NSString *path = [[NSBundle mainBundle] pathForResource:filename ofType:@""];
136 return path;
137 }
复制代码

打开模拟器沙盒目录

下面看看模拟器的沙盒文件夹在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这里写你的用户名。


相关文章
|
2月前
|
iOS开发 开发者
苹果iOS App Store上架操作流程详解:从开发者账号到应用发布
很多开发者在开发完iOS APP、进行内测后,下一步就面临上架App Store,不过也有很多同学对APP上架App Store的流程不太了解,下面我们来说一下iOS APP上架App Store的具体流程,如有未涉及到的部分,大家可以及时咨询,共同探讨。
|
2月前
|
开发者 iOS开发
iOS应用上架详细图文教程(上)
App Store作为苹果官方的应用商店,审核严格周期长一直让用户头疼不已,很多app都“死”在了审核这一关,那我们就要放弃iOS用户了吗?当然不是!本期我们从iOS app上架流程开始梳理,详细了解下iOS app上架的那些事。
|
2月前
|
Swift iOS开发 开发者
iOS 应用上架流程详解
iOS 应用上架流程详解
|
27天前
|
安全 数据安全/隐私保护 虚拟化
iOS应用加固方案解析:ipa加固安全技术全面评测
iOS应用加固方案解析:ipa加固安全技术全面评测
36 3
|
1月前
|
运维 监控 安全
应用研发平台EMAS常见问题之sophix ios flutter热更新如何解决
应用研发平台EMAS(Enterprise Mobile Application Service)是阿里云提供的一个全栈移动应用开发平台,集成了应用开发、测试、部署、监控和运营服务;本合集旨在总结EMAS产品在应用开发和运维过程中的常见问题及解决方案,助力开发者和企业高效解决技术难题,加速移动应用的上线和稳定运行。
77 0
|
1月前
|
Web App开发 前端开发 网络安全
前端分析工具之 Charles 录制 Android/IOS 手机的 https 应用
【2月更文挑战第21天】前端分析工具之 Charles 录制 Android/IOS 手机的 https 应用
47 1
前端分析工具之 Charles 录制 Android/IOS 手机的 https 应用
|
2月前
|
Linux Android开发 iOS开发
iOS 应用上架的步骤和工具简介
APP开发助手是一款能够辅助iOS APP上架到App Store的工具,它解决了iOS APP上架流程繁琐且耗时的问题,帮助跨平台APP开发者顺利将应用上架到苹果应用商店。最重要的是,即使没有配置Mac苹果机,也可以使用该工具完成一系列操作,包括iOS证书申请、创建iOS开发者证书和 iOS发布证书等各类证书。此外,在Windows、Linux或Mac系统中上传IPA到App Store也变得简单快捷,从而大大简化了iOS APP上架的流程。
|
2月前
|
移动开发 前端开发 安全
保护你的 iOS 应用,防止逆向破解
保护你的 iOS 应用,防止逆向破解
|
2月前
|
存储 安全 数据安全/隐私保护
iOS应用上架详细图文教程(下)
我们这边介绍一个简便的证书制作小方法。
|
2月前
|
安全 算法 数据安全/隐私保护
iOS 代码加固与保护方法详解 - 提升 iOS 应用安全性的关键步骤
iOS 代码加固与保护方法详解 - 提升 iOS 应用安全性的关键步骤