The sandbox mechanism (sandbox) developed by iOS specifically explains some of the sandbox's mechanisms. In development, we need to manipulate the sandbox. So we need to get to the sandbox path.
The contents of the sandbox contain documents, Library, TMP. Please click here for the function of these three directories. Next we will explain how to get the documents, Library, TMP path.
Get the Sandbox root folder
Gets the sandbox root folder. 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 folder path of the simulator if you have time.
Get Documents Path
Get the documents path such as the following:
//获取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 folder, the first parameter is the description Get Doucments folder folder, the second parameter description is obtained in the current app 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/
These are the ways to get the sandbox path in iOS development. Maybe there's a better way to get there and find out. We'll talk about reading and writing files in the sandbox later.
?
Get sandbox path for iOS development