The sandbox mechanism for iOS development (sandbox) details some of the sandbox mechanisms. In development, we need to manipulate the sandbox, so we need to get to the sandbox path.
The folders in the sandbox include documents, Library, TMP. Click here for the effects of these three folders. Next we will explain how to get the documents, Library, TMP path.
Get the Sandbox root directory
Get the sandbox root and call Nshomedirectory () directly:
//获取沙盒根目录NSString *directory = NSHomeDirectory();NSLog(@"directory:%@", directory);
Console output:
2015-07-22 00:40:16.185 iOSStrongDemo[1605:555658] directory:/var/mobile/Containers/Data/Application/F9418815-51A9-4A0A-A76C-6FD37C400928
This is the path of the real machine, you can see the root directory path of the simulator if you have time.
Get Documents Path
Get the documents path as follows:
//获取Documents路径NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);NSString *path = [paths objectAtIndex:0];NSLog(@"path:%@", path);
Console output:
2015-07-22 00:41:41.397 iOSStrongDemo[1613:556159] path:/var/mobile/Containers/Data/Application/A62B886B-A8F0-4215-B59D-1F505C3997BD/Documents
Get the Documents folder directory, the first parameter is the description get doucments folder directory, the second parameter description is obtained in the current application sandbox.
Get the Library Path
//获取Library路径NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);NSString *path = [paths objectAtIndex:0];NSLog(@"path:%@", path);
Console output:
2015-07-22 00:43:15.803 iOSStrongDemo[1619:556638] /var/mobile/Containers/Data/Application/17300507-4643-4DE7-BC68-E13DB19C8D98/Library
Get caches path
//获取Caches路径NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);NSString *path = [paths objectAtIndex:0];NSLog(@"path:%@", path);
Console output:
2015-07-22 00:44:31.383 iOSStrongDemo[1626:557083] path:/var/mobile/Containers/Data/Application/1E945B52-E29D-4041-A489-1AA1B11BB960/Library/Caches
Get the TMP path
NSString *tmp = NSTemporaryDirectory();NSLog(@"tmp:%@", tmp);
Console output:
2015-07-22 00:46:07.846 iOSStrongDemo[1632:557537] tmp:/private/var/mobile/Containers/Data/Application/4BE02307-1CC5-47E8-BEA8-CEBB7ED5A402/tmp/
The above is the way to get the sandbox path in iOS development, and perhaps there is a better way to get it to discover. We'll talk about reading and writing files in the sandbox later.
?
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Get sandbox path for iOS development