iOS sandbox
iOS的沙盒机制,应用只能访问自己应用目录下的文件。iOS不像android,没有SD卡概念,不能直接访问图像、视频等内容。iOS应用产生的内容,如图像、文件、缓存内容等都必须存储在自己的沙盒内。默认情况下,每个沙盒含有3个文件夹:Documents, Library 和 tmp。Library包含Caches、Preferences目录。
上面的完整路径为:用户->资源库->Application Support->iPhone Simulator->6.1->Aplications
Documents:苹果建议将程序创建产生的文件以及应用浏览产生的文件数据保存在该目录下,iTunes备份和恢复的时候会包括此目录
Library:存储程序的默认设置或其它状态信息;
Library/Caches:存放缓存文件,保存应用的持久化数据,用于应用升级或者应用关闭后的数据保存,不会被itunes同步,所以为了减少同步的时间,可以考虑将一些比较大的文件而又不需要备份的文件放到这个目录下。
tmp:提供一个即时创建临时文件的地方,但不需要持久化,在应用关闭后,该目录下的数据将删除,也可能系统在程序不运行的时候清除。
APP Sandbox
iOS怎么获取沙盒路径,怎么操作文件呢?下面给出答案。
获取应用沙盒根路径:
- -(void)dirHome{
- NSString *dirHome=NSHomeDirectory();
- NSLog(@"app_home: %@",dirHome);
- }
获取Documents目录路径:
- //获取Documents目录
- -(NSString *)dirDoc{
- //[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
- NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
- NSString *documentsDirectory = [paths objectAtIndex:0];
- NSLog(@"app_home_doc: %@",documentsDirectory);
- return documentsDirectory;
- }
获取Library目录路径:
- //获取Library目录
- -(void)dirLib{
- //[NSHomeDirectory() stringByAppendingPathComponent:@"Library"];
- NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
- NSString *libraryDirectory = [paths objectAtIndex:0];
- NSLog(@"app_home_lib: %@",libraryDirectory);
- }
获取Cache目录路径:
- //获取Cache目录
- -(void)dirCache{
- NSArray *cacPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
- NSString *cachePath = [cacPath objectAtIndex:0];
- NSLog(@"app_home_lib_cache: %@",cachePath);
- }
获取Tmp目录路径:
- //获取Tmp目录
- -(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 stringByAppendingPathComponent:@"test"];
- // 创建目录
- BOOL res=[fileManager createDirectoryAtPath:testDirectory withIntermediateDirectories:YES attributes:nil error:nil];
- if (res) {
- NSLog(@"文件夹创建成功");
- }else
- NSLog(@"文件夹创建失败");
- }
创建文件
- //创建文件
- -(void *)createFile{
- NSString *documentsPath =[self dirDoc];
- NSString *testDirectory = [documentsPath 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 *content=@"测试写入内容!";
- BOOL res=[content writeToFile:testPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
- if (res) {
- NSLog(@"文件写入成功");
- }else
- NSLog(@"文件写入失败");
- }
读文件数据:
- //读文件
- -(void)readFile{
- 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]?@"YES":@"NO");
- }
iOS sandbox的更多相关文章
- IOS 学习之 iOS沙盒(sandbox) 介绍 沙盒机制 文件操作(一)
1.iOS沙盒机制 iOS应用程序只能在为该改程序创建的文件系统中读取文件,不可以去其它地方访问,此区域被成为沙盒,所以所有的非代码文件都要保存在此,例如图像,图标,声音,映像,属性列表,文本文件等. ...
- iOS 沙盒(sandbox)结构 使用 实例
声明:该文档是经过自己查找网上的资料以及自己多年的经验后而总结出来的,希望对大家有所帮助,有什么不恰当支出还请大家多指点! iOS中的沙盒机制(SandBox)是一种安全体系,它规定了应用程序只能在为 ...
- iOS 沙盒(sandbox)机制和文件操作
本文参看了 http://www.uml.org.cn/mobiledev/201209211.asp#1 这篇文章中的介绍,尊重原著. 1.IOS沙盒机制 IOS应用程序只能在本应用程序中创建的文件 ...
- iOS的SandBox的结构研究
在模拟器中运行iOS程序,都会为该程序创建一个沙盒(SandBox).首先声明,我用的系统是Max OS X 10.7.3,编译器是Xcode 4.3.2.想要找到沙盒目录,先运行Finder,然后在 ...
- IOS学习之IOS沙盒(sandbox)机制和文件操作
IOS学习之IOS沙盒(sandbox)机制和文件操作(一) 1.IOS沙盒机制 IOS应用程序只能在为该改程序创建的文件系统中读取文件,不可以去其它地方访问,此区域被成为沙盒,所以所有的非代码文件都 ...
- 【iOS开发必收藏】详解iOS应用程序内使用IAP/StoreKit付费、沙盒(SandBox)测试、创建测试账号流程!【2012-12-11日更新获取”产品付费数量等于0的问题”】
转的别人的 看到很多童鞋问到,为什么每次都返回数量等于0?? 其实有童鞋已经找到原因了,原因是你在 ItunesConnect 里的 “Contracts, Tax, and Banking”没有完成 ...
- ios专题 - sandbox机制
[原创]http://www.cnblogs.com/luoguoqiang1985 ios在安装APP时,把APP的偏好设置与数据放在sandbox里.sandbox通过一系列细颗粒度控制APP访问 ...
- iOS学习之iOS沙盒(sandbox)机制和文件操作(一)
1.iOS沙盒机制 iOS应用程序仅仅能在为该改程序创建的文件系统中读取文件,不能够去其他地方訪问,此区域被成为沙盒,所以全部的非代码文件都要保存在此,比如图像,图标,声音,映像,属性列表,文本文件等 ...
- iOS 文件操作:沙盒(SandBox)、文件操作(FileManager)、程序包(NSBundle)
版权声明:本文为博主原创文章,转载请声明出处:http://blog.csdn.net/jinnchang 1.沙盒机制介绍 iOS 中的沙盒机制(SandBox)是一种安全体系.每个 iOS 应用程 ...
随机推荐
- sharepoint2013安装AppFabric出错
手动安装AppFabric "d:\WindowsServerAppFabricSetup_x64.exe" /i CacheClient,CachingService,Cache ...
- 在gnome3.x中添加eclipse菜单
ubuntu-gnome 16.04采用gnome3,界面和gnome2有较大区别.对于自己手动解压安装,而非用包管理器安装的程序,需要手工建立应用程序的启动快捷方式.例如,对于eclipse程序,采 ...
- Ocelot(七)- 入门
入门 Ocelot仅适用于.NET Core,目前是为netstandard2.0构建的.如果Ocelot适合您,那么此文档可能会有用. .NET 安装NuGet包 使用nuget安装Ocelot及其 ...
- 作为一个程序员,你了解 win 上有哪些必装的软件吗
关于 win 的一些基础必知内容之前已经分享过,没有看过的可以戳此处→Windows 使用之那些你还不知道操作 新系统安装的第一个软件 Google Chrome 毫无疑问,作为程序员应该是首选的浏览 ...
- java String, StringBuffer ,StringBulider 区别
1. String 是不可变的(immutable): 创建后不能修改: 2. StringBuffer 线程安全的,可变字符串: 3. StringBuilder 线程不安全,所以性能比较高
- 浅谈Windows API编程
WinSDK是编程中的传统难点,个人写的WinAPI程序也不少了,其实之所以难就难在每个调用的API都包含着Windows这个操作系统的潜规则或者是windows内部的运行机制…… WinSDK是编程 ...
- 重置iptables
# reset the default policies in the filter table.iptables -P INPUT ACCEPTiptables -P FORWARD ACCEPTi ...
- MYSQL 配置远程连接
例如,你想myuser使用mypassword从任何主机连接到mysql服务器的话. GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'%'IDENTIFIED BY ...
- _bzoj3223 Tyvj 1729 文艺平衡树【Splay】
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=3223 裸的,打个标记. #include <cstdio> #include & ...
- ZooKeeper通过ACL修复未授权访问漏洞
默认情况下ZooKeeper允许匿名访问,因此在安全漏洞扫描中暴漏未授权访问漏洞. 一.参考资料 <ZooKeeper 笔记(5) ACL(Access Control List)访问控制列表& ...