当前位置: 移动技术网 > IT编程>移动开发>IOS > 举例详解iOS开发过程中的沙盒机制与文件

举例详解iOS开发过程中的沙盒机制与文件

2019年07月24日  | 移动技术网IT编程  | 我要评论

跑酷,fuel.service.exe,cf穿墙

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

  • 每个应用程序都有自己的存储空间
  • 应用程序不能翻过自己的围墙去访问别的存储空间的内容

打开模拟器沙盒目录
方法1、可以设置显示隐藏文件,然后在finder下直接打开。设置查看隐藏文件的方法如下:打开终端,输入命名
<p class="p1">显示mac隐藏文件的命令:

复制代码 代码如下:
defaults write com.apple.finder appleshowallfiles -bool true</p><p class="p1">

隐藏mac隐藏文件的命令:
复制代码 代码如下:
defaults write com.apple.finder appleshowallfiles -bool false</p>

现在能看到资源库文件夹了。

打开资源库后找到/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 uicollectionview实现横向滚动

    现在使用卡片效果的app很多,之前公司让实现一种卡片效果,就写了一篇关于实现卡片的文章。文章最后附有demo实现上我选择了使用uicollectionview ... [阅读全文]
  • iOS UICollectionView实现横向滑动

    本文实例为大家分享了ios uicollectionview实现横向滑动的具体代码,供大家参考,具体内容如下uicollectionview的横向滚动,目前我使... [阅读全文]
  • iOS13适配深色模式(Dark Mode)的实现

    iOS13适配深色模式(Dark Mode)的实现

    好像大概也许是一年前, mac os系统发布了深色模式外观, 看着挺刺激, 时至今日用着也还挺爽的终于, 随着iphone11等新手机的发售, ios 13系统... [阅读全文]
  • ios 使用xcode11 新建项目工程的步骤详解

    ios 使用xcode11 新建项目工程的步骤详解

    xcode11新建项目工程,新增了scenedelegate这个类,转而将原appdelegate负责的对ui生命周期的处理担子接了过来。故此可以理解为:ios... [阅读全文]
  • iOS实现转盘效果

    本文实例为大家分享了ios实现转盘效果的具体代码,供大家参考,具体内容如下demo下载地址: ios转盘效果功能:实现了常用的ios转盘效果,轮盘抽奖效果的实现... [阅读全文]
  • iOS开发实现转盘功能

    本文实例为大家分享了ios实现转盘功能的具体代码,供大家参考,具体内容如下今天给同学们讲解一下一个转盘选号的功能,直接上代码直接看viewcontroller#... [阅读全文]
  • iOS实现轮盘动态效果

    本文实例为大家分享了ios实现轮盘动态效果的具体代码,供大家参考,具体内容如下一个常用的绘图,主要用来打分之类的动画,效果如下。主要是ios的绘图和动画,本来想... [阅读全文]
  • iOS实现九宫格连线手势解锁

    本文实例为大家分享了ios实现九宫格连线手势解锁的具体代码,供大家参考,具体内容如下demo下载地址:效果图:核心代码://// clockview.m// 手... [阅读全文]
  • iOS实现卡片堆叠效果

    本文实例为大家分享了ios实现卡片堆叠效果的具体代码,供大家参考,具体内容如下如图,这就是最终效果。去年安卓5.0发布的时候,当我看到安卓全新的material... [阅读全文]
  • iOS利用余弦函数实现卡片浏览工具

    iOS利用余弦函数实现卡片浏览工具

    本文实例为大家分享了ios利用余弦函数实现卡片浏览工具的具体代码,供大家参考,具体内容如下一、实现效果通过拖拽屏幕实现卡片移动,左右两侧的卡片随着拖动变小,中间... [阅读全文]
验证码:
移动技术网