iOS learning iOS sandbox (sandbox) mechanism and file operations 1

Source: Internet
Author: User

iOS learning iOS sandbox (sandbox) mechanism and file operations

iOS sandbox (sandbox) mechanism and file operation (one) 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 [CPP] view plain copy nsstring *homedirectory = Nshomedirectory (); NSLog (@ "path:%@", homedirectory); Print result: [CPP] View plain copy 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 directory 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 visible , 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 [CPP] view plain copy nsarray *paths = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES); NSString *path = [Paths objectatindex:0]; NSLog (@ "path:%@", Path); Print results [CPP] View plain copy 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 [CPP] view plain copy nsarray *paths = Nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES); NSString *path = [Paths objectatindex:0]; NSLog (@ "%@", Path); Print results [CPP] View plain copy 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 [CPP] view plain copy nsarray *paths = Nssearchpathfordirectoriesindomains (Nslibrarydirectory, Nsuserdomainmask, YES); NSString *path = [Paths objectatindex:0]; NSLog (@ "%@", Path); Print results [CPP] View plain copy 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 TMP directory [CPP] view plain copy nsstring *tmpdir = Nstemporarydirectory (); NSLog (@ "%@", TmpDir); Print results [CPP] View plain copy 2012-06-17 14:08:07.824 iossandbox[3782:f803]/var/folders/g7/ 246bh79130zblw0yjjtc55cw0000gn/t/

6. Write file [CPP] View plain copy 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 see the file TestFile.txt open file see the content is this, is an XML format plist file, the data format saved content. [CPP] View plain copy

Content contents

7. read file [CPP] View plain copy 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 content is printed out. [CPP] View plain copy 2012-06-17 14:14:46.249 iossandbox[3918:f803] ("\u5185\u5bb9", content) Read and print the file path on the real machine: 2012-06-17 1 4: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.

iOS learning iOS sandbox (sandbox) mechanism and file operations 1

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.