IOS sandbox mechanism and file operations for iOS Learning (2)

Source: Internet
Author: User

Next

IOS sandbox mechanism and file operations for iOS Learning (1)

 

 

Let's see how to get the sandbox directory of the application. Including the sandbox directory of the real machine.

 

1. Get the Home Directory of the program

 

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

 

Print result:

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 take a look

14:25:47. 059 IosSandbox [4281: f803]/var/mobile/Applications/3B8EC78A-5EEE-4C2F-B0CB-4C3F02B996D2

The directory on the real machine is/var/mobile/Applications/, which is different from that on the simulator. This is the Home directory. Other subdirectories are the same as those in the simulator.

 

2. Get the document directory

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);    NSString *path = [paths objectAtIndex:0];    NSLog(@"path:%@", path);

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. Obtain the Cache directory

 

 

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);    NSString *path = [paths objectAtIndex:0];    NSLog(@"%@", path);

Print 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. Obtain the Library directory

 

 

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);    NSString *path = [paths objectAtIndex:0];    NSLog(@"%@", path);

Print 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);

Print results

 

 

2012-06-17 14:08:07.824 IosSandbox[3782:f803] /var/folders/g7/246bh79130zblw0yjjtc55cw0000gn/T/

6. Write files

NSArray * paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES); NSString * docDir = [paths objectAtIndex: 0]; if (! DocDir) {NSLog (@ "events directory not found");} NSArray * array = [[NSArray alloc] initWithObjects: @ "content", @ "content", nil]; NSString * filePath = [docDir stringByAppendingPathComponent: @ "testFile.txt"]; [array writeToFile: filePath atomically: YES];

 

Note: We also run it on the real machine to write the file and read the content from the real machine.

Write the input array, which contains two strings. We will read and print them later.

Write the file testFile.txt in the sandbox directory of the program.

 

Open the file and you will see the following content: A plist file in xml format that stores the content.

 

<? Xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE plist PUBLIC "-// Apple // dtd plist 1.0 // EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version = "1.0"> <array> <string> content </string> <string> content </string> </array> </plist>

7. Read files

 

 

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);

Print result:

 

After the above file is parsed, the content is printed out.

 

2012-06-17 14:14:46.249 IosSandbox[3918:f803] (    "\U5185\U5bb9",    content)

 

Read and print the file path on the real machine:

 

14:25:47. 059 IosSandbox [4281: f803]/var/mobile/Applications/3B8EC78A-5EEE-4C2F-B0CB-4C3F02B996D2/Documents/testFile.txt

(

 

"\ U5185 \ U5bb9 ",

Content

)

The real machine can also write and print data.

Copyright Disclaimer: This article will be published at http://blog.csdn.net/totogo2010/, and you will be welcomed to enjoy the transfer and sharing. Please respect the work of the author. Keep this note and the author's blog link when reprinting. Thank you.

 

 
Related Article

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.