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