陈斌彬的技术博客

Stay foolish,stay hungry

Objective-c NSHomeDirectory() 使用方法(原创)

NSString *path = NSHomeDirectory();

上面的代码得到的是应用程序目录的路径,在该目录下有三个文件夹:Documents、Library、temp 以及一个 .app 包!

该目录下就是应用程序的沙盒,应用程序只能访问该目录下的文件夹!!!

请参考下面的例子:

1、

NSString *path1 = NSHomeDirectory();
NSLog(@"path1:%@", path1);

img

2、

NSString *path2 = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
NSLog(@"path2:%@", path2);

运行:

2015-06-30 14:32:41.177 Recording Audio[19640:263700] path2:/Users/apple/Library/Developer/CoreSimulator/Devices/359DAF7A-50F0-4CF6-B332-7567A557B3A4/data/Containers/Data/Application/407FC83F-0E15-4D4E-B5F1-7F9D2F58115E/Library/Caches

img

3、

NSString *path3 = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSLog(@"path3:%@", path3);

运行:

2015-06-30 14:34:54.691 Recording Audio[19785:266254] path3:/Users/apple/Library/Developer/CoreSimulator/Devices/359DAF7A-50F0-4CF6-B332-7567A557B3A4/data/Containers/Data/Application/B42D3344-8402-4805-9AB7-39B6389B6D54/Documents

img

4、

NSString *path4 = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSLog(@"path4:%@", path4);

运行:

2015-06-30 14:34:54.692 Recording Audio[19785:266254] path4:/Users/apple/Library/Developer/CoreSimulator/Devices/359DAF7A-50F0-4CF6-B332-7567A557B3A4/data/Containers/Data/Application/B42D3344-8402-4805-9AB7-39B6389B6D54/Documents

img

5、

NSString *path5 = [NSHomeDirectory() stringByAppendingPathComponent:@"Library"];
NSLog(@"path5:%@", path5);

运行:

2015-06-30 14:34:54.692 Recording Audio[19785:266254] path5:/Users/apple/Library/Developer/CoreSimulator/Devices/359DAF7A-50F0-4CF6-B332-7567A557B3A4/data/Containers/Data/Application/B42D3344-8402-4805-9AB7-39B6389B6D54/Library

img

6、

NSString *path6 = [NSHomeDirectory() stringByAppendingPathComponent:@"temp"];
NSLog(@"path6:%@", path6);

运行:

2015-06-30 14:34:54.692 Recording Audio[19785:266254] path6:/Users/apple/Library/Developer/CoreSimulator/Devices/359DAF7A-50F0-4CF6-B332-7567A557B3A4/data/Containers/Data/Application/B42D3344-8402-4805-9AB7-39B6389B6D54/temp

img