Mechanism of the IOS sandbox path

Source: Internet
Author: User

1. Go to the Sandbox directory: Go to folder on the Finder, enter/users/username/library/application support/iphone simulator/to

2. Sandbox structure: Each sandbox contains 3 folders: Documents, Library, and TMP.

Documents: Apple recommends that file data that is created in the program or browsed in the program be saved in this directory, whichis included in itunes backup and restore
Library: The default settings or other status information of the stored program;

Library/caches: Store cached files,itunes does not back up this directory, files in this directory will not be deleted in the app exit

TMP: Provides a place to create temporary files on the fly.

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, all application sandbox directory composed of an array structure of data storage            Nsarray *docpath = Nssearchpathfordirectoriesindomains (Nsdocumentdirectory,nsuserdomainmask, YES);            NSString *documentspath = [DocPath objectatindex:0];            


3. Start creating a file in the directory, and then write the contents to the file:

Method One:

1     Nsarray *docpath = Nssearchpathfordirectoriesindomains (Nsdocumentdirectory,nsuserdomainmask, YES);   2         nsstring *documentspath = [DocPath objectatindex:0];   3         //write file   4         if (!documentspath) {   5             NSLog (@ "directory not Found");   6         }else {   7             nsstring *filepaht = [Documentspath stringbyappendingpathcomponent:@ "test.txt"];   8             Nsarray *array = [Nsarray arraywithobjects:@ "Title", @ "Contents", nil];   9             [Array writetofile:filepaht atomically:yes];  Ten         }

Method Two:

Save the path to the background map file about the user picture

#define User_background_path [Nshomedirectory () stringbyappendingpathcomponent:@ "Documents/userbackground"]

Create a sandbox path

[Nsfilemanager Createfilefolder:user_background_path];

Write the picture into the sandbox path

NSString *filepath = nil;

FilePath = [NSString stringWithFormat: @ "%@/%@.jpg", User_background_path, FileName];

[Imagejpegdata Writetofile:filepath Atomically:yes]

4. The next step is to read the contents of the file:

Method One:

1     //Read file  2     nsarray *docpath = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES);  3         nsstring *documentspath = [DocPath objectatindex:0];  4         nsstring *readpath = [Documentspath stringbyappendingpathcomponent:@ "test.txt"];  5         Nsarray *filecontent = [[Nsarrayalloc] initwithcontentsoffile:readpath];  6         

Method Two:

Get the path to a picture

+ (NSString *) Getbackgroundfilepath: (NSString *) fileName

{

if (FileName = = nil) {

return nil;

}

return [NSString stringWithFormat: @ "%@/%@.jpg", User_background_path, FileName];

}

Reference: http://www.cnblogs.com/taintain1984/archive/2013/03/19/2969201.html

Mechanism of the IOS sandbox path

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.