Home>

ios sandbox mechanism

The ios application can only read files in the file system created for the program.Not allowed to visit elsewhere,This area is called a sandbox,So all non-code files should be saved here,Images,Icon, sound, image, property list,Text files, etc.

Each application has its own storage space Apps cannot go over their own fences to access content in other storage spaces

Open the simulator sandbox directory

Method 1, you can set to display hidden files,Then open it directly under finder.Here's how to set up hidden files:Open a terminal,Enter a name

<p>Command to show mac hidden files:

defaults write com.apple.finder appleshowallfiles -bool true</p><p>

Command to hide mac hidden files:

defaults write com.apple.finder appleshowallfiles -bool false</p>

You can now see the repository folder.

After opening the resource library, locate the/application support/iphone simulator/folder. This is the sandbox directory of the simulator's various programs.

Method 2, this method is more convenient,Click->on the finder and then press and hold the "option" key, then the "resource library" will appear, the other is the same as above

Directory Structure

by default,Each sandbox contains 3 folders:documents, library and tmp. Because of the applied sandbox mechanism,Applications can only read and write files in several directories

documents:Apple recommends saving the file data created in the program or browsed in the program under this directory,Itunes will include this directory when backing up and restoring

library:stores the default settings or other status information of the program;

library/caches:store cache files,itunes does not back up this directory,Files in this directory will not be deleted when the application exits

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

Itunes backs up all documents and library files when syncing with iPhone.

When iphone restarts,All tmp files will be discarded.

Here are the three directories mentioned above:documents, library, tmp

A few commonly used code examples:

1.Get the program's home directory

nsstring * homedirectory=nshomedirectory ();
nslog (@ "path:%@", homedirectory);

2.Get the document directory

nsarray * paths=nssearchpathfordirectoriesindomains (nsdocumentdirectory, nsuserdomainmask, yes);
nsstring * path=[paths objectatindex:0];
nslog (@ "path:%@", path);

3.Get cache directory

nsarray * paths=nssearchpathfordirectoriesindomains (nscachesdirectory, nsuserdomainmask, yes);
nsstring * path=[paths objectatindex:0];
nslog (@ "%@", path);

4.Get the library directory

nsarray * paths=nssearchpathfordirectoriesindomains (nslibrarydirectory, nsuserdomainmask, yes);
nsstring * path=[paths objectatindex:0];
nslog (@ "%@", path);

5.Get tmp directory

nsstring * tmpdir=nstemporarydirectory ();
nslog (@ "%@", tmpdir);

Write to file

nsarray * paths=nssearchpathfordirectoriesindomains (nsdocumentdirectory, nsuserdomainmask, yes);
 nsstring * docdir=[paths objectatindex:0];
 if (! docdir) {
 nslog (@ "documents directory not found");
 }
 nsarray * array=[[nsarray alloc] initwithobjects:@"content",@"content", nil];
 nsstring * filepath=[docdir stringbyappendingpathcomponent:@"testfile.txt"];
 [array writetofile:filepath atomically:yes];

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

8, determine whether a file exists,Incoming full path (fileexistsatpath)

//create file manager
nsfilemanager * filemanager=[nsfilemanager defaultmanager];
nsstring * documents=[nssearchpathfordirectoriesindomains (nsdocumentdirectory, nsuserdomainmask, yes) lastobject];
nsstring * filepath=[documents stringbyappendingpathcomponent:@"test"];
 //determine if a file exists,Incoming full path
 if ([filemanager fileexistsatpath:filepath]) {
 nslog (@ "it is exit");
 }

9.Create directories in documents

nsarray * paths=nssearchpathfordirectoriesindomains (nsdocumentdirectory, nsuserdomainmask, yes);
 nsstring * documentsdirectory=[paths objectatindex:0];
 nslog (@ "documentsdirectory%@", documentsdirectory);
 nsfilemanager * filemanager=[nsfilemanager defaultmanager];
 nsstring * testdirectory=[documentsdirectory stringbyappendingpathcomponent:@"test"];
 //Create a directory
 [filemanager createdirectoryatpath:testdirectory withintermediatedirectories:yes attributes:nil error:nil];

10.Create files in the directory

nsstring * testpath=[testdirectory stringbyappendingpathcomponent:@"test00.txt"];
nsstring * testpath2=[testdirectory stringbyappendingpathcomponent:@"test22.txt"];
nsstring * testpath3=[testdirectory stringbyappendingpathcomponent:@"test33.txt"];
nsstring * string [email protected]"
write string ";
[filemanager createfileatpath:testpath contents:[string datausingencoding:nsutf8stringencoding] attributes:nil];
[filemanager createfileatpath:testpath2 contents:[string datausingencoding:nsutf8stringencoding] attributes:nil];
[filemanager createfileatpath:testpath3 contents:[string datausingencoding:nsutf8stringencoding] attributes:nil];

11.Get all file names in the directory column

Two methods to obtain:subpathsofdirectoryatpath and subpathsatpath

nsarray * paths=nssearchpathfordirectoriesindomains (nsdocumentdirectory, nsuserdomainmask, yes);
nsstring * documentsdirectory=[paths objectatindex:0];
nslog (@ "documentsdirectory%@", documentsdirectory);
nsfilemanager * filemanage=[nsfilemanager defaultmanager];
nsstring * mydirectory=[documentsdirectory stringbyappendingpathcomponent:@"test"];
nsarray * file=[filemanage subpathsofdirectoryatpath:mydirectory error:nil];
nslog (@ "%@", file);
nsarray * files=[filemanage subpathsatpath:mydirectory];
nslog (@ "%@", files);

12, filemanager use the current directory

//Create a file manager
 nsfilemanager * filemanager=[nsfilemanager defaultmanager];
 nsarray * paths=nssearchpathfordirectoriesindomains (nsdocumentdirectory, nsuserdomainmask, yes);
 nsstring * documentsdirectory=[paths objectatindex:0];
 //Change to the directory to be operated
 [filemanager changecurrentdirectorypath:[documentsdirectory stringbyexpandingtildeinpath]];
 //create file filename file name,contents of the contents file,If there is no content in the beginning, it can be set to nil, attributes of the attributes file,Initial nil
 nsstring * filename [email protected]"testfilensfilemanager.txt";
 nsarray * array=[[nsarray alloc] initwithobjects:@"hello world",@"hello world1",@"hello world2", nil];
 [filemanager createfileatpath:filename contents:array attributes:nil];

Delete files

[filemanager removeitematpath:filename error:nil];
ios
  • Previous C # method to simply merge word documents
  • Next Detailed knowledge of the use of class templates in C ++ programming