Let's see how to get the application sandbox directory. The directory that includes the sandbox for the real machine.
1, get the program's home directory
NSString *homedirectory = Nshomedirectory (); NSLog (@ "path:%@", homedirectory); |
Printing results:
2012-06-17 14:00:06.098 iossandbox[3536:f803]/users/rongfzh/library/application Support/iPhone SIMULATOR/5.1/ Applications/3b8ec78a-5eee-4c2f-b0cb-4c3f02b996d2 |
What about the catalogue on the real machine? Let's see.
2012-06-17 14:25:47.059 iossandbox[4281:f803]/var/mobile/applications/3b8ec78a-5eee-4c2f-b0cb-4c3f02b996d2
It can be seen that the directory on the real machine is/var/mobile/applications/this directory, and the emulator is not the same. This is the home directory, and the other subdirectories are the same as the emulator.
2. Get the document Directory
Nsarray *paths = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES); NSString *path = [Paths objectatindex:0]; |
Print results
2012-06-17 14:00:06.099 iossandbox[3536:f803] path:/users/rongfzh/library/application Support/iPhone SIMULATOR/5.1/ Applications/3b8ec78a-5eee-4c2f-b0cb-4c3f02b996d2/documents |
3. Get the Cache directory
Nsarray *paths = Nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES); NSString *path = [Paths objectatindex:0]; NSLog (@ "%@", Path); |
Printing results:
2012-06-17 14:03:50.431 iossandbox[3628:f803]/users/rongfzh/library/application Support/iPhone SIMULATOR/5.1/ Applications/3b8ec78a-5eee-4c2f-b0cb-4c3f02b996d2/library/caches |
4. Get the Library directory
Nsarray *paths = Nssearchpathfordirectoriesindomains (Nslibrarydirectory, Nsuserdomainmask, YES); NSString *path = [Paths objectatindex:0]; NSLog (@ "%@", Path); |
Printing results:
2012-06-17 14:07:17.544 iossandbox[3733:f803]/users/rongfzh/library/application Support/iPhone SIMULATOR/5.1/ Applications/3b8ec78a-5eee-4c2f-b0cb-4c3f02b996d2/library |
5. Get the TMP directory
NSString *tmpdir = Nstemporarydirectory (); NSLog (@ "%@", TmpDir); |
Printing results:
2012-06-17 14:08:07.824 iossandbox[3782:f803]/var/folders/g7/246bh79130zblw0yjjtc55cw0000gn/t/ |
6. Writing files
Nsarray *paths = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES); NSString *docdir = [Paths objectatindex:0]; if (!docdir) { NSLog (@ "Documents directory not Found"); } Nsarray *array = [[Nsarray alloc] initwithobjects:@ "Contents", @ "content", nil]; NSString *filepath = [Docdir stringbyappendingpathcomponent:@ "TestFile.txt"]; [Array Writetofile:filepath atomically:yes]; |
Note: We also run on the real machine, write the file, and then read the content from the real machine.
Write input array, inside is two strings, one will read out to print.
Write us in the program sandbox directory to see the file TestFile.txt
Open the file see the content is this, is an XML format of the plist file, the data format to save the content.
7. Read the file
Nsarray *paths = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES); NSString *docdir = [Paths objectatindex:0]; NSString *filepath = [Docdir stringbyappendingpathcomponent:@ "TestFile.txt"]; Nsarray *array = [[Nsarray alloc]initwithcontentsoffile:filepath]; NSLog (@ "%@", array); |
Printing results:
After parsing the above file, the contents are printed out.
2012-06-17 14:14:46.249 iossandbox[3918:f803] ( "\U5185\U5BB9", |
Read and print the file path on the real machine:
2012-06-17 14:25:47.059 iossandbox[4281:f803]/var/mobile/applications/3b8ec78a-5eee-4c2f-b0cb-4c3f02b996d2/ Documents/testfile.txt
(
"\u5185\u5bb9",
Content
)
can also be written and printed on a real machine.