iOS开发之获取沙盒路径

iOS开发之沙盒机制(SandBox)具体解说了沙盒的一些机制。在开发中,我们须要对沙盒进行操作。所以我们须要获取到沙盒路径。

沙盒里的目录包含Documents、Library、tmp。这三个目录的作用请点击这里。接下来我们来解说怎样获取Documents、Library、tmp的路径。

获取沙盒根文件夹

获取沙盒根文件夹。直接调用NSHomeDirectory():

 //获取沙盒根文件夹
NSString *directory = NSHomeDirectory();
NSLog(@"directory:%@", directory);

控制台输出:

2015-07-22 00:40:16.185 iOSStrongDemo[1605:555658] directory:/var/mobile/Containers/Data/Application/F9418815-51A9-4A0A-A76C-6FD37C400928

这个是真机的路径,大家有时间的话能够看看模拟器的根文件夹路径。

获取Documents路径

获取Documents路径例如以下:

//获取Documents路径
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [paths objectAtIndex:0];
NSLog(@"path:%@", path);

控制台输出:

2015-07-22 00:41:41.397 iOSStrongDemo[1613:556159] path:/var/mobile/Containers/Data/Application/A62B886B-A8F0-4215-B59D-1F505C3997BD/Documents

获取Documents文件夹文件夹,第一个參数是说明获取Doucments文件夹文件夹,第二个參数说明是在当前应用沙盒中获取。

获取Library路径

 //获取Library路径
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *path = [paths objectAtIndex:0];
NSLog(@"path:%@", path);

控制台输出:

2015-07-22 00:43:15.803 iOSStrongDemo[1619:556638] /var/mobile/Containers/Data/Application/17300507-4643-4DE7-BC68-E13DB19C8D98/Library

获取Caches路径

//获取Caches路径
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *path = [paths objectAtIndex:0];
NSLog(@"path:%@", path);

控制台输出:

2015-07-22 00:44:31.383 iOSStrongDemo[1626:557083] path:/var/mobile/Containers/Data/Application/1E945B52-E29D-4041-A489-1AA1B11BB960/Library/Caches

获取tmp路径

NSString *tmp = NSTemporaryDirectory();
NSLog(@"tmp:%@", tmp);

控制台输出:

2015-07-22 00:46:07.846 iOSStrongDemo[1632:557537] tmp:/private/var/mobile/Containers/Data/Application/4BE02307-1CC5-47E8-BEA8-CEBB7ED5A402/tmp/

以上是iOS开发中获取沙盒路径的方法。或许还有更好的获取方法等待我们去发现。后面我们还会讲到关于在沙盒里进行读写文件操作。

原文地址:https://www.cnblogs.com/blfbuaa/p/6885299.html