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怎么获取沙盒路径,怎么操作文件呢?下面给出答案。

获取应用沙盒根路径:

  1. -(void)dirHome{
  2. NSString *dirHome=NSHomeDirectory();
  3. NSLog(@"app_home: %@",dirHome);
  4. }

获取Documents目录路径:

  1. //获取Documents目录
  2. -(NSString *)dirDoc{
  3. //[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
  4. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  5. NSString *documentsDirectory = [paths objectAtIndex:0];
  6. NSLog(@"app_home_doc: %@",documentsDirectory);
  7. return documentsDirectory;
  8. }

获取Library目录路径:

  1. //获取Library目录
  2. -(void)dirLib{
  3. //[NSHomeDirectory() stringByAppendingPathComponent:@"Library"];
  4. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
  5. NSString *libraryDirectory = [paths objectAtIndex:0];
  6. NSLog(@"app_home_lib: %@",libraryDirectory);
  7. }

获取Cache目录路径:

  1. //获取Cache目录
  2. -(void)dirCache{
  3. NSArray *cacPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  4. NSString *cachePath = [cacPath objectAtIndex:0];
  5. NSLog(@"app_home_lib_cache: %@",cachePath);
  6. }

获取Tmp目录路径:

  1. //获取Tmp目录
  2. -(void)dirTmp{
  3. //[NSHomeDirectory() stringByAppendingPathComponent:@"tmp"];
  4. NSString *tmpDirectory = NSTemporaryDirectory();
  5. NSLog(@"app_home_tmp: %@",tmpDirectory);
  6. }

创建文件夹:

  1. //创建文件夹
  2. -(void *)createDir{
  3. NSString *documentsPath =[self dirDoc];
  4. NSFileManager *fileManager = [NSFileManager defaultManager];
  5. NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];
  6. // 创建目录
  7. BOOL res=[fileManager createDirectoryAtPath:testDirectory withIntermediateDirectories:YES attributes:nil error:nil];
  8. if (res) {
  9. NSLog(@"文件夹创建成功");
  10. }else
  11. NSLog(@"文件夹创建失败");
  12. }

创建文件

  1. //创建文件
  2. -(void *)createFile{
  3. NSString *documentsPath =[self dirDoc];
  4. NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];
  5. NSFileManager *fileManager = [NSFileManager defaultManager];
  6. NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"];
  7. BOOL res=[fileManager createFileAtPath:testPath contents:nil attributes:nil];
  8. if (res) {
  9. NSLog(@"文件创建成功: %@" ,testPath);
  10. }else
  11. NSLog(@"文件创建失败");
  12. }

写数据到文件:

  1. //写文件
  2. -(void)writeFile{
  3. NSString *documentsPath =[self dirDoc];
  4. NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];
  5. NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"];
  6. NSString *content=@"测试写入内容!";
  7. BOOL res=[content writeToFile:testPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
  8. if (res) {
  9. NSLog(@"文件写入成功");
  10. }else
  11. NSLog(@"文件写入失败");
  12. }

读文件数据:

  1. //读文件
  2. -(void)readFile{
  3. NSString *documentsPath =[self dirDoc];
  4. NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];
  5. NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"];
  6. //    NSData *data = [NSData dataWithContentsOfFile:testPath];
  7. //    NSLog(@"文件读取成功: %@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
  8. NSString *content=[NSString stringWithContentsOfFile:testPath encoding:NSUTF8StringEncoding error:nil];
  9. NSLog(@"文件读取成功: %@",content);
  10. }

文件属性:

  1. //文件属性
  2. -(void)fileAttriutes{
  3. NSString *documentsPath =[self dirDoc];
  4. NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];
  5. NSFileManager *fileManager = [NSFileManager defaultManager];
  6. NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"];
  7. NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:testPath error:nil];
  8. NSArray *keys;
  9. id key, value;
  10. keys = [fileAttributes allKeys];
  11. int count = [keys count];
  12. for (int i = 0; i < count; i++)
  13. {
  14. key = [keys objectAtIndex: i];
  15. value = [fileAttributes objectForKey: key];
  16. NSLog (@"Key: %@ for value: %@", key, value);
  17. }
  18. }

删除文件:

    1. //删除文件
    2. -(void)deleteFile{
    3. NSString *documentsPath =[self dirDoc];
    4. NSString *testDirectory = [documentsPath stringByAppendingPathComponent:@"test"];
    5. NSFileManager *fileManager = [NSFileManager defaultManager];
    6. NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.txt"];
    7. BOOL res=[fileManager removeItemAtPath:testPath error:nil];
    8. if (res) {
    9. NSLog(@"文件删除成功");
    10. }else
    11. NSLog(@"文件删除失败");
    12. NSLog(@"文件是否存在: %@",[fileManager isExecutableFileAtPath:testPath]?@"YES":@"NO");
    13. }

iOS sandbox的更多相关文章

  1. IOS 学习之 iOS沙盒(sandbox) 介绍 沙盒机制 文件操作(一)

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

  2. iOS 沙盒(sandbox)结构 使用 实例

    声明:该文档是经过自己查找网上的资料以及自己多年的经验后而总结出来的,希望对大家有所帮助,有什么不恰当支出还请大家多指点! iOS中的沙盒机制(SandBox)是一种安全体系,它规定了应用程序只能在为 ...

  3. iOS 沙盒(sandbox)机制和文件操作

    本文参看了 http://www.uml.org.cn/mobiledev/201209211.asp#1 这篇文章中的介绍,尊重原著. 1.IOS沙盒机制 IOS应用程序只能在本应用程序中创建的文件 ...

  4. iOS的SandBox的结构研究

    在模拟器中运行iOS程序,都会为该程序创建一个沙盒(SandBox).首先声明,我用的系统是Max OS X 10.7.3,编译器是Xcode 4.3.2.想要找到沙盒目录,先运行Finder,然后在 ...

  5. IOS学习之IOS沙盒(sandbox)机制和文件操作

    IOS学习之IOS沙盒(sandbox)机制和文件操作(一) 1.IOS沙盒机制 IOS应用程序只能在为该改程序创建的文件系统中读取文件,不可以去其它地方访问,此区域被成为沙盒,所以所有的非代码文件都 ...

  6. 【iOS开发必收藏】详解iOS应用程序内使用IAP/StoreKit付费、沙盒(SandBox)测试、创建测试账号流程!【2012-12-11日更新获取”产品付费数量等于0的问题”】

    转的别人的 看到很多童鞋问到,为什么每次都返回数量等于0?? 其实有童鞋已经找到原因了,原因是你在 ItunesConnect 里的 “Contracts, Tax, and Banking”没有完成 ...

  7. ios专题 - sandbox机制

    [原创]http://www.cnblogs.com/luoguoqiang1985 ios在安装APP时,把APP的偏好设置与数据放在sandbox里.sandbox通过一系列细颗粒度控制APP访问 ...

  8. iOS学习之iOS沙盒(sandbox)机制和文件操作(一)

    1.iOS沙盒机制 iOS应用程序仅仅能在为该改程序创建的文件系统中读取文件,不能够去其他地方訪问,此区域被成为沙盒,所以全部的非代码文件都要保存在此,比如图像,图标,声音,映像,属性列表,文本文件等 ...

  9. iOS 文件操作:沙盒(SandBox)、文件操作(FileManager)、程序包(NSBundle)

    版权声明:本文为博主原创文章,转载请声明出处:http://blog.csdn.net/jinnchang 1.沙盒机制介绍 iOS 中的沙盒机制(SandBox)是一种安全体系.每个 iOS 应用程 ...

随机推荐

  1. sharepoint2013安装AppFabric出错

    手动安装AppFabric "d:\WindowsServerAppFabricSetup_x64.exe" /i CacheClient,CachingService,Cache ...

  2. 在gnome3.x中添加eclipse菜单

    ubuntu-gnome 16.04采用gnome3,界面和gnome2有较大区别.对于自己手动解压安装,而非用包管理器安装的程序,需要手工建立应用程序的启动快捷方式.例如,对于eclipse程序,采 ...

  3. Ocelot(七)- 入门

    入门 Ocelot仅适用于.NET Core,目前是为netstandard2.0构建的.如果Ocelot适合您,那么此文档可能会有用. .NET 安装NuGet包 使用nuget安装Ocelot及其 ...

  4. 作为一个程序员,你了解 win 上有哪些必装的软件吗

    关于 win 的一些基础必知内容之前已经分享过,没有看过的可以戳此处→Windows 使用之那些你还不知道操作 新系统安装的第一个软件 Google Chrome 毫无疑问,作为程序员应该是首选的浏览 ...

  5. java String, StringBuffer ,StringBulider 区别

    1. String 是不可变的(immutable): 创建后不能修改: 2. StringBuffer 线程安全的,可变字符串: 3. StringBuilder 线程不安全,所以性能比较高

  6. 浅谈Windows API编程

    WinSDK是编程中的传统难点,个人写的WinAPI程序也不少了,其实之所以难就难在每个调用的API都包含着Windows这个操作系统的潜规则或者是windows内部的运行机制…… WinSDK是编程 ...

  7. 重置iptables

    # reset the default policies in the filter table.iptables -P INPUT ACCEPTiptables -P FORWARD ACCEPTi ...

  8. MYSQL 配置远程连接

    例如,你想myuser使用mypassword从任何主机连接到mysql服务器的话.  GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'%'IDENTIFIED BY ...

  9. _bzoj3223 Tyvj 1729 文艺平衡树【Splay】

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=3223 裸的,打个标记. #include <cstdio> #include & ...

  10. ZooKeeper通过ACL修复未授权访问漏洞

    默认情况下ZooKeeper允许匿名访问,因此在安全漏洞扫描中暴漏未授权访问漏洞. 一.参考资料 <ZooKeeper 笔记(5) ACL(Access Control List)访问控制列表& ...