当前位置: 移动技术网 > 移动技术>移动开发>IOS > IOS获取各种文件目录路径的方法

IOS获取各种文件目录路径的方法

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

iphone沙箱模型有四个文件夹,分别是什么,永久数据存储一般放在什么位置,得到模拟器的路径的简单方式是什么.

documents,tmp,app,library。

(nshomedirectory()),

手动保存的文件在documents文件里

nsuserdefaults保存的文件在tmp文件夹里

1、documents 目录:您应该将所有de应用程序数据文件写入到这个目录下。这个目录用于存储用户数据或其它应该定期备份的信息。

2、appname.app 目录:这是应用程序的程序包目录,包含应用程序的本身。由于应用程序必须经过签名,所以您在运行时不能对这个目录中的内容进行修改,否则可能会使应用程序无法启动。

3、library 目录:这个目录下有两个子目录:caches 和 preferences
preferences 目录:包含应用程序的偏好设置文件。您不应该直接创建偏好设置文件,而是应该使用nsuserdefaults类来取得和设置应用程序的偏好.
caches 目录:用于存放应用程序专用的支持文件,保存应用程序再次启动过程中需要的信息。

4、tmp 目录:这个目录用于存放临时文件,保存应用程序再次启动过程中不需要的信息。

获取这些目录路径的方法:

第一种方式:获取家目录路径的函数:

nsstring *homedir = nshomedirectory();


第二种方式:获取documents目录路径的方法:

nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes);
nsstring *docdir = [paths objectatindex:0];

第三种方式:获取caches目录路径的方法:

nsarray *paths = nssearchpathfordirectoriesindomains(nscachesdirectory, nsuserdomainmask, yes);
nsstring *cachesdir = [paths objectatindex:0];

第四种方式:获取tmp目录路径的方法:

nsstring *tmpdir = nstemporarydirectory();

第五种方式:获取应用程序程序包中资源文件路径的方法:
例如获取程序包中一个图片资源(apple.png)路径的方法:

nsstring *imagepath = [[nsbundle mainbundle] pathforresource:@”apple” oftype:@”png”];
uiimage *appleimage = [[uiimage alloc] initwithcontentsoffile:imagepath];

代码中的mainbundle类方法用于返回一个代表应用程序包的对象。

iphone沙盒(sandbox)中的几个目录获取方式:

// 获取沙盒主目录路径 

nsstring *homedir = nshomedirectory(); 

// 获取documents目录路径 

nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); 
nsstring *docdir = [paths objectatindex:0]; 

// 获取caches目录路径 

nsarray *paths = nssearchpathfordirectoriesindomains(nscachesdirectory, nsuserdomainmask, yes); 
nsstring *cachesdir = [paths objectatindex:0]; 

// 获取tmp目录路径 

nsstring *tmpdir = nstemporarydirectory(); 

// 获取当前程序包中一个图片资源(apple.png)路径 

nsstring *imagepath = [[nsbundle mainbundle] pathforresource:@"apple" oftype:@"png"]; 
uiimage *appleimage = [[uiimage alloc] initwithcontentsoffile:imagepath]; 

例子:

nsfilemanager* fm=[nsfilemanager defaultmanager];
if(![fm fileexistsatpath:[self datafilepath]]){

//下面是对该文件进行制定路径的保存

[fm createdirectoryatpath:[self datafilepath] withintermediatedirectories:yes attributes:nil error:nil];

//取得一个目录下得所有文件名

nsarray *files = [fm subpathsatpath: [self datafilepath] ];

//读取某个文件

nsdata *data = [fm contentsatpath:[self datafilepath]];

//或者

nsdata *data = [nsdata datawithcontentofpath:[self datafilepath]];
}

ios获取文件路径的方法有多种,下面介绍一种ios中获取文件路径比较简单方法。

网上的documnet和“教程”真让人越看越糊涂,还是自己记下吧。

首先把文件(比如本例中的testfile.txt文件)放置在resources分组下,然后代码这样写:

nsstring *filepath = [[nsbundle mainbundle] pathforresource:@"testfile" oftype:@"txt"];
nslog(@"data path: %@", filepath); 

输出的日志中你可以看到testfile.txt的路径已经获得。

再举一个例子:连接sqlite数据库

nsstring *datapath = [[nsbundle mainbundle] pathforresource:@"mydata" oftype:@"sqlite"];
if (sqlite3_open([datapath utf8string], &db) != sqlite_ok)
{
  sqlite3_close(db);
  nslog(@"数据库打开失败");
}
else
{
  nslog(@"数据库成功打开");
}

以上内容是ios获取各种文件目录路径的方法,希望对大家有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网